I have a table
Item(ItemName*, ItemSize*, Price, Notes)
I was making composite key of (ItemName,ItemSize) to uniquely identify item. And now after reading some answers on stackoverflow suggesting the use of UNIQUE i revised it as
Item(ItemID*, ItemName, ItemSize, Price, Notes)
But How to apply UNIQUE constraint on ItemName and ItemSize
please correct if there is something wrong in question
CREATE TABLE employee ( id INT NOT NULL PRIMARY KEY, name VARCHAR(100) UNIQUE, address VARCHAR(100), designation VARCHAR(100) ); The above SQL Query creates a table named employee and makes the column name unique by adding the UNIQUE clause at the end of the column definition (name VARCHAR(100) UNIQUE).
Like any other column in a table, composite keys can also be operated with the alter commands. You can easily perform add or drop operations in a table by using the alter command with the composite key in SQL. To add additional columns in the set of columns forming the composite key, you can use the alter-add command.
Unique key constraints can accept only one NULL value for column. Unique constraints are also referenced by the foreign key of another table. A composite key is having two or more attributes that together can uniquely identify a tuple in a table.
To select more than one field to create a composite key, hold down CTRL and then click the row selector for each field. On the Design tab, in the Tools group, click Primary Key. A key indicator is added to the left of the field or fields that you specify as the primary key.
ALTER TABLE Items ADD UNIQUE INDEX(ItemName, ItemSize);
and here's an article explaining how to achieve the same using SQL Server Management Studio.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With