(
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)
);
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);
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