codescracker


sql

SQL Primary Key



« Previous Tutorial Next Tutorial »


SQL PRIMARY KEY constraint is used to identify each record in a database table uniquely.

SQL PRIMARY KEY Constraint on CREATE TABLE

Here are the SQL statements, creates a PRIMARY KEY on the column "Student_ID" when the table "Students" is created:

For MySQL Database

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

For SQL Server / Oracle / MS Access Databases

CREATE TABLE Students
(
Student_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

Here are the SQL statements used to create a PRIMARY KEY constraint on the column "Student_ID" when the table is already created/exist:

For MySQL / SQL Server / Oracle / MS Access Databases

ALTER TABLE Students
ADD PRIMARY KEY (Student_ID)

DROP SQL PRIMARY KEY Constraint

Here are the SQL statements used to drop a PRIMARY KEY constraint:

For MySQL Database

ALTER TABLE Students
DROP PRIMARY KEY

For SQL Server / Oracle / MS Access Databases

ALTER TABLE Students
DROP CONSTRAINT pk_StdID

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test