索引
1.介绍
索引是一种可以选择创建的数据库对象,主要用于提高查询性能
与书本的目录相似,数据库索引吧用户感兴趣的列值连通其rowid存储在一起【rowid包含列的物理位置】,如果没有可用的索引,oracle就必须读取表中的每一行,才能确定所需的信息
索引需要消耗磁盘空间
比如:
查询一个学生的学号是001的姓名,sql语句为select xm from bj.student where xsxm=‘001’; 如果xsxm属性上没有索引,必须遍历整个表,直到xsxm等于001这一行被找到,有了索引后【必须是在xsxm这一列上建立索引】在索引中查询,查询次数要少很多。
2.索引创建
create [unique] [bitmap] [cluster] index 索引名 on 表名 (列名[asc/desc] [,列名[asc/desc]]...) TABLESPACE <tablespace_name>;
// tablespace创建在哪个表空间中,[unique] [bitmap] [cluster]index修饰词,不加修饰词index创建普通闭数索引
[unique](distinct):唯一性索引,不允许表中不同的元组在索引列上取相同值,若存在相同值,则系统给出相关信息,不建此索引
[cluster]:聚集索引,表中元组按索引项的值排序并物理的聚集在一起,一个基本表上只能建一个聚集索引(改索引可包含多个列)
asc/desc:索引表中索引值得排序次序,缺省为asc
== oracle数据库自动给每个表的主码建立唯一索引==
列:在表student.per_natl的xm属性上建立索引
create index student.sy_student.per_natl_xm
on student.per_natl(xm)
tablespace tsi_student;
//student.sy索引名称,在student.per_natl(xm)姓名列上创建索引,指定表空间tsi_student。
在teacher.patient_script_detl的xmid和sxh属性上建立索引
create index teacher.sy_teacher.patient_script_detl_xmidsxh //索引名
on teacher.patient_script_detl(smid,sxh)
tablespace tsi_dw;
索引一经建立,有oracle对其进行使用和维护,不需要用户干预
3.删除索引
drop index <索引名>
4.索引创建规则
- 1)创建索引和维护索引要耗费时间,这种时间随着数据量增加而增加
- 2)索引需要占用物理空间,除了数据表占数据空间之外,每个索引还要占一定的物理空间
- 3)当对表中的数据进行增加、删除和修改的时候,索引也要动态的维护,这样就降低了数据的维护速度
序列号
1.介绍
在oracle中sequence就是序列号,是数据库系统按照一定规则自动增加的数字序列,一般用在需要按序列号排序的地方
2.创建sequence
create sequence <Sequence_name>
increment by 1 //每次增加的数值
start with 1 //开始数值
maxvalue 99999999999999 //最大值
minvalue 1 nocycle;
//nocycle:当sequence达到最大值时,不重头开始(也可以选择cycle,表示到达最大值得时候,重头开始)
例:建立xmid序列号
create sequence student.xmid minvalue 1000 maxvalue 9999 start with 1000
increment by cycle 1 ;//每次取值加1
//Sequence定义后,使用方式
select student.xmid.nextval //查看下一个值,创建初期只能查看下一个值
from dual;
select student.xmid.currentval //查询当前的值
from dual;
3.修改序列号
alter squence 序列名
[increment by n]
[{maxvalue / minvalue n|nomaxvalue}]
[{cycle|nocycle}]
[{cache n|nocache}];
//不能修改序列号的初始值
4.删除序列号
drop sequence 序列名;