SQL UNIQUE Constraint

Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

11/03/2016 SQL UNIQUE Constraint

w3schools.com
THE WORLD'S LARGEST WEB DEVELOPER SITE

☰  SQL  

SQL UNIQUE Constraint


« Previous Next Chapter »

SQL UNIQUE Constraint


The UNIQUE constraint uniquely identifies each record in a database table.

The UNIQUE and PRIMARY KEY constraints both provide a guarantee for uniqueness for a
column or set of columns.

A PRIMARY KEY constraint automatically has a UNIQUE constraint defined on it.

Note that you can have many UNIQUE constraints per table, but only one PRIMARY KEY
constraint per table.

SQL UNIQUE Constraint on CREATE TABLE


The following SQL creates a UNIQUE constraint on the "P_Id" column when the
"Persons" table is created:

SQL Server / Oracle / MS Access:

CREATE TABLE Persons


(
P_Id int NOT NULL UNIQUE,
http://www.w3schools.com/sql/sql_unique.asp 1/7
11/03/2016 SQL UNIQUE Constraint

LastName varchar(255) NOT NULL,


FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

MySQL:

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (P_Id)
)

To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on


multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons


(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)
)

SQL UNIQUE Constraint on ALTER TABLE


To create a UNIQUE constraint on the "P_Id" column when the table is already created,
use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

http://www.w3schools.com/sql/sql_unique.asp 2/7
11/03/2016 SQL UNIQUE Constraint

ALTER TABLE Persons


ADD UNIQUE (P_Id)

To allow naming of a UNIQUE constraint, and for defining a UNIQUE constraint on


multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons


ADD CONSTRAINT uc_PersonID UNIQUE (P_Id,LastName)

To DROP a UNIQUE Constraint


To drop a UNIQUE constraint, use the following SQL:

MySQL:

ALTER TABLE Persons


DROP INDEX uc_PersonID

SQL Server / Oracle / MS Access:

ALTER TABLE Persons


DROP CONSTRAINT uc_PersonID

« Previous Next Chapter »

http://www.w3schools.com/sql/sql_unique.asp 3/7
11/03/2016 SQL UNIQUE Constraint

W3SCHOOLS EXAMS
HTML, CSS, JavaScript, PHP, jQuery, and XML Certifications

COLOR PICKER

SHARE THIS PAGE


http://www.w3schools.com/sql/sql_unique.asp
   4/7
11/03/2016 SQL UNIQUE Constraint

  


LEARN MORE:

Color Converter
Google Maps
Animated Buttons
Modal Boxes
Modal Images
Tooltips
Loaders
JS Animations
Progress Bars
Dropdowns
Slideshow
Side Navigation
HTML Includes
Color Palettes

All­In­One Google Suite


Gmail, Drive, Calendar, Hangouts & More. Built For
Work. Learn More!

http://www.w3schools.com/sql/sql_unique.asp 5/7
11/03/2016 SQL UNIQUE Constraint

REPORT ERROR
PRINT PAGE
FORUM
ABOUT

Top 10 Tutorials
HTML Tutorial
CSS Tutorial
JavaScript Tutorial
SQL Tutorial
PHP Tutorial
jQuery Tutorial
Bootstrap Tutorial
Angular Tutorial
ASP.NET Tutorial
XML Tutorial

Top 10 References
HTML Reference
CSS Reference
JavaScript Reference
Browser Statistics
HTML DOM
PHP Reference
jQuery Reference
HTML Colors
HTML Character Sets
AngularJS Reference

http://www.w3schools.com/sql/sql_unique.asp 6/7
11/03/2016 SQL UNIQUE Constraint

Top 10 Examples
HTML Examples
CSS Examples
JavaScript Examples
HTML DOM Examples
PHP Examples
jQuery Examples
XML Examples
ASP Examples
SVG Examples

Web Certificates
HTML Certificate
HTML5 Certificate
CSS Certificate
JavaScript Certificate
jQuery Certificate
PHP Certificate
Bootstrap Certificate
XML Certificate

W3Schools is optimized for learning, testing, and training. Examples might be simplified to improve reading
and basic understanding. Tutorials, references, and examples are constantly reviewed to avoid errors, but we
cannot warrant full correctness of all content. While using this site, you agree to have read and accepted our
terms of use, cookie and privacy policy. Copyright 1999­2016 by Refsnes Data. All Rights Reserved.

http://www.w3schools.com/sql/sql_unique.asp 7/7

You might also like