网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

怎么理解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外键约束
网站URL:http://bjjierui.cn/article/pjdhsc.html

其他资讯