首页 > 数据库 > Oracle > oracle管理常用命令整理(三)
索引
创建索引
--B-TREE索引
create index id_idx on test(id);
--位图索引
create bitmap index gender_idx on test(gender);
--反向索引
create index reverse_idx on test(id) reverse;
--创建全局索引(已散列为例,其他类似)
create index id_idx on test(id)
global partition by hash(id)
(partition p1 tablespace ts1,
partition p2 tablespace ts2,
partition p3 tablespace ts3,
partition p4 tablespace ts4);
--创建本地索引
create index id_idx on testt(id) local
tablespace localindex_01;
[@more@]监控索引
--打开监控
alter index id_idx monitoring usage; --取消监控使用nomonitoriong
--查看使用情况
select * from v$object_usage
where index_name='ID_IDX';
维护索引
--检查索引的有效性
analyze index id_idx validate structure;
select blocks, pct_used, distinct_keys,lf_rows, del_lf_rows
from index_stats;
--重建索引
alter index id_idx rebuild;
--tablespace idx_tbs; --重建的同时更改索引存放的表空间
--reverse/noreverse --B-TREE和反向索引转换
alter index id_idx rebuild online; --减少重建时的并发限制
--合并索引
alter index id_idx coalesce;
完整性约束
创建约束
--创建表时创建约束
create table test (id number primary key);
create table test(id number,constraint pk_id primary key(id)); --取约束名,如果约束中有多 个列,则必须使用这种方式
create table test (id number constraint fk_id references test02(id) --创建外键约束
deferrable initially deferred/immediate); --指定检查时间
--修改表时创建约束
alter table test add constraint pk_id primary key(id);
--disable validate; --确保表中数据满足约束,然后禁用约束
--disable novalidate; --所有数据都不检查有效性
--enable validate; --所有数据都检查有效性
--enable novalidate; --只对新插入数据检查,原有数据不检查
--rely disable novalidate --信赖约束
alter table test modify id primary key;
--修改约束检查时间
set constraint pk_id deferred/immediate;
视图
创建视图
Create view my_test as
Select id,name from test where id=1;
创建实体化视图
--授权
grant create materialized view to scott;
grant query rewrite to scott;
--创建实体化视图日志(FAST刷新)
create materialized view log on test;
--创建实体化视图
create materialized view test_mv
build deferred --默认为immediate,创建时即写入数据
refresh fast on commit --刷新方式和刷新时机
enable query rewrite --启用查询重写
as
select sum(empno) from test
group by deptno;
同义词、序列
创建同义词
--公共同义词
create public synonym emp for scott.emp;
--私有同义词
create synonym emp for scott.emp;
删除同义词
--删除公共同义词
drop synonym emp public;
--删除私有同义词
drop synonym emp;
切换模式
alter session set current_schema=scott; --只切换,但不授予任何权限
创建序列
create sequence test_seq
start with 1000 --开始值
increment by 1 --增长值
maxvalue 10000 --最大值
cache 30 --缓冲多少值到内存中,默认为20,也可设no cache
no cycle --是否循环
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/10176825/viewspace-1018401/ ,如需转载,请注明出处,否则将追究法律责任。