In a database, the primary key uniquely identifies each record in a table.
A table can have only one primary key; and in the table, this primary key can consist of single or multiple columns (fields).
Important
Primary keys cannot contain NULL values.
CREATE TABLE Person (
ID int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
PRIMARY KEY (ID)
);We are creating a Person table where the primary key is the ID. We can identify a person with its ID, since we know there won’t be any other row with the same ID.
Note
If we try to add a new row with the same primary key as an already existing row, we will get an error from the DBMS.
CREATE TABLE Persons (
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Age int,
CONSTRAINT PK_Person PRIMARY KEY (FirstName, LastName)
);If we assume that the database won’t ever contains people with the same first and last name, we can make the tuple (FirstName, LastName) a primary key.
databases resources: