Skip to main content

SQL Unique Constraints

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

Comments

Popular posts from this blog

Introduction of SQL

Introduction of SQL SQL (Structure Query Language)  was initially developed at IBM by Donald D. Chamberlin and Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL (Structured English Query Language), was designed to manipulate and retrieve data stored in IBM's original quasi-relational database management system. Features of SQL SQL is not a case sensitive language. Every commands in SQL should ends with semicolon(;). SQL can also called as sequel (SEQUEL). SQL Sub Language SQL is mainly divided into four sub language Data Definition Language(DDL) Data Manipulation Language(DML) Transaction Control Language(TCL) Data Control Language(DCL) Only for remember You can remember all these command like below; DDL Commands "dr. cat" d-Drop, r-Rename, c-Create, a-Alter, t-Truncate. DML commands "sudi". s-Select, u-Update, d-Delete, i-Insert.

SQL Syntax

SQL Syntax SQL follows some unique set of rules and guidelines called  Syntax . Some basic guidelines related to SQL Syntax are given below; SQL is not case sensitive. Commonly SQL keywords are written in uppercase. You can write SQL statements in one line or in multiple lines. SQL are depends on relational algebra and tuple are relational calculus. SQL statement SQL statements are started with any of the SQL commands/keywords like SELECT, INSERT, UPDATE, DELETE, ALTER, DROP etc. and the statement ends with a semicolon (;). Syntax SELECT * FROM table_name; Why use semicolon after SQL statements ? Semicolon is used to separate SQL statements. It is a standard way to separate SQL statements in a database system in which more than one SQL statements are used in the same call. Some Basic Commands SELECT:  It is used for Retrive data from a database. UPDATE:  It is used for updates data in database. DELETE:  It is used for deletes data from da...

DDL Commands in SQL

DDL Commands in SQL Data Definition Language(DML)  statements are used to define data structures in database. DDL Commands All DDL command are given below; Drop Create Alter Rename Truncate All DML Command I will discuss in detail later.