check约束的增删操作
以MySQL为例:
添加语法:
alter table table_name add constraint check_name check(约束条件);
删除语法:
alter table student drop check check_name;
以上两条语句可对于已经建成的表进行check约束的添加或删除
对于表:
create table student(
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
primary key (ID),
foreign key (dept_name) references department(dept_name) on delete set null
);
对 tot_cred添加check:
alter table student add constraint student_chk_1 check(tot_cred >= 0);
删除:
alter table student drop check student_chk_1;
·
·
·
·
对于在表内直接添加check约束:
Case 1:
create table student(
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0) check(tot_cred >= 0),
primary key (ID),
foreign key (dept_name) references department(dept_name) on delete set null
);
对于这种属性后面直接跟check的,其名称是mysql自动生成的。
生成规则是:tablename_chk_1、tablename_chk_2……
目前在存储在information_schema库中的check_constraint表中
Case 2:
constraint tot_cred_chk check (tot_cred >= 0),
create table student(
ID varchar(5),
name varchar(20) not null,
dept_name varchar(20),
tot_cred numeric(3,0),
constraint tot_cred_chk check (tot_cred >= 0),
primary key (ID),
foreign key (dept_name) references department(dept_name) on delete set null
);
这种方式便于我们自定义check约束的名称。
·
·
·
·
tips:
貌似目前没有相关的直接修改check约束的操作,如果有请各位大佬告与我!!!