SQL-W3School- High: SQL UNIQUE constraint

ylbtech-SQL-W3School- High: SQL UNIQUE constraint

 

1. Back to top
1、

SQL UNIQUE constraint

UNIQUE constraint uniquely identifies each record in the database table.

UNIQUE and PRIMARY KEY constraints are column or set of columns provides uniqueness guaranteed.

PRIMARY KEY constraint automatically has a UNIQUE definition .

Note that each table can have multiple UNIQUE constraints , but each table can have only one PRIMARY KEY constraint .

SQL UNIQUE Constraint on CREATE TABLE

The following SQL in the "Persons" table is created to create a UNIQUE constraint in the column "Id_P":

MySQL:

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
UNIQUE (Id_P)
)

SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
Id_P int NOT NULL UNIQUE,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255)
)

To allow naming of a UNIQUE constraint, as well as multiple columns define UNIQUE constraint, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

CREATE TABLE Persons
(
Id_P int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
)

SQL UNIQUE Constraint on ALTER TABLE

When the table has been created To create a UNIQUE constraint "Id_P" column, please use the following SQL:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD UNIQUE (Id_P)

To name UNIQUE constraints UNIQUE constraints and define multiple columns, use the following SQL syntax:

MySQL / SQL Server / Oracle / MS Access:

ALTER TABLE Persons
ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

Undo UNIQUE constraint

To drop a UNIQUE constraint, please use the following SQL:

MySQL:

ALTER TABLE Persons
DROP INDEX uc_PersonID

SQL Server / Oracle / MS Access:

ALTER TABLE Persons
DROP CONSTRAINT uc_PersonID
2、
2. Return to top
 
3. Back to top
 
4. Top
 
5. Top
1、
2、
 
6. Back to top
 
warn Author: ylbtech
Source: http://ylbtech.cnblogs.com/
This article belongs to the author and blog Park total, welcome to reprint, but without the author's consent declared by this section must be retained, and given the original connection in the apparent position of the article page, otherwise reserves the right to pursue legal responsibilities.

Guess you like

Origin www.cnblogs.com/storebook/p/11818374.html