当前位置: 首页 > news >正文

PK Nounique CASCADE DROP INDEX keep index

Explicit Control Over Indexes when Creating, Disabling, or Dropping PK/Unique Constraints (Doc ID 139666.1)​编辑To Bottom


PURPOSEIn Oracle 9i, the DBA has an explicit control over how indexes are affectedwhile creating, disabling, or dropping Primary Key (PK) and unique constraints.This bulletin explains the different behaviours of indexes associated withPrimary Key or UNIQUE constraints according to the new clauses used when you execute one of the following commands:CREATE TABLE ... PRIMARY KEY/UNIQUEALTER TABLE  ... DISABLE PRIMARY KEY/UNIQUEALTER TABLE  ... DROP PRIMARY KEY/UNIQUESCOPE & APPLICATIONIt is important for DBAs to know what happens to the indexes when creating,disabling or dropping a constraint relying on an index, since indexes may have to be rebuilt after these operations. This can have two consequences:- Indexes may be missing for the Cost Based Optimizer (CBO) if the DBA thinks that the index was not dropped. This can have a major impact on performance.- Index rebuilding takes time.Explicit control over INDEXES when DISABLING/DROPPING PK, Unique constraints:
=============================================================================A. Creation of Primary Key/Unique constraints and associated index ----------------------------------------------------------------In the following views, depending on the way you created the Primary Key (PK)or UNIQUE constraint and its associated index, you get these different combinations:+-----------------+        +------------+| DBA_CONSTRAINTS |        | DBA_INDEXES|+-----------------+        +------------+-----------------------------   ------------Constraint_name   Index_name     Index_name--------------- -------------   ------------
Case 1: Create constraint, and index   PK_EMP_ID     EMP_ID_IX      EMP_ID_IX    explicitely within the samestatement.Case 2: Create constraint, and index   PK_EMP_ID     PK_EMP_ID      PK_EMP_ID    implicitely within the same statement.Case 3: Create constraint and index    PK_EMP_ID         -          EMP_ID_IX   separately within twostatements.Enable the constraint.         PK_EMP_ID     EMP_ID_IX      EMP_ID_IX-------------------------------------------------------------------------
Case 1: Create constraint and index explicitely within the same statement
-------------------------------------------------------------------------SQL> drop table <OWNER>.<TABLE_NAME>
Table dropped.SQL> create table <OWNER>.<TABLE_NAME>(emp_id NUMBERCONSTRAINT pk_emp_id PRIMARY KEY USING INDEX(CREATE INDEX <OWNER>.emp_id_ix ON <OWNER>.<TABLE_NAME>(emp_id)TABLESPACE indx),ename VARCHAR2(12),sal   number);Table created.SQL> select index_name,uniqueness from dba_indexes where table_name='<TABLE_NAME>';INDEX_NAME                     UNIQUENES------------------------------ ---------EMP_ID_IX                      NONUNIQUESQL> select constraint_name,index_name, constraint_type from dba_constraintswhere table_name='<TABLE_NAME>' and constraint_type='P';CONSTRAINT_NAME                INDEX_NAME                     C------------------------------ ------------------------------ -PK_EMP_ID                      EMP_ID_IX                      P-------------------------------------------------------------------------
Case 2: Create constraint and index implicitely within the same statement
-------------------------------------------------------------------------SQL> drop table <OWNER>.<TABLE_NAME>
Table dropped.SQL> create table <OWNER>.<TABLE_NAME>(emp_id NUMBERCONSTRAINT pk_emp_id PRIMARY KEY USING INDEX TABLESPACE indx,ename VARCHAR2(12),sal   number);Table created.SQL> select index_name,uniqueness from dba_indexes where table_name='<TABLE_NAME>';INDEX_NAME                     UNIQUENES------------------------------ ---------PK_EMP_ID                      UNIQUESQL> select constraint_name,index_name, constraint_type from dba_constraintswhere table_name='<TABLE_NAME>' and constraint_type='P';CONSTRAINT_NAME                INDEX_NAME                     C------------------------------ ------------------------------ -PK_EMP_ID                      PK_EMP_ID                      P--------------------------------------------------------------------
Case 3: Create constraint and index separately within two statements
--------------------------------------------------------------------SQL> drop table <OWNER>.<TABLE_NAME>
Table dropped.SQL> create table <OWNER>.<TABLE_NAME>(emp_id NUMBERCONSTRAINT pk_emp_id PRIMARY KEY  DISABLE,ename VARCHAR2(12),sal   number);Table created.SQL> create index <OWNER>.emp_id_ix on <OWNER>.<TABLE_NAME>(emp_id)tablespace indx;
Index created.SQL> select index_name,uniqueness from dba_indexes where table_name='<TABLE_NAME>';INDEX_NAME                     UNIQUENES------------------------------ ---------EMP_ID_IX                      NONUNIQUESQL> select constraint_name,index_name, constraint_type from dba_constraintswhere table_name='<TABLE_NAME>' and constraint_type='P';CONSTRAINT_NAME                INDEX_NAME                     C------------------------------ ------------------------------ -PK_EMP_ID                                                     PSQL> alter table <OWNER>.<TABLE_NAME> ENABLE constraint pk_emp_id;
Table altered.SQL> select index_name,uniqueness from dba_indexes where table_name='<TABLE_NAME>';INDEX_NAME                     UNIQUENES------------------------------ ---------EMP_ID_IX                      NONUNIQUESQL> select constraint_name,index_name, constraint_type from dba_constraintswhere table_name='<TABLE_NAME>' and constraint_type='P';CONSTRAINT_NAME                INDEX_NAME                     C------------------------------ ------------------------------ -PK_EMP_ID                      EMP_ID_IX                      PB. Disabling PK/UNIQUE constraints: what happens to the associated index ---------------------------------------------------------------------In Case 1 where the index was created explicitely within the same statementas the constraint, the index is in both cases disassociated from the constraint; depending on the clause "CASCADE DROP INDEX" usage, the index is dropped or not.In traditionnal Case 2, the behavior remains the same: using the clause "CASCADE DROP INDEX" or not does not influence the usual behavior: it automatically drops the relying index.In case 3, disabling the constraint drops the index or not: * if the constraint has never been enabled, it never drops the index.* but in most cases, the constraint has been enabled for some time. In this case, the clause "CASCADE DROP INDEX" drops the index.+-----------------+       +------------+| DBA_CONSTRAINTS |       | DBA_INDEXES|+-----------------+       +------------+-----------------------------   ------------Constraint_name   Index_name     Index_name--------------- -------------   ------------
Case 1: ALTER TABLE ... DISABLE PK     PK_EMP_ID         -             -        CASCADE DROP INDEX;orALTER TABLE ... DISABLE PK;    PK_EMP_ID         -         EMP_ID_IX    Case 2: ALTER TABLE ... DISABLE PK     PK_EMP_ID         -             -       CASCADE DROP INDEX;or ALTER TABLE ... DISABLE PK;    PK_EMP_ID         -             -      Case 3: ALTER TABLE ... DISABLE PK     PK_EMP_ID         -             -    CASCADE DROP INDEX;or ALTER TABLE ... DISABLE PK;    PK_EMP_ID         -         EMP_ID_IXC. Dropping PK/UNIQUE constraints: what happens to the associated index ---------------------------------------------------------------------In Case 1, where the index was created explicitely within the same statementas the constraint, the index is by default KEPT when the constraint is dropped.If you want the index to be dropped, you have to explicitely ask for it through the "DROP INDEX" clause.In case 2, the behavior is the opposite: if you want the index to be kept and the constraint dropped, you have to explicitly ask for it with the "KEEP INDEX" clause; otherwise the index is DROPPED by default.In Case 3, dropping the constraint drops the index or not: * if the constraint has never been enabled, it never drops the index.* but in most cases, the constraint has been enabled for some time. Then the index is by default KEPT when the constraint is dropped. If you want the index to be dropped, you have to explicitly ask for it with the "DROP INDEX" clause.+-----------------+   +-----------+| DBA_CONSTRAINTS |   |DBA_INDEXES|+-----------------+   +-----------+----------------------- ------------Constraint  Index_name   Index_name----------- ----------- ------------
Case 1: ALTER TABLE ... DROP PK DROP INDEX;     -            -           -       
Case 1: ALTER TABLE ... DROP PK KEEP INDEX;     -            -       EMP_ID_IX              
Case 1: ALTER TABLE ... DROP PK;                -            -       EMP_ID_IX   Case 2: ALTER TABLE ... DROP PK DROP INDEX;     -            -           -                                                      
Case 2: ALTER TABLE ... DROP PK KEEP INDEX;     -            -       PK_EMP_ID                                                              
Case 2: ALTER TABLE ... DROP PK;                -            -           -       Case 3: ALTER TABLE ... DROP PK DROP INDEX;     -            -           -   
Case 3: ALTER TABLE ... DROP PK KEEP INDEX;     -            -       EMP_ID_IX   
Case 3: ALTER TABLE ... DROP PK;                -            -       EMP_ID_IX
http://www.lryc.cn/news/124937.html

