SQL Unique Constraints


Prev Tutorial Next Tutorial

SQL Unique Constraints

Unique Constraints does not allow duplicate values. You can apply unique constraints on more than one column in same table. You can apply unique constraints on more than one column in same table.

Advantage of Unique Constraints

This constraints allows to enter null value.

unique key

MySQL

	
create table employee
(
e_id number(3),
e_name varchar(15),
sal number(5),
unique e_id)
);

SQL Server / Oracle / MS Access

	
create table employee
(
e_id number(3) unique,
e_name varchar(15),
sal number(5)
);

Note: If you avoid the unique constraints property then it gives the following error messages "unique constraints scott sys-avoided"

SQL UNIQUE KEY constraint on ALTER TABLE:

This is the way to add unique key constraint on any column of table which table is already created, use following syntax;

MySQL / SQL Server / Oracle / MS Access:

	
ALTER TABLE employee
ADD UNIQUE (E_Id)

Naming of a UNIQUE constraint and apply on multiple column

To give name of unique key constraint and it apply on multiple column use given syntax;

MySQL / SQL Server / Oracle / MS Access

	
ALTER TABLE employee
ADD CONSTRAINT uc_EmployeeID UNIQUE (E_Id,LastName)

Drop Unique constraint

MySQL

	
ALTER TABLE employee
DROP INDEX uc_employeeID

SQL Server / Oracle / MS Access:

	
ALTER TABLE employee
DROP CONSTRAINT uc_employeeID

Prev Tutorial Next Tutorial

Advertisements




Facebook Likes

Advertisements