2017-06-18 回答
多个表之间有参照完整性约束. check 一般是检查自己这个表的 约束, 例如 什么性别只能是 0/1, 年龄不能大于 100 之类的检查. 多表参照, 一般是使用 外键约束来实现的: 例如: -- 创建测试主表. id 是主键. create table test_main ( id int not null, value varchar(10), primary key(id) ); -- 创建测试子表. create table test_sub ( id int not null, main_id int , value varchar(10), primary key(id) ); -- 插入测试主表数据. insert into test_main(id, value) values (1, 'one'); insert into test_main(id, value) values (2, 'two'); -- 插入测试子表数据. insert into test_sub(id, main_id, value) values (1, 1, 'oneone'); insert into test_sub(id, main_id, value) values (2, 2, 'twotwo'); sql> -- 创建外键(默认选项) sql> alter table test_sub add constraint main_id_cons foreign key (main_id) references test_main; table altered. sql> sql> -- 测试删除主表数据. 将出错 ora-02292: 违反完整约束条件 sql> delete 2 test_main ...多个表之间有参照完整性约束. check 一般是检查自己这个表的 约束, 例如 什么性别只能是 0/1, 年龄不能大于 100 之类的检查. 多表参照, 一般是使用 外键约束来实现的: 例如: -- 创建测试主表. id 是主键. create table test_main ( id int not null, value varchar(10), primary key(id) ); -- 创建测试子表. create table test_sub ( id int not null, main_id int , value varchar(10), primary key(id) ); -- 插入测试主表数据. insert into test_main(id, value) values (1, 'one'); insert into test_main(id, value) values (2, 'two'); -- 插入测试子表数据. insert into test_sub(id, main_id, value) values (1, 1, 'oneone'); insert into test_sub(id, main_id, value) values (2, 2, 'twotwo'); sql> -- 创建外键(默认选项) sql> alter table test_sub add constraint main_id_cons foreign key (main_id) references test_main; table altered. sql> sql> -- 测试删除主表数据. 将出错 ora-02292: 违反完整约束条件 sql> delete 2 test_main 3 where 4 id = 1; delete * error at line 1: ora-02292: integrity constraint (hr.main_id_cons) violated - child record found