sql server - Exchangeable fields in SQL index -


i m designing table list of chats. every chat references 2 users , must unique per given user pair. evidently, user pair symmetric permutations: not matter, user in pair comes first , 1 comes second.

suppose user table has integer userid pk. chat table have pair of fk fields: userid1, userid2. want every pair of users have unique record in chats table. can create unique index(userid1, userid2). however, index not unique in terms of user pair because may include permutations when userid2 comes first: (userid1, userid2) , (userid2, userid1) 2 distinct different pairs, while required logic, should treated 1 distinct record.

is there way implement construct in pure sql without external coding, such db triggers or scripting? m using ms sql server prototyping. want have design universal , neutral possible compatible sql compliant databases. mere question on optimal architecture on specific sql implementation code such.

possible ideas:

  1. make , index on hash of ordered user pairs
  2. check user order , put user smaller id first in queries

but these put restrictions on external queries outside of sql.


Comments

Popular posts from this blog

java - Suppress Jboss version details from HTTP error response -

gridview - Yii2 DataPorivider $totalSum for a column -

Sass watch command compiles .scss files before full sftp upload -