Define Primary and Unique key.Primary Key
- The column or columns of the table whose value uniquely identifies each row in the table is called primary key.
- You can define column as primary key using primary key constraint while you create table.
- When you define a column as primary key, a unique index is created which restricts duplicate data and fast access to data.
- A column defined as primary key doesn’t allow null value.
- By default, clustered index in created with the column having primary key.
- Unique key also ensures data uniqueness like primary key.
- A column with unique key defined allows null value.
- By default, it creates non-clustered index.
Explain the difference between a primary key and a unique key.- Both are defined to ensure unique row.
- Primary key creates a clustered index on the column by default.
- Unique creates a non-clustered index by default.
- Primary key doesn't allow NULLs, but unique key allows one NULL only.
Define Primary and Unique key.Primary key: Enforces uniqueness of the column they are attached to. It creates a clustered index on the column and does not allow null values. Every table can have only one primary key.
Unique Key: Enforces uniqueness of the column they are attached to. It creates a non clustered index on the column and allows only one NULL value.