phprockers-logo

Difference between Primary Key and Unique key in mysql?

Difference between Primary Key and Unique key in mysql/sql/oracle?

SNO Primary Key Unique Key
1 Primary Key does not allow NULLs values in a table Unique Key allows Nulls value in a table
2 Only one primary key will be there in a table More than one unique key will be there in a table
3 Clustered index is created in primary key in a table Non-Clustered index is created in unique key in a table
4 Primary key allows each row in a table to be uniquely identified and ensures that no duplicate rows exist as well as does not allow the duplicates values in the rows of a table. Unique key constraint is used to prevent the duplication of key values within the rows of a table and allow null values

By default Primary Key creates a Clustered Index on the column as same as Unique Key creates a Nonclustered Index. Another major difference is Primary Key doesn’t allow NULLs, but Unique Key allows NULLs. We can create only one primary key in a table, but a table can have the multiple unique key.

0 comments:

Post a Comment