Unique constraints in SQL Server
This tutorial explains how to create, add and delete unique constraints in SQL Server.
This tutorial explains how to create, add and delete unique constraints in SQL Server.
What is the only constraint in SQL Server?
The only constraint is a field or combination of data fields, uniquely identifying a record. Some fields may contain NULL values, as long as the combination of values is unique.
Difference between unique constraint and primary key
Primary Key Primary Key Unique Unique Constraint constraint No field in the primary key is allowed to contain NULL values. Some fields of unique constraints may contain NULL values, provided that the combination of values is unique.Create a unique constraint with the CREATE TABLE command
CREATE TABLE ten_bang
(
cot1 kieudulieu [ NULL | NOT NULL ];
cot2 kieudulieu [ NULL | NOT NULL ];
…
CONSTRAINT ten_rangbuoc UNIQUE (cot1_rb, cot2_rb, … cot_n_rb)
);
ten_bang
The name of the table you want to create
cot1, cot2
The column you want to create in the table
ten_rangbuoc
Name of unique binding
cot1_rb, cot2_rb, . cot_n_rb
The columns make up the unique constraint.
Data types in SQL Server
For example
CREATE TABLE nhanvien
( id_nhanvien INT PRIMARY KEY,
so_nhanvien INT NOT NULL,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50),
luong MONEY,
CONSTRAINT nhanvien_duynhat UNIQUE (so_nhanvien)
);
In this example, we have created a unique constraint named nhanvien_duynhat on the table created by the CREATE TABLE command, including a single field so_nhanvien.
Can create unique constraints with more than 1 field as in the example below.
CREATE TABLE nhanvien
( id_nhanvien INT PRIMARY KEY,
so_nhanvien INT NOT NULL,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50),
luong MONEY,
CONSTRAINT nhanvien_duynhat UNIQUE (ho, ten)
);
Create a unique constraint with the ALTER TABLE command
ALTER TABLE ten_bang
ADD CON
STRAINT ten_rangbuoc UNIQUE (cot1, cot2, . cot_n);
ten_bang
The name of the table you want to edit. This is the table you want to add unique constraints.
ten_rangbuoc
The name of the unique constraint that you want to create.
cot1, cot2 . cot_n
The columns make up the unique constraint.
For example
ALTER TABLE nhanvien
ADD CONSTR
AINT nhanvien_duynhat UNIQUE (so_nhanvien);
The above example uses the ALTER TABLE command to create a unique constraint on an existing table, called nhanvien, called so_nhanvien field. To create constraints with more than 1 field, see the example below.
ALTER TABLE nhanvien
ADD CONSTRAINT ten
_nhanvien_duynhat UNIQUE (cough, ten);
Delete unique binding
Syntax
ALTER TABLE ten_bang
DROP CONSTRAINT
ten_rangbuoc;
ten_bang
The name of the table you want to edit. This is the table you want to delete the unique constraint.
ten_rangbuoc
The name of the unique constraint that you want to delete.
For example
ALTER TABLE nhanvien
DROP CONST
RAINT nhanvien_duynhat;
The example above removes the unique constraint named nhanvien_duynhat on the table.
Previous post: Activate foreign key in SQL Server
Lesson: Check constraints in SQL Server
You should read it
- Check constraints in SQL Server
- Constraints in SQL
- What is MAC-Binding? How does it work?
- Foreign Key with Cascade Delete in SQL Server
- The best SFTP server and tools for secure file transfer
- How to Think of a Username
- What is Unique Content & Why it's Crucial for Your Website?
- Everything you need to know about data binding in Vue
- Delete the foreign key in SQL Server
- How to use the UNIQUE function in Google Sheets
- How to start and stop services in MS SQL Server
- SQL Server 2019 - Microsoft Relational Database Management System
Maybe you are interested
5 quietest keyboards When you encounter failure, remember these sayings Close-up of the world's largest excavator super monster, weighing up to 13,500 tons Invite to download the space theme wallpapers on the NASA XS inspired by NASA 5 types of hair that are both beautiful and suitable for going to play with Noel but she always ends up If we bring a part of the Sun to Earth, what terrible thing will happen?