sql check约束
Basically, CHECK constraint is used to LIMIT in columns for the range of values. We can use this constraint for single or multiple columns.
基本上, CHECK约束用于限制值范围内的列 。 我们可以将此约束用于单列或多列。
In single column, CHECK allows LIMIT on some values in a column and in multiple columns, CHECK allows LIMIT on firm columns based in other column in the row on a table.
在单列中, CHECK允许对一列中的某些值进行限制,在多列中 , CHECK允许对基于表中行中其他列的公司列进行LIMIT 。
1) For single column
1)对于单列
CREATE TABLE Conpany
(E_Id int NOT NULL CHECK (E_Id>0),E_Name varchar(255) NOT NULL,
Contact number(10),Address varchar(255),City varchar(255));
2) For multiple Columns
2)对于多列
CREATE TABLE company
(E_Id int NOT NULL,E_Name varchar(255) NOT NULL,contact number(10),
Address varchar(255),City varchar(255),
CONSTRAINT chk_emp CHECK (E_Id>0 AND City='Gwalior'));
USE of ALTER to ADD CHECK constraint in an already created table
在已创建的表中使用ALTER来添加CHECK约束
1) For single column
1)对于单列
ALTER TABLE company ADD CHECK (E_Id>0);
2) For multiple columns
2)对于多列
ALTER TABLE company ADD CONSTRAINT chk_emp CHECK (E_Id>0 AND E_name='Bharti');
How to DROP CHECK constraint from a table?
如何从表中删除检查约束?
ALTER TABLE company DROP CONSTRAINT chk_emp;
Conclusion:
结论:
In this article, we have learnt what is CHECK constraint, How to use it with example and how we can alter and drop CHECk on a table?
在本文中,我们学习了什么是CHECK约束,如何在示例中使用它以及如何在表上更改和删除CHECk?
sql check约束