资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

怎么理解oracle外键约束

这期内容当中小编将会给大家带来有关怎么理解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;

上述就是小编为大家分享的怎么理解oracle外键约束了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注创新互联行业资讯频道。


网站栏目:怎么理解oracle外键约束
当前地址:http://cdkjz.cn/article/pjdhsc.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220