user1298426 user1298426 - 2 months ago 13
SQL Question

How to maintain unique values in database?

I have attribute table with attribute type and value field.

AttrType -- AttrValue
----------------------------------
attr1 -- value1
attr2 -- value2
attr3 -- value3
attr1 -- value1


I need to make sure that attribute type 'attr1' must have unique values.
Like above value1 is repeated for attr1 which I need to avoid.

Is there any database level check that I can add on table?

Answer

Add a unique constraint to your table:

alter table tablename add constraint constraintname unique(AttrType, AttrValue)

(Can also be done when create table.)