HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMajor

Differences between "Unique Key" and "Primary Key"

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
uniqueprimarydifferencesbetweenandkey

Problem

What are the major differences between Unique Key and Primary Key in MySQL?

Solution

A table can have at most one PRIMARY KEY constraint but it can have as many as you want UNIQUE KEY constraints.

Columns that are part of the PRIMARY KEY must be defined as NOT NULL. That is not required for columns that are part of UNIQUE KEY constraints. If the columns are not Nullable, then there is no difference between Unique and Primary Keys.

Another minor difference is that you can choose the name you want for a UNIQUE KEY constraint (and index). On the other hand, the PRIMARY KEY has the default name: PRIMARY.

Context

StackExchange Database Administrators Q#15572, answer score: 25

Revisions (0)

No revisions yet.