What is the difference between primary key and unique key?
| Point | Primary key | Unique key |
|---|---|---|
| NULL allowed | No | Yes (it allows NULL values) |
| How many per table | One | Many |
| Purpose | Identifies each row | Stops duplicate values in a column, like email |
CREATE TABLE users (
id INT PRIMARY KEY,
email VARCHAR(100) UNIQUE
);
See MySQL Constraints.
Learn more about the similar topics:
| Tutorials |
|---|
| MySQL Data Types |
| MySQL CREATE DATABASE |
| MySQL USE and SHOW DATABASES |
| MySQL DROP DATABASE |
| MySQL CREATE TABLE |
| MySQL Constraints |
| MySQL DROP TABLE |
| Exercises & Assignments |
|---|
| Write a query to get all column names of a database table |
| Write a query to create a new table with the data and structure of another table |