本帖最后由 Yong Huang 于 2013-9-30 09:31 编辑

An index on the child table foreign key column is to prevent locking the parent table. To quote "Pro Oracle Database 12c Administration" by Darl Khun:

"Without a regular B-tree index on the foreign key column in the child table, any time you attempt to insert or delete in the child table, a table-wide lock is placed on the parent table; this prevents deletes or updates in the parent table until the child table transaction completes."

There's no requirement on whether the index should be only on the column(s) on which the foreign key is defined. Here's a test on Oracle 11.2.0.4 (code mostly from the quoted book):

create table emp(emp_id number primary key, dept_id number);

create table dept(dept_id number primary key);

alter table emp add constraint emp_fk1 foreign key (dept_id) references dept(dept_id);

insert into dept values(10);insert into dept values(20);

insert into dept values(10);

insert into dept values(20);

insert into dept values(30);

insert into emp values(1,10);

insert into emp values(2,20);

insert into emp values(3,10);

commit;

Session 1: delete from emp where dept_id= 10;

Session 2: delete from dept where dept_id= 30;

It hangs.

Go back to Session 1 and rollback, and to Session 2 (where the delete is done now) and rollback. Then create this index:

create index emp_fk1 on emp(dept_id);

Repeat the two deletes. Session 2 delete no longer hangs. Rollback in both sessions. And create a composite index.

drop index emp_fk1;

alter table emp add (name varchar2(10));

create index emp_fk2 on emp(dept_id, name);

Repeat the two deletes. Session 2 still does not hang.

oracle外键如何创建索引,子表建立外键的索引问题相关推荐

  1. mysql取消外码约束_MySQL 关闭子表的外键约束检察方法

    准备: 定义一个教师表.一个学生表:在学生表中引用教师表ID create table teachers(teacherID int not null auto_increment primary k ...

  2. oracle能不能改外键属性,CSS_小议Oracle外键约束修改行为(五), Oracle的外键用来限制子表 - phpStudy...

    小议Oracle外键约束修改行为(五) Oracle的外键用来限制子表中参考的字段的值,必须在主表中存在.而且在主表的记录发生变化导致外键参考唯一约束值发生了变化时,定义了一系列的动作. 前面几篇文章 ...

  3. Oracle中用一个序列给两个表创建主键自增功能的后果

    前几天在创建数据表时,想偷个懒,使用一个序列给两个表创建主键自增功能,结果...... 情景: 为宠物中心创建一个简单的数据库,要求如下: 1.创建一个主人表,字段:主人编号,主人姓名,主人性别,主人 ...

  4. mysql的表是聚集索引组织表_Mysql的Innodb引擎索引总结

    索引的目的是什么? 答:数据库添加索引的目的是为了加快查询速度. 索引的的数据结构是什么? 答:(这里的B是balance)B+树来存储索引,B+树类似于二叉树. B+树是怎么查找数据的? 答:B+树 ...

  5. python字典添加主键_Python--MySql(主键的创建方式、存储引擎、存储过程、索引、pymsql)...

    主键的创建方式 1. create tablestud( idint not null unique, namevarchar(20) ); mysql> descstud;+-------+- ...

  6. SQL SERVER 2008 创建,删除,添加表的主键

    创建有主键的表 create table 教师 (编号 char(10),姓名 char(10) not null,性别 char(2),年龄 int,职称 char(10),constraint a ...

  7. SQL Server 索引和表体系结构(包含列索引)

    包含列索引 概述 包含列索引也是非聚集索引,索引结构跟聚集索引结构是一样,有一点不同的地方就是包含列索引的非键列只存储在叶子节点:包含列索引的列分为键列和非键列,所谓的非键列就是INCLUDE中包含的 ...

  8. 关于spring boot多张表建立外健的讨论

    现在有四张表:student(学生表).blogs(博客表).comment(评论表).reply(回复表) 现在说一下这四张表: student(学生表):学生的信息记录表 blogs(博客表):学 ...

  9. MySQL 关闭子表的外键约束检察

    准备: 定义一个教师表.一个学生表:在学生表中引用教师表ID create table teachers(teacherID int not null auto_increment primary k ...

最新文章

  1. 人工智能设计芯片,比你想象的更大胆
  2. c++ requests网络请求库
  3. 国服被ban咋看_LOL国服十大神人霸哥
  4. idea 快捷键java
  5. [html] 说说js代码写到html里还是单独写到js文件里哪个好?为什么?
  6. 5.3矩阵乘积(三元组存储结构)
  7. 李想当年是被赶出汽车之家?真相了...
  8. VS2015 输出目录、中间目录、目标文件名、工作目录
  9. windowsC盘msp文件清理
  10. 如何解决金山词霸与卡巴斯基的冲突问题
  11. [Android第三方类库]聊一聊Android的第三方开发组件
  12. 科创板第二批受理名单公布,为何AI独角兽 “全军覆没”?
  13. python 爬虫保存为word_微信公众号文章爬虫,本地word文档保存
  14. 大数据产品推荐:金蜂巢大数据集成与脱敏系统
  15. Android apk下载与安装
  16. 英文论文评审意见_小弟第一篇英文论文审稿意见求助 - 论文投稿 - 小木虫 - 学术 科研 互动社区...
  17. 小程序微信商家API V3对接转账到零钱(JAVA)
  18. 互联网的「中年危机」是什么?程序员对中年被裁有什么误解
  19. Linux运维面试题-01
  20. 切图工具GraphicsMagick安装

热门文章

  1. [PyTorch] rnn,lstm,gru中输入输出维度
  2. Java中对象引用的机制,及特点
  3. TensorFlow用法
  4. 色彩(颜色)空间原理(下)
  5. 深度学习数据特征提取:ICCV2019论文解析
  6. Python的Xpath介绍和语法详解
  7. adb.exe: device offline
  8. C++ 结构体struct 的使用
  9. 201621123030《Java程序设计》第4周学习总结
  10. JavaScript总结(七)