SQL PRIMARY KEY Constraint

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



PRIMARY KEY: Definition and Usage

Every table should have a primary key. It ensures that each row is uniquely identifiable. A primary key can be a single column or multiple columns (a composite key). All values in the primary key column(s) must be unique and cannot be NULL (empty or undefined). A table can have only one primary key.

Adding PRIMARY KEY Constraints

You can add a primary key constraint when you create a table (using CREATE TABLE) or later, using ALTER TABLE. The syntax varies slightly depending on your database system (MySQL, SQL Server, PostgreSQL, etc.).

PRIMARY KEY Constraint on CREATE TABLE

This example shows adding a primary key during table creation. Note the slightly different syntax 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 a 'Persons' table with 'ID' as the primary key. 'ID' cannot be NULL 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 every row.

Adding a PRIMARY KEY After Table Creation

Use ALTER TABLE to add a primary key to an existing table. The column(s) 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 meet the uniqueness and NOT NULL requirements; 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. Uniqueness and NOT NULL are no longer enforced on the specified column(s).


**Note:** These examples assume the existence of a `Persons` table with `ID` and `LastName` columns. The precise syntax for primary key management can vary slightly between different database systems. Always consult your database system's documentation for the correct and most up-to-date usage.