Constraints

Can someone please explain the difference between UNIQUE and PRIMARY KEY clauses? I don’t know how these clauses specify the uniqueness in different ways:slightly_smiling_face:

The difference is really one of purpose.

UNIQUE just tells SQL that this column cannot contain duplicate values.

PRIMARY KEY, however, tells SQL that the designated column(s) are the unique and and individual identifier for the record (row). Thus, PRIMARY KEY implies UNIQUE.

Primary key column(s) also cannot contain NULL values whereas you can have NULL values in a column which is simply unique.

4 Likes

Thanks for the clear explanation :slight_smile:

No worries.

It took me a while to get to that post. The difference is subtle, I think, and it can seem a little bit like there’s a difference for the sake of there being a difference, so I wanted to try and get it as clear as possible.

Still not convinced there isn’t a better explanation of the differences…

3 Likes

This is generally not true, especially in the context of the Codecademy course.

Codecademy uses SQLite in the course. SQLite, PostgreSQL, MySQL, Oracle allow multiple NULL values in the UNIQUE columns. It’s rather unpopular to allow only a single NULL value, the only example that comes to my mind is MS SQL.

5 Likes

That might’ve been what I was thinking of.

I’ll amend the answer. :slight_smile:

3 Likes

Thank you very much :slight_smile:

2 Likes

Thanks for the correction and help :slight_smile: