FresherFresher (0–1 yrs)CodingSQLInfosysAccentureAmazon
PRIMARY KEY vs UNIQUE
Difference between PRIMARY KEY and UNIQUE constraints?
Answers use simple, clear English.
Quick interview answer
PRIMARY KEY uniquely identifies a row, disallows NULL, and usually implies a clustered index. UNIQUE also enforces uniqueness but may allow one/multiple NULLs depending on the engine, and a table can have many UNIQUE constraints but only one PRIMARY KEY.
Detailed answer
PRIMARY KEY uniquely identifies a row, disallows NULL, and usually implies a clustered index. UNIQUE also enforces uniqueness but may allow one/multiple NULLs depending on the engine, and a table can have many UNIQUE constraints but only one PRIMARY KEY.
Code example
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL
);Practice code · sql (view only · no execution)
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(255) UNIQUE NOT NULL
);#sql#basics