第十二章:其它数据库对象

序列:数据产生器


创建序列:

create sequence s1 start with 7936;


查看序列信息:

select * from user_sequences;

使用序列:

insert into emp (empno) values (s1.nextval);


修改序列:

注意:序列的start with 不能被修改!

alter sequence s1 increment by 10;


查看序列当前值:

注意:刚刚创建的序列在没有初始化的时候没有当前值!

select s1.currval from dual;

select s1.nextval from dual;


删除序列:

drop sequence s1;


索引:通过rowid实现的快速检索


对象号 文件号 块号 行号

AAAMfM AAE AAAAAg AAI

OOOOOO FFF BBBBBB RRR


A - Z a - z 0 - 9 + /

0 - 25 26 - 51 52 - 61 62 63


AAAM0a AAE AAAAAc AAA

M*64*64 + O*64 + 26 =


select

dbms_rowid.ROWID_OBJECT(rowid) oooooo,

dbms_rowid.ROWID_RELATIVE_FNO(rowid) fff,

dbms_rowid.ROWID_BLOCK_NUMBER(rowid) bbbbbb,

dbms_rowid.ROWID_ROW_NUMBER(rowid) rrr,

rowid,empno

from emp;


自动创建索引(由主键或唯一键约束创建):

drop table e purge;

create table e as select * from emp;


select index_name from user_indexes where table_name='E';


alter table e add constraint pk_e_empno primary key (empno);

alter table e add constraint uk_e_ename unique(ename);

主键和唯一键有隐式的创建索引的动作!前提是启用约束时当前列没有索引!


手工创建索引:

create index i_e_job on e (job);

create index i_e_2 on e (empno,job,sal);


select index_name,table_name,column_name from user_ind_columns;


基于函数的索引:

create index scott.i_e_ename on e (lower(ename));


监控索引的使用:

alter index UK_E_ENAME monitoring usage;

select * from v$object_usage;

select * from e where lower(ename)='scott';

select * from v$object_usage;

select * from e where ename='SCOTT';

select * from v$object_usage;

create index i_e_ename on e (lower(ename));

alter index i_e_ename monitoring usage;

select * from e where lower(ename)='scott';

alter index UK_E_ENAME nomonitoring usage;


删除索引:

drop index i_e_ename;


分析索引结构有效性:

analyze index i_e_ename validate structure;


查看索引结构:

select NAME,HEIGHT,BLOCKS,BR_BLKS,BR_ROWS,LF_BLKS,LF_ROWS from index_stats;


合并索引叶级块碎片:

alter index i_e_ename coalesce;


重建索引:

alter index i_e_ename rebuild;

alter index i_e_ename rebuild online;


同义词:复杂对象名称的别名!

私有同义词:只有自己能用,不能和本方案下的表同名

创建私有同义词:

conn system/oracle

select * from scott.emp;

create synonym emp for scott.emp;


有了同义词就可以使用emp代替scott.emp

select * from emp;

show user

USER is "SYSTEM"

SQL> conn / as sysdba

Connected.

SQL> show user

USER is "SYS"

select * from emp;


删除私有同义词:

conn system/oracle

drop synonym emp;

create table emp (i int);

insert into emp values (1982);

commit;

create synonym emp for scott.emp;

ERROR at line 1:

ORA-00955: name is already used by an existing object


共有同义词:所有用户都能用,可以和本方案下的表同名,但表的优先级别高于同义词

创建公有同义词:

create public synonym emp for scott.emp;

select * from emp;

I

----------

1982

select * from scott.emp;


conn / as sysdba

select * from emp;


删除共有同义词:

conn system/oracle

drop public synonym emp;