Difference between Primary Key and Unique Key in MySQL

Unique data is stored in the primary key and unique key fields. The primary key field never accepts NULL value but a unique key field accepts a NULL value.

Example: Create below table

CREATE TABLE shopping.myusers(
        uid int(10) PRIMARY KEY,
        uname VARCHAR(50) UNIQUE KEY,
        password VARCHAR(50));

Now insert NULL in unique key, you won't get any error

INSERT INTO shopping.myusers(uid, uname, password) VALUES(1, NULL, '1234');

Query OK, 1 row affected (0.01 sec)

Now insert NULL in primary key, you will get error:

mysql> INSERT INTO shopping.myusers(uid, uname, password) VALUES(NULL, NULL, '1234');

ERROR 1048 (23000): Column 'uid' cannot be null