相关文章:

  • 【Antd】实现Table组件行点击,解决某一列不触发行点击
  • Kafka3.0.0版本——Broker( 退役旧节点)示例
  • 【Rust】Rust学习 第十二章一个 I/O 项目:构建一个命令行程序
  • 【MySQL--->表的操作】
  • PyTorch从零开始实现ResNet
  • 企业微信 企业内部开发 学习笔记
  • 03 QT基本控件和功能类
  • epoll数据结构
  • LINUX学习笔记_GIT操作命令
  • 第一百二十九天学习记录:数据结构与算法基础:栈和队列(中)(王卓教学视频)
  • C语言 — qsort 函数
  • 开放式耳机哪个好一点?推荐几款优秀的开放式耳机
  • vue-cli前端工程化——创建vue-cli工程 router版本的创建 目录结构 案例初步
  • Go和Java实现外观模式
  • 人工智能(一)基本概念
  • 〔AI 绘画〕Stable Diffusion 之 解决绘制多人或面部很小的人物时面部崩坏问题 篇
  • 初步认识OSI/TCP/IP一(第三十八课)
  • 英伟达结构化剪枝工具Nvidia Apex Automatic Sparsity [ASP](2)——代码分析
  • FileNotFoundError: [WinError 2] 系统找不到指定的文件。
  • Linux: sysctl:net: IPV4_DEVCONF_ALL ignore_routes_with_linkdown; all vs default
  • 光耦继电器:实现电气隔离的卓越选择
  • 鸿蒙开发学习笔记2——实现页面之间跳转
  • 电子商务类网站需要什么配置的服务器?
  • table 根据窗口缩放,自适应
  • 应急响应-Webshell
  • 【调整奇数偶数顺序】
  • Linux(Ubuntu)系统临时IP以及静态IP配置(关闭、启动网卡等操作)
  • 2023-08-11 LeetCode每日一题(矩阵对角线元素的和)
  • Github 80 个键盘快捷键和一些搜索技巧的备忘清单
  • 神经网络基础-神经网络补充概念-08-逻辑回归中的梯度下降算法