- Type of database constraint that ensures that values in a column or combination of column
- unique across all the rows in a table.
- Prevent duplicated data from entering into the table.
While Creating the table
CREATE TABLE table_name (
column1 datatype UNIQUE,
column2 datatype,
...
);
E.g.
After Creating the table
- Difference between Primary key and Unique key
S.N.
Primary Key
Unique Key
1.
Uniquely identifies each row in a table
Ensures that values in one or more columns are unique
Cannot contain null values
Can contain Null value but only one per column.
Can be used to create foreign key relationship with another table.
Cannot be used to create foreign key relationships with another table.
Each table can only have one primary key
A table can have more than one unique key
Comments
Post a Comment