PRIMARY KEY

Description

The PRIMARY KEY constraint uniquely identifies each record in a table.

Primary keys must contain UNIQUE values, and cannot contain NULL values.

A table can have only ONE primary key; and in the table, this primary key can consist of single or multiple columns (fields).

SQL PRIMARY KEY on CREATE TABLE

The following SQL creates a PRIMARY KEY on the “ID” column when the “Persons” table is created:

  1. > CREATE TABLE Persons (
  2. ID int NOT NULL,
  3. LastName varchar(255) NOT NULL,
  4. FirstName varchar(255),
  5. Age int,
  6. PRIMARY KEY (ID)
  7. );

To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:

  1. > CREATE TABLE Persons (
  2. ID int NOT NULL,
  3. LastName varchar(255) NOT NULL,
  4. FirstName varchar(255),
  5. Age int,
  6. CONSTRAINT PK_Person PRIMARY KEY (ID,LastName)
  7. );

Note: In the example above there is only ONE PRIMARY KEY (PK_Person). However, the VALUE of the primary key is made up of TWO COLUMNS (ID + LastName).

Constraints

DROP PRIMARY KEY is not supported yet.