codescracker


sql

SQL Unique



« Previous Tutorial Next Tutorial »


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

Important - You can use many UNIQUE constraints per table, but use only one PRIMARY KEY constraint per table

SQL UNIQUE Constraint on CREATE TABLE

Here are SQL statements are used to create SQL constraint on the column "Student_ID" when the table "Students" is created.

For SQL Server / Oracle / MS Access Databases

Here is an example of SQL UNIQUE constraint on CREATE TABLE for SQL Server/Oracle/Ms Access Databases:

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

For MySQL Database

Here is an example of SQL UNIQUE constraint on CREATE TABLE for MySQL Databases:

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

SQL UNIQUE Constraint on ALTER TABLE

Here are SQL statements used to create UNIQUE constraint on the column "Student_ID" when the table is already created:

For MySQL / SQL Server / Oracle / MS Access Databases

Here is an example of SQL UNIQUE constraint on ALTER TABLE for MySQL/SQL Server/Oracle/Ms Access Databases:

ALTER TABLE Students
ADD UNIQUE (Student_ID)

DROP SQL UNIQUE Constraint

Here are the SQL statements used to drop a UNIQUE constraint:

For MySQL Database

ALTER TABLE Students
DROP INDEX uc_StdID

For SQL Server / Oracle / MS Access Database

ALTER TABLE Students
DROP CONSTRAINT uc_StdID

« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test