SQL PRIMARY KEY Constraint Example
The constraint PRIMARY KEY defines a column or a set of columns to have values that uniquely identify a table row.
- The primary key cannot be a NULL.
 - No two rows can have the same primary key in a table.
 - One or more columns can be used to define a primary key, but a table can have only one primary key.
 
In the below example, the column id is defined to have NOT NULL and PRIMARY KEY.
CREATE TABLE persons (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    birth_date DATE,
    phone VARCHAR(15) NOT NULL
);
In case the table already exists, it returns an error message, which can be avoided by including the keyword IF NOT EXISTS as shown below.
CREATE TABLE IF NOT EXISTS persons (
    id INT NOT NULL PRIMARY KEY,
    name VARCHAR(30) NOT NULL,
    birth_date DATE,
    phone VARCHAR(15) NOT NULL
);
Overall
We now know when and how to use the PRIMARY KEY constraint.