MySQL unique key vs primary key constraints

MySQL unique and primary keys serve to identify one and only one row in a table. However, there are some differences that we are going to look at in this tutorial. All the conclusions were drawn using MySQL 5.0.67 server.

MySQL unique keys can be NULL whereas primary keys cannot be:

mysql> CREATE TABLE uni_pri (id_primary INT PRIMARY KEY NULL DEFAULT NULL, id_unique INT NULL UNIQUE KEY DEFAULT NULL);
Query OK, 0 rows affected (0.06 sec)

mysql> SHOW CREATE TABLE uni_pri;
+---------+--------------------------+
| Table   | Create Table             
+---------+--------------------------+
| uni_pri | CREATE TABLE "uni_pri" (
  "id_primary" int(11) NOT NULL default '0',
  "id_unique" int(11) default NULL,
  PRIMARY KEY  ("id_primary"),
  UNIQUE KEY "id_unique" ("id_unique")
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+---------+--------------------------+
1 row in set (0.00 sec)

mysql> INSERT INTO uni_pri (id_primary, id_unique) VALUES (NULL, NULL);
ERROR 1048 (23000): Column 'id_primary' cannot be null
mysql> INSERT INTO uni_pri (id_primary, id_unique) VALUES (1, NULL);
Query OK, 1 row affected (0.00 sec)
mysql> INSERT INTO uni_pri (id_primary, id_unique) VALUES (2, NULL);
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM uni_pri;
+------------+-----------+
| id_primary | id_unique |
+------------+-----------+
|          1 |      NULL |
|          2 |      NULL |
+------------+-----------+
2 rows in set (0.00 sec)

There can be only one Primary key in a table but one or more unique keys:

mysql> CREATE TABLE uni_pri (id_primary1 INT PRIMARY KEY, id_primary2 INT PRIMARY KEY, 
id_unique1 INT NULL UNIQUE KEY, id_unique2 INT NULL UNIQUE KEY);
ERROR 1068 (42000): Multiple primary key defined
mysql> CREATE TABLE uni_pri (id_primary1 INT PRIMARY KEY, id_unique1 INT NULL UNIQUE KEY, id_unique2 INT NULL UNIQUE KEY);
Query OK, 0 rows affected (0.05 sec)

mysql> SHOW CREATE TABLE uni_pri;
+---------+--------------------------+
| Table   | Create Table             
+---------+--------------------------+
| uni_pri | CREATE TABLE "uni_pri" (
  "id_primary1" int(11) NOT NULL,
  "id_unique1" int(11) default NULL,
  "id_unique2" int(11) default NULL,
  PRIMARY KEY  ("id_primary1"),
  UNIQUE KEY "id_unique1" ("id_unique1"),
  UNIQUE KEY "id_unique2" ("id_unique2")
) ENGINE=MyISAM DEFAULT CHARSET=latin1 |
+---------+--------------------------+
1 row in set (0.00 sec)

Please feel free to use the comments form below if you have any questions or need more explanation on anything. I recommend thoroughy testing on a production-like test system first before moving to production. Use the icons below to share this tutorial with your friends.

tags cloud

mysql primary-key myisam mysql-unique-key

popular searches

Comments (write a comment):

Thanks for Post & give me knowledge about this Posted by: vinay Pandit on Jan 05, 2012

In InnoDB, there are huge differences between the PRIMARY KEY and a UNIQUE key. Performance differences, implementation differences, etc.

Many of the gory details are described here:
http://mysql.rjweb.org/doc.php/myisam2innodb

(Sorry if this is supposed to be a MySQL-only web site. The world is moving away from MyISAM toward InnoDB.) Posted by: Rick James on Feb 27, 2012

You state that you can only have one primary key. But how come i can create a table like:
CREATE TABLE `myTable` (
`ID1` INT UNSIGNED NOT NULL,
`ID2` INT UNSIGNED NOT NULL,
`value` INT NOT NULL,
PRIMARY KEY(ID1, ID2)
);

Wouldn't that be considered having 2 primary keys? Posted by: Tim M on Apr 04, 2012

Tim, in your create table statement you're creating one primary key with two columns in it. In that case, ID1 (or ID2) could have repeated values but each row must have a unique combination of (ID1, ID2). Posted by: Matthew on May 31, 2012

What is the difference between:

PRIMARY KEY(ID1, ID2)
and
CONSTRAINT IDs PRIMARY KEY(ID1, ID2)

Just wondering... Posted by: Jafet on Sep 13, 2012

leave a comment