What is the difference between "Primary Key" and "Unique Key" in SQL?

I've had a chance to with a Postgres database and came across a table with two primary keys. This intrigues me to look up a definition of primary and unique key. I always thought there would only be one primary key in a table, and the values have to be unique. But, what I saw in the PostgreSQL v8.1.23 was something otherwise. It allowed duplicate entries, and also allowed multiple primary keys in a table. Is this possible? Yes, it's called composite Primary Keys. Composite primary keys (multiple primary keys) makeup uniqueness in a table row -- which means composite primary keys work together to provide uniqueness.

Here is the definition of primary key and unique key constraints in a SQL table.

Primary Key

Primary Key is an attribute or a set of attributes in a table that uniquely identifies a record (row) in a table and no two records (rows) in the table can have the same values for all the columns comprising the primary key.

A primary key can consist of one or more columns of a table. When two or more columns are used as a primary key, they are called a composite key. Each single column's data can be duplicated but the combination values of these columns cannot be duplicated.

  • A primary key is a column or a set of columns in a table that uniquely identifies each record.
  • It must have a unique value for each row, and it cannot contain NULL values.
  • There can be only one primary key in a table.
  • It is used to establish relationships between tables, often as a foreign key in other tables.
  • Primary keys are automatically indexed for efficient retrieval.

Unique Key

A unique key is a key that stores unique values (no duplicates) for that particular column, and it accepts a null value and multiple null values do not violate the unique constraints.

  • A unique key is a constraint that ensures the values in a column or a set of columns are unique across the entire table.
  • Unlike a primary key, a unique key can allow NULL values, but if a column is defined as NOT NULL, the unique constraint enforces uniqueness with respect to non-NULL values.
  • There can be multiple unique keys in a table.
  • Unique keys are used to enforce the integrity of the data by preventing duplicate values in specific columns.

Differences between Primary Key and Unique Key

  • A Unique key can store a null value and a primary key cannot store any null value.
  • A primary key can be referenced to another table as a Foreign Key.
  • A table can have only one primary key, but have multiple unique keys.
  • Primary is not always a single-column key, and may consist of multiple columns to make a composite key.

Conclusion

Both primary keys and unique keys ensure uniqueness, but the primary key is more restrictive as it also serves as the main identifier for records in a table and is often used in relationships with other tables. Unique keys, on the other hand, focus solely on ensuring uniqueness within the specified columns without necessarily being the main identifier for the records.

Share this post

Comments (0)

    No comment

Leave a comment

All comments are moderated. Spammy and bot submitted comments are deleted. Please submit the comments that are helpful to others, and we'll approve your comments. A comment that includes outbound link will only be approved if the content is relevant to the topic, and has some value to our readers.


Login To Post Comment