CREATE [UNIQUE] | [BITMAP] INDEX index_name --unique表示唯一索引ON table_name([column1 [ASC|DESC],column2 --bitmap,创建位图索引[ASC|DESC],…] | [express]) [TABLESPACE tablespace_name][PCTFREE n1] --指定索引在数据块中空闲空间[STORAGE (INITIAL n2)][NOLOGGING] --表示创建和重建索引时允许对表做DML操作,默认情况下不应该使用[NOLINE][NOSORT]; --表示创建索引时不进行排序,默认不适用,如果数据已经是按照该索引顺序排列的可以使用
例子:
--给列 localid 添加索引(索引名称一般就是表名加个index_列名)create Index index_localid_STATATIC_YEAR on STATATIC_YEAR(localid);
alter index index_localid_STATATIC_YEAR rename to bitmap_index;
alter index index_localid_STATATIC_YEAR coalesce;
方式一:删除原来的索引,重新建立索引
方式二:
alter index index_localid_STATATIC_YEAR rebuild;
drop index index_localid_STATATIC_YEAR;
---查询索引select * from user_indexes where table_name=‘PEOPLE‘ select index_name,index-type, tablespace_name, uniqueness from all_indexes where table_name =‘tablename‘; -- 创建例子create index index_sno on student(‘name‘); select * from all_indexes where table_name=‘student‘;
craete index index_sno on student(‘sno‘);
说明
使用场景
列基数(列不重复值的个数)大时适合使用B数索引
create bitmap index index_sno on student(sno);
说明
使用场景
对于基数小的列适合简历位图索引(例如性别等)
craete index index_sno on student(‘sno‘,‘sex‘);
create index fbi on student (upper(name)); select * from student where upper(name) =‘WISH‘;
-- 常用函数-- lower:大写字符转化成百小写的函数 select lower(表中字段) from 表名 --将表里的字问段信息中含有字母答的全部转回成小写-- upper:小写字答符转化成大写的函数 select upper(表中字段) from 表名 --将 user表里的字段信息中含有字母的全部转成大写-- substr:切割字符串create index t_object_type_idn_fun on t(substr(object_type,1,3));select * from t where substr(t.object_type,1,3)=‘XXX‘;
--在name上创建索引;create index index_name on student(‘name‘); --下面的方式oracle不适用name索引select * from student where name like ‘%wish%‘; --如果通配符出现在字符串的其他位置时,优化器能够利用索引select * from student where name like ‘wish%‘;
select * from student where not (score=100);select * from student where score <> 100; --替换为select * from student where score>100 or score <100
select * from student where score is not null;