SQL PRIMARY KEY Constraint

Published on by LakshmiSaahul,Dhana Royal

SQL PRIMARY KEY Constraint

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

Primary keys must contain UNIQUE values.

A primary key column cannot contain NULL values.

Most tables should have a primary key, and each table can have only ONE primary key.

SQL PRIMARY KEY Constraint on CREATE TABLE

The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Persons" table is created:

CREATE TABLE Persons
(
P_Id int NOT NULL PRIMARY KEY,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

SQL PRIMARY KEY Constraint on ALTER TABLE

ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)

DROP a PRIMARY KEY Constraint

ALTER TABLE Persons
DROP CONSTRAINT pk_PersonID

Advertising
To be informed of the latest articles, subscribe:
Comment on this post