Check constraints in SQL Server

What is the check constraint in SQL Server, what is it used for and what is it used for? This article will give you the answer.

What is Check Constraint check constraint in SQL Server?

The check constraint in SQL Server (Transact-SQL) allows defining conditions for each row in the table.

Note

  1. Test constraints cannot be defined in SQL View.
  2. The check constraint in the table must refer to the column in that table, unable to reference the column in another table.
  3. Check constraints cannot use Subquery subqueries.
  4. Test constraints can be defined by the CREATE TABLE or ALTER TABLE command.

Create check constraints with the CREATE TABLE command

Syntax

 CREATE TABLE ten _bang 
(
cot1 kieudulieu [ NULL | NOT NULL ],
cot2 kieudulieu [ NULL | NOT NULL ],


CONSTRAINT ten_rangbuoc
CHECK [ NOT FOR REPLICATION ] (dieu_kien ten_cot)
);

Data types in SQL Server

ten_bang

The name of the table that wants to create check constraints.

ten_rangbuoc

The name you want to set for check binding.

ten_cot

The table column that the check constraint applies.

condition

Conditions must meet.

For example

 CREA TE TABLE nhanvien 
(id_nhanvien INT NOT NULL,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50),
luong MONEY,
CONSTRAINT id_nhanvien_kiemtra
CHECK (id_nhanvien BETWEEN 1 AND 10000)
);

In this example, the CREATE TABLE statement creates a check constraint named id_nhanvien_kiemtra in the table. This constraint will ensure that the id_nhanvien information field contains a value between 1 and 10000.

This is another example.

 CRE ATE TABLE nhanvien 
(id_nhanvien INT NOT NULL,
ho VARCHAR(50) NOT NULL,
ten VARCHAR(50),
luong MONEY,
CONSTRAINT luong_kiemtra
CHECK (luong > 0)
);

This example creates a constraint that checks the checksum in the table, ensuring that the salary will be greater than zero.

Create check constraints with the ALTER TABLE command

Syntax

 ALTER TABLE ten_bang 
ADD CONSTAINT ten_rangbuoc
CHECK (d ieu_kien ten_cot);

ten_bang

The name of the table wants to add check constraints.

ten_rangbuoc

Name set for check binding.

ten_cot

Columns in the table that check constraints apply.

condition

Conditions that check constraints must meet.

For example

This is an example of using the ALTER TABLE command to create check constraints in SQL Server.

 ALTER TABLE  nhanvien 
ADD CONSTRAINT ho_kiemtra
CHECK (ho IN ('S mith', 'Anderson', 'Jonas'));

The check constraint ho_kiemtra is created on the existing table, ensuring that the employee's surname will contain only values ​​for Smith, Anderson or Jonas.

Delete check constraints

Syntax

 ALTE R TABLE ten_bang 
DROP CONSTRAINT ten_rangbuoc

ten_bang

The name of the table to delete the check constraint.

ten_rangbuoc

Check binding name wants to delete.

For example

 ALTE R TABLE nhanvien 
DROP CONSTRAINT ho_kiemtra;

This command will delete the constraint ho_kiemtra on the table.

Enable check binding

Syntax

 ALTER TA BLE ten_bang 
WITH CHEC K CHECK CONSTRAINT ten_rangbuoc;

ten_bang

The name of the table to re-enable the check constraint.

ten_rangbuoc

The name of the check constraint needs to be activated.

For example

 ALTER TA BLE nhanvien 
WITH CHEC K CHECK CONSTRAINT luong_kiemtra;

This example re-activates the check constraint on the table in the table.

Disable check binding

Syntax

 ALTER TA BLE ten_bang 
NOCHECK CONSTRAINT ten_rangbuoc;

ten_bang

The name of the table to disable check constraints.

ten_rangbuoc

The name of the check constraint wants to be disabled.

For example

 ALTER T ABLE nhanvien 
NOCHECK CONSTRAINT luong_kiemtra;

This example disables the check constraint on luong_kiemtra in the table.

Previous article: Unique binding in SQL Server

The following article: Index in SQL Server

4 ★ | 1 Vote

May be interested

  • Comparison operators in SQL ServerComparison operators in SQL Server
    this article will show the comparison operators that are used to check for equilibrium along with more advanced operators in sql server.
  • How to Check PHP VersionHow to Check PHP Version
    if you're interested in implementing new features on your website, or are trying to pinpoint a bug, you may need to check the version of php that your server is currently running. you can check the version by running a simple php file on...
  • Network basics: Part 3 - DNS ServerNetwork basics: Part 3 - DNS Server
    a dns server is a server that contains a database of public ip addresses and hostnames associated with them. in most cases, the dns server is used to resolve or translate those common names into ip addresses as required.
  • Test SQL Server with Windows PowerShell - Part 5Test SQL Server with Windows PowerShell - Part 5
    in this part 5, we will check whether we can connect to sql server and see if we can query some properties related to sql server.
  • How to check if email is still working?How to check if email is still working?
    there is a tool that helps you check if your email is still active or not, which is reacher. reacher connects directly to the email server, so the check is very fast and accurate.
  • ALTER TABLE statement in SQLALTER TABLE statement in SQL
    the alter table statement in sql is used to add, delete, and modify columns in an existing table.
  • How to set up your own Git server on LinuxHow to set up your own Git server on Linux
    while you can count on globally renowned git hosting services like github, in some cases it is better to host a personal git server for enhanced privacy, customizability, and security.
  • Use IIS to set up FTP Server on WindowsUse IIS to set up FTP Server on Windows
    set up an ftp server (file transfer protocol server) to share and convert large files with unlimited traffic.
  • How to change DNS server on the most popular routersHow to change DNS server on the most popular routers
    changing the dns server settings on your router is not difficult, but every manufacturer uses their own custom interface, which means the process can be very different depending on which router you are owned.
  • Test SQL Server with Windows PowerShell - Part 6Test SQL Server with Windows PowerShell - Part 6
    part 6 will show you how to check all existing databases in the sql server instance and query the database properties.