Is it considered bad practice to have a UNIQUE constraint applying to all columns on a table, essentially enforcing unique rows?

My use case is to store tokens from textual message from various sources, tracking the ID of each message. There can be many tokens per message, and IDs may clash from various sources.

I want to DO NOTHING in the case of reading the same message multiple times, rather than removing duplicates after the fact. I also want to be able to quickly access the tokens for any particular message, hence indexing on (source,id). Is something like the below a good design for this purpose?

source | id | token
x      | 1  | token_1
x      | 1  | token_2
y      | 1  | token_3
y      | 2  | token_1

idx (source, id)
pk (source, id, token)
  • 3
    There's nothing wrong with having all attributes forming a key. However, index on (source, id) is probably redundant. – mustaccio May 25 '19 at 15:14
  • 1
    It is not bad practice if this is what your application / business requires. – Albert Godfrind May 26 '19 at 14:26
  • 1
    An index does not constrain to UNIQUE. – philipxy May 28 '19 at 5:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.