Is declaring an attribute of a table as UNIQUE equivalent to declaring it as PRIMARY KEY?
thanks a lot!
A unique key is a set of one or more than one fields/columns of a table that uniquely identify a record in a database table. You can say that it is little like primary key but it can accept only one null value and it cannot have duplicate values.
Purpose: Primary Key is used to uniquely identify a row but a unique key is used to prevent duplicate values in a column. Existence: A table can have only one primary key but it can have multiple unique keys.
yes, you can have primary key and unique key in the same MySQL table.
The different is: Primary key will create clustered index by default and only one PK can exist in one table. Primary key can cover multiple columns (composite key)
UNIQUE can still be NULL.
PRIMARY KEY means UNIQUE and NOT NULL and there can be only one PRIMARY KEY per table.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With