一:前言
二:索引对象
1:索引概述
2:创建索引
1:建立B树索引
1 SCOTT@orcl> conn scott/scott 2 Connected. 3 SCOTT@orcl> select table_name from user_tables; 4 5 TABLE_NAME 6 ------------------------------ 7 DEPT 8 EMP 9 BONUS 10 SALGRADE 11 DEPT_LOG 12 GOODS 13 DDL_OPER_LOG 14 TB_TEST 15 STUDENTS 16 STUDENTS_DEMO 17 STUDENTS_DEMO1 18 19 TABLE_NAME 20 ------------------------------ 21 STUDENTS_3 22 STUDENTS_4 23 BOOKS 24 BOOKS_1 25 MEMBERS 26 STUDENTS_7 27 STUDENTS_5 28 29 18 rows selected. 30 31 SCOTT@orcl> create index emp_deptno_index on emp(deptno) pctfree 25 tablespace users; 32 33 Index created. 34 35 SCOTT@orcl>1 SCOTT@orcl> select table_name,tablespace_name from user_tables; 2 3 TABLE_NAME TABLESPACE_NAME 4 ------------------------------ ------------------------------ 5 DEPT USERS 6 EMP USERS 7 BONUS USERS 8 SALGRADE USERS 9 DEPT_LOG USERS 10 GOODS USERS 11 DDL_OPER_LOG USERS 12 TB_TEST USERS 13 STUDENTS TBS_TEST1 14 STUDENTS_DEMO TBS_TEST1 15 STUDENTS_DEMO1 TBS_TEST1 16 17 TABLE_NAME TABLESPACE_NAME 18 ------------------------------ ------------------------------ 19 STUDENTS_3 TBS_TEST1 20 STUDENTS_4 TBS_TEST1 21 BOOKS TBS_TEST1 22 BOOKS_1 TBS_TEST1 23 MEMBERS TBS_TEST1 24 STUDENTS_7 TBS_TEST_3 25 STUDENTS_5 TBS_TEST_3 26 27 18 rows selected. 28 29 SCOTT@orcl>表创建索引默认是 B树索引.即:以 rowid 来实现快速查找数据的
2:建立位图索引
1 SCOTT@orcl> conn hr/hr; 2 Connected. 3 HR@orcl> select table_name ,tablespace_name from user_tables; 4 5 TABLE_NAME TABLESPACE_NAME 6 ------------------------------ ------------------------------ 7 REGIONS EXAMPLE 8 LOCATIONS EXAMPLE 9 EMPLOYEES_TEMP TBS_TEST1 10 DEPARTMENTS_TEMP TBS_TEST1 11 STUDENT TBS_TEST1 12 COUNTRIES 13 JOBS_TEMP USERS 14 JOB_HISTORY EXAMPLE 15 EMPLOYEES EXAMPLE 16 DEPARTMENTS EXAMPLE 17 JOBS EXAMPLE 18 19 11 rows selected. 20 21 HR@orcl> create bitmap index emp_salary_bmp on employees(salary) tablespace users; 22 23 Index created. 24 25 HR@orcl> desc employees; 26 Name Null? Type 27 ----------------------------------------- -------- ---------------------------- 28 EMPLOYEE_ID NOT NULL NUMBER(6) 29 FIRST_NAME VARCHAR2(20) 30 LAST_NAME NOT NULL VARCHAR2(25) 31 EMAIL NOT NULL VARCHAR2(25) 32 PHONE_NUMBER VARCHAR2(20) 33 HIRE_DATE NOT NULL DATE 34 JOB_ID NOT NULL VARCHAR2(10) 35 SALARY NUMBER(8,2) 36 COMMISSION_PCT NUMBER(2,2) 37 MANAGER_ID NUMBER(6) 38 DEPARTMENT_ID NUMBER(4) 39 40 HR@orcl>
1 2 3 4 HR@orcl> conn sys/oracle as sysdba; 5 Connected. 6 SYS@orcl> show parameter create_bitmap_area_size 7 8 NAME TYPE VALUE 9 ------------------------------------ ----------- ------------------------------ 10 create_bitmap_area_size integer 8388608 11 SYS@orcl>
3:建立反向建索引
1 SYS@orcl> conn scott/scott 2 Connected. 3 SCOTT@orcl> select table_name ,tablespace_name from user_tables; 4 5 TABLE_NAME TABLESPACE_NAME 6 ------------------------------ ------------------------------ 7 DEPT USERS 8 EMP USERS 9 BONUS USERS 10 SALGRADE USERS 11 DEPT_LOG USERS 12 GOODS USERS 13 DDL_OPER_LOG USERS 14 TB_TEST USERS 15 STUDENTS TBS_TEST1 16 STUDENTS_DEMO TBS_TEST1 17 STUDENTS_DEMO1 TBS_TEST1 18 19 TABLE_NAME TABLESPACE_NAME 20 ------------------------------ ------------------------------ 21 STUDENTS_3 TBS_TEST1 22 STUDENTS_4 TBS_TEST1 23 BOOKS TBS_TEST1 24 BOOKS_1 TBS_TEST1 25 MEMBERS TBS_TEST1 26 STUDENTS_7 TBS_TEST_3 27 STUDENTS_5 TBS_TEST_3 28 29 18 rows selected. 30 31 SCOTT@orcl> desc emp; 32 Name Null? Type 33 ----------------------------------------- -------- ---------------------------- 34 EMPNO NOT NULL NUMBER(4) 35 ENAME VARCHAR2(10) 36 JOB VARCHAR2(9) 37 MGR NUMBER(4) 38 HIREDATE DATE 39 SAL NUMBER(7,2) 40 COMM NUMBER(7,2) 41 DEPTNO NUMBER(2) 42 43 SCOTT@orcl> create index emp_job_reverse on emp(job) reverse tablespace users; 44 45 Index created. 46 47 SCOTT@orcl>
1 2 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 3 4 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 5 ------------------------------ -------------------------------------------------------------------------------------------------------------- 6 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 7 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL8 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 9 10 11 12 SCOTT@orcl> alter index emp_deptno_index rebuild reverse; 13 14 Index altered. 15 16 SCOTT@orcl> 17 18 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 19 20 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 21 ------------------------------ -------------------------------------------------------------------------------------------------------------- 22 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 23 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 24 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 25 26 27
4:基于函数的索引
1 2 SCOTT@orcl> conn scott/scott 3 Connected. 4 SCOTT@orcl> select empno,ename,sal from emp where job=upper('manager'); 5 6 EMPNO ENAME SAL 7 ---------- ---------- ---------- 8 7566 JONES 2975 9 7698 BLAKE 2850 10 7782 CLARK 2450 11 12 SCOTT@orcl> 13
1 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 2 3 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 4 -------------------------------------------------------------------------------------------------------------------------------------------- 5 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 6 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 7 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 8 9 SCOTT@orcl> desc emp 10 Name Null? Type 11 ----------------------------------------- -------- ---------------------------- 12 EMPNO NOT NULL NUMBER(4) 13 ENAME VARCHAR2(10) 14 JOB VARCHAR2(9) 15 MGR NUMBER(4) 16 HIREDATE DATE 17 SAL NUMBER(7,2) 18 COMM NUMBER(7,2) 19 DEPTNO NUMBER(2) 20 21 SCOTT@orcl> create index emp_job_fun on emp(lower(job)); 22 23 Index created. 24 25 26 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 27 28 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 29 -------------------------------------------------------------------------------------------------------------------------------------------- 30 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 31 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 32 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 33 EMP_JOB_FUN EMP SYS_NC00009$ 1 9 9 ASC FUNCTION-BASED NORMAL 34 35 36 37 SCOTT@orcl>
3: 修改索引
1 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 2 3 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 4 -------------------------------------------------------------------------------------------------------------------------------------------- 5 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 6 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 7 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 8 EMP_JOB_FUN EMP SYS_NC00009$ 1 9 9 ASC FUNCTION-BASED NORMAL 9 10 11 -----进行索引的合并 12 SCOTT@orcl> alter index EMP_DEPTNO_INDEX coalesce deallocate unused; 13 14 Index altered. 15 16 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 17 18 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 19 -------------------------------------------------------------------------------------------------------------------------------------------- 20 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 21 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 22 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 23 EMP_JOB_FUN EMP SYS_NC00009$ 1 9 9 ASC FUNCTION-BASED NORMAL 24 25 SCOTT@orcl>
1 2 SCOTT@orcl> alter idnex EMP_DEPTNO_INDEX rebuild;
1 alater index EMP_DEPTNO_INDEX rebuild tablespace example;
4:删除索引
1 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 2 3 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 4 -------------------------------------------------------------------------------------------------------------------------------------------- 5 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 6 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 7 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV 8 EMP_JOB_FUN EMP SYS_NC00009$ 1 9 9 ASC FUNCTION-BASED NORMAL 9 10 SCOTT@orcl> drop index EMP_JOB_FUN; 11 12 Index dropped. 13 14 SCOTT@orcl> select t.*,i.index_type from user_ind_columns t,user_indexes i where t.index_name = i.index_name and t.table_name = i.table_name and t.table_name ='EMP'; 15 16 INDEX_NAME TABLE_NAME COLUMN_NAME COLUMN_POSITION COLUMN_LENGTH CHAR_LENGTH DESC INDEX_TYPE 17 -------------------------------------------------------------------------------------------------------------------------------------------- 18 PK_EMP EMP EMPNO 1 22 0 ASC NORMAL 19 EMP_DEPTNO_INDEX EMP DEPTNO 1 22 0 ASC NORMAL/REV 20 EMP_JOB_REVERSE EMP JOB 1 9 9 ASC NORMAL/REV
5:显示索引信息
1:显示表的所有索引
1 SCOTT@orcl> conn system/oracle 2 Connected. 3 SYSTEM@orcl> select index_name ,index_type from dba_indexes where owner='HR'; 4 5 INDEX_NAME INDEX_TYPE 6 ------------------------------ --------------------------- 7 EMP_SALARY_BMP BITMAP 8 COUNTRY_C_ID_PK IOT - TOP 9 REG_ID_PK NORMAL 10 LOC_STATE_PROVINCE_IX NORMAL 11 LOC_ID_PK NORMAL 12 LOC_COUNTRY_IX NORMAL 13 LOC_CITY_IX NORMAL 14 DEPT_ID_PK NORMAL 15 DEPT_LOCATION_IX NORMAL 16 JOB_ID_PK NORMAL 17 EMP_NAME_IX NORMAL 18 19 INDEX_NAME INDEX_TYPE 20 ------------------------------ --------------------------- 21 EMP_MANAGER_IX NORMAL 22 EMP_EMP_ID_PK NORMAL 23 EMP_EMAIL_UK NORMAL 24 EMP_DEPARTMENT_IX NORMAL 25 EMP_JOB_IX NORMAL 26 JHIST_EMPLOYEE_IX NORMAL 27 JHIST_JOB_IX NORMAL 28 JHIST_DEPARTMENT_IX NORMAL 29 JHIST_EMP_ID_ST_DATE_PK NORMAL 30 SYS_C0011441 NORMAL 31 SYS_C0012464 NORMAL 32 33 22 rows selected. 34 35 SYSTEM@orcl>
2:显示索引列
1 SCOTT@orcl> column colunm_name for a10; 2 SCOTT@orcl> select column_name ,column_length from user_ind_columns where index_name='EMP_DEPTNO_INDEX'; 3 4 COLUMN_NAME COLUMN_LENGTH 5 ----------------------------------------- 6 DEPTNO 22 7 SCOTT@orcl>
3: 显示索引段位置及其大小
1 2 SCOTT@orcl> select tablespace_name,segment_type,bytes from user_segments where segment_name ='EMP_DEPTNO_INDEX'; 3 4 TABLESPACE_NAME SEGMENT_TYPE BYTES 5 ------------------------------ ------------------ ---------- 6 USERS INDEX 65536 7 8 SCOTT@orcl>
4: 显示函数索引
1 SCOTT@orcl> select tablespace_name,segment_type,bytes from user_segments where segment_name ='EMP_DEPTNO_INDEX'; 2 3 TABLESPACE_NAME SEGMENT_TYPE BYTES 4 ------------------------------ ------------------ ---------- 5 USERS INDEX 65536 6 7 SCOTT@orcl>
二:视图对象
1:创建视图
1:简单视图
1 SCOTT@orcl> conn scott/scott 2 Connected. 3 SCOTT@orcl> create or replace view emp_view as select empno,ename,job,deptno from emp where deptno=20; 4 5 View created. 6 7 SCOTT@orcl>1 SCOTT@orcl> select * from emp_view; 2 3 EMPNO ENAME JOB DEPTNO 4 ---------- ---------- --------- ---------- 5 7369 SMITH CLERK 20 6 7566 JONES MANAGER 20 7 7788 SCOTT ANALYST 20 8 7876 ADAMS CLERK 20 9 7902 FORD ANALYST 20 10 11 SCOTT@orcl>
对于 简单的视图而言,不仅是可以执行select 操作,而且还可以执行insert update delete 等操作
1 2 SCOTT@orcl> select * from emp_view; 3 4 EMPNO ENAME JOB DEPTNO 5 ---------- ---------- --------- ---------- 6 7369 SMITH CLERK 20 7 7566 JONES MANAGER 20 8 7788 SCOTT ANALYST 20 9 7876 ADAMS CLERK 20 10 7902 FORD ANALYST 20 11 12 SCOTT@orcl> insert into emp_view values(9304,'aaa','afda3eae',20); 13 14 1 row created. 15 16 SCOTT@orcl> select * from emp_view; 17 18 EMPNO ENAME JOB DEPTNO 19 ---------- ---------- --------- ---------- 20 7369 SMITH CLERK 20 21 7566 JONES MANAGER 20 22 7788 SCOTT ANALYST 20 23 7876 ADAMS CLERK 20 24 7902 FORD ANALYST 20 25 9304 aaa afda3eae 20 26 27 6 rows selected. 28 29 SCOTT@orcl> update emp_view set ename='aaaaee23' where empno='9304'; 30 31 1 row updated. 32 33 SCOTT@orcl> select * from emp_view; 34 35 EMPNO ENAME JOB DEPTNO 36 ---------- ---------- --------- ---------- 37 7369 SMITH CLERK 20 38 7566 JONES MANAGER 20 39 7788 SCOTT ANALYST 20 40 7876 ADAMS CLERK 20 41 7902 FORD ANALYST 20 42 9304 aaaaee23 afda3eae 20 43 44 6 rows selected. 45 46 SCOTT@orcl> delete from emp_view where empno=9304; 47 48 1 row deleted. 49 50 SCOTT@orcl> select * from emp_view; 51 52 EMPNO ENAME JOB DEPTNO 53 ---------- ---------- --------- ---------- 54 7369 SMITH CLERK 20 55 7566 JONES MANAGER 20 56 7788 SCOTT ANALYST 20 57 7876 ADAMS CLERK 20 58 7902 FORD ANALYST 20 59 60 SCOTT@orcl> commit; 61 62 Commit complete. 63 64 SCOTT@orcl>
2:建立只读视图
1 2 SCOTT@orcl> create or replace view emp_view_readonly as select * from dept where deptno != 88; 3 4 View created. 5 6 SCOTT@orcl> select * from emp_view_readonly; 7 8 DEPTNO DNAME LOC 9 ---------- -------------- ------------- 10 77 ????? ????? 11 15 asadad asdfasd 12 28 asdfa fasdf 13 38 adfasd adfasd 14 57 ??? ?? 15 10 ACCOUNTING NEW YORK 16 20 RESEARCH DALLAS 17 30 SALES CHICAGO 18 40 OPERATIONS BOSTON 19 20 9 rows selected. 21 22 SCOTT@orcl> 23
3:复杂视图
1 SCOTT@orcl> create or replace view emp_view_complex as select deptno as deptno ,max(sal) max_sal ,min(sal) min_sal ,avg(sal) avg_sal from emp group by deptno; 2 3 View created. 4 5 SCOTT@orcl> select * from emp_view_complex; 6 7 DEPTNO MAX_SAL MIN_SAL AVG_SAL 8 ---------- ---------- ---------- ---------- 9 10 30 3200 2500 2810 11 20 3000 2975 2991.66667 12 10 5000 2450 3725 13 14 SCOTT@orcl>
4:链接视图
1 2 SCOTT@orcl> create or replace view emp_view_union as select d.dname,d.loc,e.empno,e.ename from emp e,dept d where e.deptno=d.deptno and d.deptno=20; 3 4 View created. 5 6 SCOTT@orcl> select * from emp_view_union ; 7 8 DNAME LOC EMPNO ENAME 9 -------------- ------------- ---------- ---------- 10 RESEARCH aaa 7369 SMITH 11 RESEARCH aaa 7566 JONES 12 RESEARCH aaa 7788 SCOTT 13 RESEARCH aaa 7876 ADAMS 14 RESEARCH aaa 7902 FORD 15 16 SCOTT@orcl>
2:管理视图
1:查看视图定义
1 2 3 SCOTT@orcl> desc emp_view; 4 Name Null? Type 5 ----------------------------------------- -------- ---------------------------- 6 EMPNO NOT NULL NUMBER(4) 7 ENAME VARCHAR2(10) 8 JOB VARCHAR2(9) 9 DEPTNO NUMBER(2) 10 11 SCOTT@orcl>1 SCOTT@orcl> desc user_views; 2 Name Null? Type 3 ----------------------------------------- -------- ---------------------------- 4 VIEW_NAME NOT NULL VARCHAR2(30) 5 TEXT_LENGTH NUMBER 6 TEXT LONG 7 TYPE_TEXT_LENGTH NUMBER 8 TYPE_TEXT VARCHAR2(4000) 9 OID_TEXT_LENGTH NUMBER 10 OID_TEXT VARCHAR2(4000) 11 VIEW_TYPE_OWNER VARCHAR2(30) 12 VIEW_TYPE VARCHAR2(30) 13 SUPERVIEW_NAME VARCHAR2(30) 14 EDITIONING_VIEW VARCHAR2(1) 15 READ_ONLY VARCHAR2(1) 16 17 SCOTT@orcl> select text from user_views where view_name =upper('emp_view'); 18 19 TEXT 20 -------------------------------------------------------------------------------- 21 select empno,ename,job,deptno from emp where deptno=20 22 23 SCOTT@orcl>
2:修改视图定义
3:重新编译视图
4:删除视图
三:同义词对象
1 2 SCOTT@orcl> conn system/oracle 3 Connected. 4 SYSTEM@orcl> create public synonym public_demp for scott.dept; 5 6 Synonym created.
1 2 HR@orcl> conn scott/scott; 3 Connected. 4 SCOTT@orcl> select * from public_demp; 5 6 DEPTNO DNAME LOC 7 ---------- -------------- ------------- 8 77 ????? aaa 9 15 asadad aaa 10 28 asdfa aaa 11 38 adfasd aaa 12 57 ??? aaa 13 10 ACCOUNTING aaa 14 20 RESEARCH aaa 15 30 SALES aaa 16 40 OPERATIONS aaa 17 88 design beijing 18 19 10 rows selected. 20 21 SCOTT@orcl>
四:序列对象
1:创建序列
2:管理序列
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------