Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Unique Composite Key

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

like image 259
SMUsamaShah Avatar asked Jan 15 '11 10:01

SMUsamaShah


People also ask

How do I use a unique key?

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).

How do you use a composite key?

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.

Is composite key and unique key same?

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.

How do I access my composite key?

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.


1 Answers

ALTER TABLE Items ADD UNIQUE INDEX(ItemName, ItemSize);

and here's an article explaining how to achieve the same using SQL Server Management Studio.

like image 91
Darin Dimitrov Avatar answered Nov 04 '22 00:11

Darin Dimitrov