oracle外键约束的总结

外键约束的创建方法
tes1的建表语句为create table test1 (hid number primary key,hname varchar2(10));
1、创建表的同时创建外键约束

1.1、列级别
create table test2 (hid1 number(10) REFERENCES test1(hid),hname1 varchar2(10));--系统自动生成约束名
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
1.2、表级别
create table test2 (hid1 number(10) ,hname1 varchar2(10),foreign key (hid1) REFERENCES test1(hid));--系统自动生成约束名
create table test2 (hid1 number(10) ,hname1 varchar2(10),constraint hid_pk foreign key (hid1) REFERENCES test1(hid));

2、表创建后再创建外键约束
ALTER TABLE test2 ADD  FOREIGN KEY (hid1)  REFERENCES test1 (hid);--系统自动生成约束名
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1)  REFERENCES test1 (hid);



子表操作会遇到的报错
不能修改值为父表不存在的记录
不能插入父表不存在的记录
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
insert into test2 values(1,'100');
update test2 set hid1=2 where hid1=1;--报错ORA-02291: 违反完整约束条件 (HR.HID_PK) - 未找到父项关键字
insert into test2 values(2,'100');--报错ORA-02291: 违反完整约束条件 (HR.HID_PK) - 未找到父项关键字
drop table test2;
drop table test1;


父表操作遇到的报错
create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
insert into test2 values(1,'100');
delete from test1;--报错ORA-02292: 违反完整约束条件 (HR.HID_PK) - 已找到子记录
truncate table test1;--报错ORA-02266: 表中的唯一/主键被启用的外键引用
drop table test1;--报错ORA-02449: 表中的唯一/主键被外键引用
update test1 set hid=2 where hid=1;--报错ORA-02292: 违反完整约束条件 (HR.HID_PK) - 已找到子记录

create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
insert into test1 values(1,'1');
truncate table test1;
drop table test1;--报错ORA-02266: 表中的唯一/主键被启用的外键引用

create table test1 (hid number primary key,hname varchar2(10));
create table test2 (hid1 number(10) constraint hid_pk REFERENCES test1(hid),hname1 varchar2(10));
drop table test1;--报错ORA-02266: 表中的唯一/主键被启用的外键引用


delete报错的解决方法
解决方法1
delete from test2;
delete from test1;

解决方法2(不保留子表记录)
alter table test2 drop constraint hid_pk;
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1)  REFERENCES test1 (hid) ON DELETE CASCADE;
delete from test1;

解决方法3(保留子表记录,但是字表对应字段值变成null,如下test2的hid1为null了)
alter table test2 drop constraint hid_pk;
ALTER TABLE test2 ADD CONSTRAINT hid_pk FOREIGN KEY (hid1)  REFERENCES test1 (hid) ON DELETE SET NULL;
delete from test1;


truncate报错的解决方法
drop table test2;
truncate table test1;

alter table test1 disable primary key cascade;
truncate table test1;

alter table test1 disable primary key cascade;
truncate table test2;
truncate table test1;

采用如下一样会报错
truncate table test2;
truncate table test1;--继续报错ORA-02266: 表中的唯一/主键被启用的外键引用


drop报错的解决方法
drop table test1 cascade constraints;

drop table test2;
drop table test1;

采用如下一样会报错
alter table test1 disable primary key cascade;
truncate table test2;
drop table test1;

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/30126024/viewspace-2152987/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/30126024/viewspace-2152987/

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值