oracle创建、删除索引以及对字段的一系列操作

  • Post author:
  • Post category:其他


一、创建索引

create index 索引名 on 表名(列名);

二、查找索引

--根据索引名,查询表索引字段
select * from user_ind_columns where index_name='索引名';
--根据表名,查询一张表的索引
select * from user_indexes where table_name='表名';

三、删除索引

drop index 索引名;

四、修改字段允许为空或者不允许为空

--设置字段允许为空
alter table 表名 modify 字段名 null;
--设置字段不允许为空
alter table 表名 modify 字段名 not null;

注意:修改字段允许为空的话,好像需要将字段值都为空的。

五、修改字段类型或者长度精度

---修改字段名
alter table 表名 rename column 旧字段名 to 新字段名;

---修改字段精度或长度
alter table 表名 modify 字段名 数据类型;

--例如:
alter table test modify number_test NUMBER(10,4);

注意:修改字段精度的时候,字段值要为空的。我另一篇博客有讲如何修改字段精度,需要的话可以去看看。

六、增加或修改字段注释

comment on column 表名.列名 is '注释内容';

七、修改索引

-- 重命名索引
alter index index_old rename to index_new;

-- 合并索引
alter index index_name coalesce;

-- 重新构造索引
alter index index_name rebuild;

注:索引经过一段时间的使用后,索引表中存储的空间会产生一些碎片,导致索引的查询效率降低,这个时候就可以使用合并索引或删除索引重新构造索引。



版权声明:本文为weixin_42228950原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。