PRIMARY KEY

PRIMARY KEY uniquely identifies each row in a table.

The column defined as PRIMARY KEY must be UNIQUE (unique value) and NOT NULL (can not contain NULL).

Each table can have only one PRIMARY KEY.

Example PRIMARY KEY in MySQL

CREATE TABLE people { id int NOT NULL, name varchar (255) NOT NULL, surname varchar (255) NOT NULL, PRIMARY KEY (id) }

Example PRIMARY KEY in ORACLE, SQLSERVER, ACCESS

CREATE TABLE people { id int NOT NULL PRIMARY KEY, name varchar (255) NOT NULL, surname varchar (255) NOT NULL }

PRIMARY KEY can consist of multiple columns, for example by the columns 'id' and 'name', then is defined as

CREATE TABLE people { id int NOT NULL, name varchar (255) NOT NULL, surname varchar (255) NOT NULL, PRIMARY KEY CONSTRAINT pers (id, name) }

PRIMARY KEY can also be defined after creating the table, for that use the ALTER TABLE command

Example with ALTER TABLE PRIMARY KEY

ALTER TABLE people ADD PRIMARY KEY (id)

Example ALTER TABLE PRIMARY KEY multiple

ALTER TABLE people CONSTRAINT pers PRIMARY KEY (id, name)