CREATE TABLE Person.EmployeeEducationType
(
EmployeeEducationTypeID int NOT NULL PRIMARYKEY,
EmployeeID int NOT NULL,
EducationTypeID int NULL,
GPA numeric(4,3) NOT NULL CHECK(GPA>2.5 AND GPA<=4.0)
)
-----------在列上直接定义CHECK约束
CREATE TABLE Person.EmployeeEducationType
(
EmployeeEducationTypeID int NOT NULL PRIMARYKEY,
EmployeeID int NOT NULL,
EducationTypeID int NULL,
GPA numeric(4,3) NOT NULL,
CONSTRAINT CK_EmployeeEducationType
CHECK(EducationTypeID>1 ANDGPA>2.5 AND GPA<=4.0)
)
-----------在表上定义约束,表达式中允许有多个列条件综合
ALTER TABLE Person.ContactType WITH NOCHECK
ADD CONSTRAINT CK_ContackType
CHECK(Name NOT LIKE '%assistant%')
-------为现有表添加CHECK约束,如果这几行代码没有带WITH NOCHECK就会失败,
------因为表中已经存在具有'assistant'行,增加WITH NOCHECK意味着现有值会被忽略,只有新值才会检验