SQL PRIMARY KEY Constraint

A PRIMARY KEY constraint in SQL uniquely identifies each record (row) in a database table. It's a fundamental concept in relational database design, ensuring data integrity and efficient data retrieval.



PRIMARY KEY Constraint: Definition and Usage

Primary keys are essential for managing data effectively. They guarantee that each row in a table is uniquely identifiable. A primary key can consist of a single column or multiple columns (a composite key). All values in the primary key column(s) must be unique and cannot be NULL. A table can have only one primary key.

Adding PRIMARY KEY Constraints

Primary key constraints can be defined during table creation (using CREATE TABLE) or later using ALTER TABLE. The precise syntax might differ slightly among different database systems (MySQL, SQL Server, PostgreSQL, etc.).

PRIMARY KEY Constraint on CREATE TABLE

This example shows how to add a primary key during table creation. (Note the differences between MySQL and other systems.)

Syntax (MySQL)

CREATE TABLE Persons (
  ID INT NOT NULL,
  LastName VARCHAR(255) NOT NULL,
  FirstName VARCHAR(255),
  Age INT,
  PRIMARY KEY (ID)
);
      
Syntax (SQL Server, Oracle, MS Access)

CREATE TABLE Persons (
  ID INT NOT NULL PRIMARY KEY,
  LastName VARCHAR(255) NOT NULL,
  FirstName VARCHAR(255),
  Age INT
);
      
Output

Creates the 'Persons' table with 'ID' as the primary key. 'ID' cannot contain NULL values and must be unique across all rows.

Named PRIMARY KEY Constraints and Composite Keys

You can name your primary key constraint and define it across multiple columns.

Syntax

CREATE TABLE Persons (
  ID INT NOT NULL,
  LastName VARCHAR(255) NOT NULL,
  FirstName VARCHAR(255),
  Age INT,
  CONSTRAINT PK_Person PRIMARY KEY (ID, LastName)
);
      
Output

Creates the 'Persons' table with a named primary key constraint ('PK_Person') using 'ID' and 'LastName' as a composite key. The combination of 'ID' and 'LastName' must be unique for each row.

Adding a PRIMARY KEY After Table Creation

You can add a primary key to an existing table using ALTER TABLE. The column(s) you specify must already be defined as NOT NULL.

Syntax

ALTER TABLE Persons
ADD PRIMARY KEY (ID);

--Named constraint and composite key:
ALTER TABLE Persons
ADD CONSTRAINT PK_Person PRIMARY KEY (ID, LastName);
      
Output

Adds a primary key constraint to the 'Persons' table. Existing data must adhere to the constraint; otherwise an error occurs.

Dropping PRIMARY KEY Constraints

To remove a primary key constraint, use DROP PRIMARY KEY (MySQL) or DROP CONSTRAINT (other systems).

Syntax (MySQL)

ALTER TABLE Persons
DROP PRIMARY KEY;
      
Syntax (SQL Server, Oracle, MS Access)

ALTER TABLE Persons
DROP CONSTRAINT PK_Person;
      
Output

The primary key constraint is removed. The table no longer has a primary key enforced.


**Note:** The examples assume the existence of a `Persons` table with an `ID` and `LastName` column. The specific syntax for adding and dropping primary key constraints might have slight variations depending on your database system. Always refer to your database system's documentation.