第十章创建其他对象(视图,索引,序列,同义词) 视图的优点: 1.简化查询 2.可以限制数据访问 3.提供数据的独立性 4.同样的数据可以采用不同的视图展示 创建视图: create view emp_view as select employee_id id_number, last_name name, salary*12 any_salary from employees where department_id=50; 修改视图: create or replace view emp_view (id_number ,name,sal,department_id) as select employee_id,first_name||' '||last_name ,salary,department_id from employees where department_id=80; 创建复杂的视图: create or replace view dept_sum_vu (name,minsal,maxsal ,avgsal) as select d.department_name,min(e.salary), max(e.salary),avg(e.salary) from employees e join departments d on ( e.department_id=d.department_id) group by d.department_name; 创建只读视图: create view mv as select * from employees with read only; 以下情况不能通过视图添加数据: 1.分组函数,group by 功能 2.包含distinct关键字 3.包含伪列rownum关键字 4.通过表达式定义列 Sequence 创建序列 create sequence dept_id_seq increment by 10 start with 120 maxvalue 9999 nocache nocycle; 备注:伪列nextval和currentval的使用. 序列的使用: insert into departments(department_id,department_name,location_id) values(dept_id_seq.nextval ,'Support',2500); 查看序列值 select dept_id_seq.nextval from dual; 修改序列 alter sequence dept_id_seq increment by 20 maxvalue 99999 nocache nocycle; Indexes:提高查询的效率. create index emp_last_name_idx on employees(last_name); Oracle创建索引的几条策略 学习Oracle时,经常会遇到Oracle索引问题,这里将介绍Oracle索引问题的解决方法。Oracle索引和对应的表应该位于不同的表空间中,Oracle能够并行读取位于不同硬盘上的数据,可以避免产生I/O冲突B树索引:在B树的叶节点中存储索引字段的值与ROWID.唯一索引和不唯一索引都只是针对B树索引而言。Oracle最多允许包含32个字段的复合索引 Oracle索引创建策略 1.导入数据后再创建索引 2.不需要为很小的表创建索引 3.对于取值范围很小的字段(比如性别字段)应当建立位图索引 4.限制表中的索引的数目 5.为索引设置合适的PCTFREE值 6.存储索引的表空间最好单独设定 创建不唯一索引 1. create index emp_ename on employees(ename) 2. tablespace users 3. storage(……) 4. pctfree 0; 创建唯一索引 1. create unique index emp_email on employees(email) 2. tablespace users; 创建位图索引 1. create bitmap index emp_sex on employees(sex) 2. tablespace users; 创建反序索引 1. create unique index order_reinx on orders(order_num,order_date) 2. tablespace users 3. reverse; 创建函数索引(函数索引即可以是普通的B树索引,也可以是位图索引) 1. create index emp_substr_empno 2. on employees(substr(empno,1,2)) 3. tablespace users; 以上介绍Oracle索引创建策略。 在以下情况下推荐创建索引: 1.一个包含在一个范围内的. 2.一个列彪悍大量的null数值. 3.一列或者多列经常作为where字句或者join使用. 4.表很大并且查询频繁,获取表的数据小于2%到4%之间时候. 在以下情况下不推荐创建索引: 1.表频繁更改. 2.索引列为表达式的一部分. 3.表很小并且多数查询结果大于2%到4%之间时候. 4.列在查询条件中不经常使用. Synonym: 创建同义词synonym: create public synonym synonym_name for object; 删除同义词 drop synonym synonym_name; 第十一章数据库字典视图 desc dictionary; select * from dictionary where table_name = 'USER_OBJECTS'; user_objects select object_name ,object_type ,created ,status from user_objects order by object_type; all_objects; user_tables: desc user_tables; select table_name from user_tables; user_tab_columns: desc user_tab_columns; select column_name,data_type, data_length,data_precision,data_scale,nullable from user_tab_columns where table_name ='' user_constraints select constraint_name,constraint_type,search_condition,r_constraint_name, delete_rule,status from user_constraints where table_name='' user_cons_columns: select constraint_name,column_name from user_cons_columns where table_name='' user_views select distinct view_name from user_views; select text from user_views where view_name =''; user_sequences desc user_sequences select sequence_name,min_value,max_value,increment_by ,last_number from user_sequences; user_synonyms select * from user_synonyms 给表添加注释 comment on table table_name is 'EMPLOYEE INFORMATION'; user_tab_comments desc user_tab_comments; all_col_comments; user_col_comments: all_tab_comments; user_tab_comments;