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

定制建站费用3500元

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

成都品牌网站建设

品牌网站建设费用6000元

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

成都商城网站建设

商城网站建设费用8000元

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

成都微信网站建设

手机微信网站建站3000元

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

建站知识

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

oracle如何修改外键,Oracle设置外键

oracle 外键如何更新

用scott用户打开两个窗口

创新互联是一家专业提供城关企业网站建设,专注与成都网站制作、成都做网站、外贸营销网站建设H5场景定制、小程序制作等业务。10年已为城关众多企业、政府机构等服务。创新互联专业网站设计公司优惠进行中。

1、外键无索引时,子表更新外键未提交,主表更新非子表引用的主键时被阻塞

会话1:

create table t1 (x int primary key);

insert into t1 values(1);

insert into t1 values(2);

insert into t1 values(3);

commit;

create table t2(y int references t1);

insert into t2 values(1);

commit;

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=3; //会话被阻塞

2、外键有索引时,子表更新外键未提交,主表更新非子表引用的主键时不会被阻塞

会话1:

create index t2_index on t2(y) ; //创建外键索引

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=3;

已更新 1 行;//可以正常更新

3、外键有无索引,对于子表更新外键未提交,主表更新相对应的主键无影响,更新主键的session都会被阻塞

会话1:

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=1; //更新子表已引用的

会话被阻塞。

会话1:

update t2 set y=2 where y=1;

会话2:

update t1 set x=4 where x=2 ; //更新子表将要引用的

会话被阻塞。――很好理解,主表要判断是否违反约束

二、更新子表非外键列未提交

1、外键无索引,更新主表已被外键引用的主键时,更新主键的session被阻塞

会话1:

create table t1 (x int primary key,x1 int);

insert into t1 values(1,1);

insert into t1 values(2,2);

insert into t1 values(3,3);

commit ;

create table t2(y int references t1,y1 int);

insert into t2 values(1,1);

commit ;

update t2 set y1=2 where y1=1;

会话2:

update t1 set x=4 where x=1; //更新外键引用的主键

会话被阻塞。

2、外键有索引,更新主表已被外键引用的主键时,更新主键的session不会被阻塞而报约束错误

会话1:

create index t2_index on t2(y);

update t2 set y1=2 where y1=1;

会话2:

update t1 set x=4 where x=1

*

ERROR 位于第 1 行:

ORA-02292: 违反完整约束条件 (SCOTT.SYS_C001607) - 已找到子记录日志

3、外键无索引,更新主表未被外键引用的主键时,更新主键的session被阻塞

会话1:

drop index t2_index;

update t2 set y1=2 where y1=1

会话2:

update t1 set x=4 where x=2;

会话被阻塞。

4、外键有索引,更新主表未被外键引用的主键时,更新主键的session不会被阻塞

会话1:

create index t2_index on t2(y);

update t2 set y1=2 where y1=1;

会话2:

update t1 set x=4 where x=2;

已更新 1 行。

另外在一个主表有on delete cascade,子表没有外键索引时,对主表操作会级联到子表,子表将进行全表扫描。

总结:在需要更新主键的情况下,最好是创建子表的外键索引。

oracle中 怎么设主外键?

以oracle自带的用户scott为例。

create table dept(

deptno number(2) primary key, --deptno 为 dept表的主键

dname varchar2(10),

loc varchar2(9)

);

create table emp(

empno number(4) primary key, --empno 为 emp表的主键

ename varchar2(10),

job varchar2(9),

mgr number(4),

hiredate date,

sal number(7,2),

comm number(7,2),

deptno number(2) references dept(deptno) --dept表中deptno字段 为 emp表的外键

);

oracle数据库设置外键问题

SQL drop table Stu;

drop table Stu

*

ERROR 位于第 1 行:

ORA-00942: 表或视图不存在

SQL create table Stu(

2  sno varchar2(10) primary key,

3  sname varchar2(10) not null,

4  sex varchar2(5) check(sex='男'or sex='女'),

5  class varchar2(10),

6  spasswd varchar2(10)

7  );

表已创建。

SQL drop table Cou;

drop table Cou

*

ERROR 位于第 1 行:

ORA-00942: 表或视图不存在

SQL create table Cou(

2  cno varchar2(10) primary key,

3  cname varchar2(30) not null,

4  kcxz varchar2(20),

5  lesson_hours number(4),

6  test_hours number(4),

7  credit number(3)

8  );

表已创建。

SQL drop table SC;

drop table SC

*

ERROR 位于第 1 行:

ORA-00942: 表或视图不存在

SQL create table SC(

2  sno varchar2(10),

3  cno varchar2(10),

4  grade number(3),

5  primary key(sno,cno)

6  );

表已创建。

SQL 

SQL 

SQL ALTER TABLE SC ADD CONSTRAINT fk_SC_Stu  FOREIGN KEY (sno)  REFERENCES  Stu;

表已更改。

SQL 

SQL ALTER TABLE SC ADD CONSTRAINT fk_SC_Cou  FOREIGN KEY (cno)  REFERENCES  Cou;

表已更改。

SQL


网站栏目:oracle如何修改外键,Oracle设置外键
浏览地址:http://bjjierui.cn/article/hsesih.html

其他资讯