zoukankan      html  css  js  c++  java
  • Oracle数据库学习_Oracle索引之using index的用法

    using index的作用:

    using index可以让你在创建主键、唯一性约束的时候使用指定的索引或创建索引、或修改索引的存储结构。

    不用using index创建主键的情况

    先不用using index,创建主键时oracle自动创建唯一索引。主键名和索引名一致,主键列和索引列一致。

    alter table emp add constraint pk_emp_id primary key(emp_id); 

    执行上面语句的同时,同名索引pk_emp_id也被创建。

    因此,当使用下面语句删除主键的同时,同名索引也会被删除:

    alter table emp drop primary key;


    如果我想删除主键保留索引,可以使用下面语句:

    alter table emp drop primary key keep index;

    使用using index创建主键的情况

     如果表emp只有索引没有主键,如果想要使用刚创建的唯一索引pk_emp_id再创建主键,此时就用到了using index。

    alter table emp add constraint pk_emp_id primary key(emp_id)

    using index pk_emp_id ; 

    这样主键就创建成功了。注意,using index后面的索引所指定的索引列必须与主键指定的主键列相同,索引名和主键名相同与否没影响。

    实际上,在创建主键时不使用using index也可以创建索引。一是在主键列没有索引时,不使用using index也可以创建同名索引;二是主键列已经创建索引,不管索引名是否与主键名相同,都不会再创建相应的索引。

    这时候,使用alter table emp drop primary key;删除主键时,与不使用using index情况不同的是,由于索引不是在建主键时自动添加的,仅有主键被删除,索引会被保留下来。

  • 相关阅读:
    Bit Manipulation
    218. The Skyline Problem
    Template : Two Pointers & Hash -> String process
    239. Sliding Window Maximum
    159. Longest Substring with At Most Two Distinct Characters
    3. Longest Substring Without Repeating Characters
    137. Single Number II
    142. Linked List Cycle II
    41. First Missing Positive
    260. Single Number III
  • 原文地址:https://www.cnblogs.com/tongying/p/13215803.html
Copyright © 2011-2022 走看看