PRIMARY KEY
The PRIMARY KEY
constraint is a combination of the NOT NULL
and UNIQUE
constraints. It ensures that a column cannot have a NULL
value and that all values in the column are unique. The PRIMARY KEY
constraint is often used to uniquely identify each row in a table. For example, consider a table products
with a column product_id
that should be unique and not NULL
. The PRIMARY KEY
constraint can be applied as follows:
sql
CREATE TABLE products (
product_id INT PRIMARY KEY,
product_name VARCHAR(50),
price DECIMAL(10, 2)
);
In this case, the product_id
column must have a unique value for every row in the products
table, and it cannot be NULL
.
name | type |
product_id | INT |
product_name | VARCHAR(50) |
price | DECIMAL(10, 2) |
In the table above, each row has a unique value for the product_id
column, as required by the PRIMARY KEY
constraint.