PRIMARY KEY - This restriction allows you to uniquely identify each record in the table.
The primary key must contain unique values.
The primary key can not contain NULL values.
SQL PRIMARY KEY constraints in CREATE TABLE
The following SQL creates a PRIMARY KEY constraint on column "P_Id", when creating a table "Persons":
MySQL:
SQL Server / Oracle / MS Access:
MySQL / SQL Server / Oracle / MS Access:
SQL PRIMARY KEY Constraints with ALTER TABLE
MySQL / SQL Server / Oracle / MS Access:
1 2 |
ALTER TABLE Persons ADD PRIMARY KEY (P_Id) |
MySQL / SQL Server / Oracle / MS Access:
1 2 |
ALTER TABLE Persons ADD CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName) |
- Note:
Removal of PRIMARY KEY
To remove a PRIMARY KEY constraint, use the following SQL:
MySQL:
1 2 |
ALTER TABLE Persons DROP PRIMARY KEY |
SQL Server / Oracle / MS Access:
1 2 |
ALTER TABLE Persons DROP CONSTRAINT pk_PersonID |
SQL Primary Key





Comments
I really enjoyed! Quote
Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'primary'. Quote