The PRIMARY KEY constraint uniquely identifies each record in a database table.
Primary keys contain unique values, none of the record in the columns should be same.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.
The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Students" table is created:
CREATE TABLE Student
(
Stud_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
MySQL / SQL Server / Oracle / MS Access:
Primary keys contain unique values, none of the record in the columns should be same.
A primary key column cannot contain NULL values.
Each table should have a primary key, and each table can have only ONE primary key.
The following SQL creates a PRIMARY KEY on the "P_Id" column when the "Students" table is created:
CREATE TABLE Student
(
Stud_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
PRIMARY KEY (P_Id)
)
To allow naming of a PRIMARY KEY constraint, and for defining a PRIMARY KEY constraint on multiple columns, use the following SQL syntax:
SQL PRIMARY KEY Constraint on ALTER TABLE
To create a PRIMARY KEY constraint on the "P_Id" column when the table is already created, use the following SQL:MySQL / SQL Server / Oracle / MS Access:
ALTER TABLE Persons
ADD PRIMARY KEY (P_Id)
ADD PRIMARY KEY (P_Id)
To DROP a PRIMARY KEY Constraint
To drop a PRIMARY KEY constraint,
use the following SQL:
MySQL:
ALTER TABLE Persons
DROP PRIMARY KEY
DROP PRIMARY KEY

0 comments:
Post a Comment