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 TABL E 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

 CRE ATE 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 TABL E 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 T ABLE 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 TABL E 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 nhanvi en 
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 TABL E 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

4 ★ | 1 Vote

May be interested

  • 7 great ideas using Raspberry Pi as a server7 great ideas using Raspberry Pi as a server
    raspberry pi is a great solution for many computer projects, from learning programming to remote control a car to building a basic stop-motion animation studio. but do you know that raspberry pi can also be used as a server? here are some ideas for using raspberry pi as a server.
  • New points in SQL Server 2017New points in SQL Server 2017
    the sql server 2017 version is primarily connected to linux, bringing the power of sql to linux. in short, you can install sql server 2017 on linux, using sql server 2017 on linux-based docker containers. sql server 2017 also allows you to choose development languages, develop it on-premise or cloud-based.
  • Instructions for setting up and managing FTP Server on Windows 10Instructions for setting up and managing FTP Server on Windows 10
    if you want to create a private cloud for sharing and converting large files without restrictions, you can create an ftp server (file transfer protocol server) on your windows 10 computer.
  • Create VPN Server on Windows 8Create VPN Server on Windows 8
    no need to install any additional applications, you can easily 'turn' your computer into a vpn server if you're using windows 8. in this way, you can share data from the computer. as a simple lan system in the form of remote access. & a
  • What is the future of server virtualization?What is the future of server virtualization?
    server virtualization can help combat poor server performance, make better use of computing capabilities, limit energy consumption and improve data center flexibility.
  • Learn about the architecture of MS SQL ServerLearn about the architecture of MS SQL Server
    in the previous articles, you already know briefly about sql server, how to install sql server on the computer. in this section we will learn about the architecture of sql server.
  • Instructions for installing MS SQL ServerInstructions for installing MS SQL Server
    this is a step by step guide to installing ms sql server.
  • How to download Free Fire OB46 Advance Server trial versionHow to download Free Fire OB46 Advance Server trial version
    the free fire ob45 advance server test version gives battle royale warriors the opportunity to experience new and unique features early. in this article, taimienphi will show you how to download and install the ob46 advance server test.
  • How to install DNS Server on Windows Server 2019How to install DNS Server on Windows Server 2019
    from microsoft, the domain name system (dns) is one of the industry standard protocol sets that includes tcp / ip, along with dns client and dns server that provide name resolution services that map names to the ip addresses of computers. .
  • Install Windows Server 2003 and create a backup serverInstall Windows Server 2003 and create a backup server
    network management documentation server 2003 as details of how to install, create a companion server in case the main server is malfunctioning, create a domain, join the client to the domain, the conection {remote desktop from the client data to server}, set adsl router parameters to server, turn off 1 working machine immediately.