Oracle复习笔记(二)

2015-02-02 23:25:38 · 作者: · 浏览: 22
[with admin option]; 2)授对象权限 grant all on scott.emp to lee [with admin option]; 3)撤销权限 revoke resource from lee; revoke update,delete on scott.emp from lee; /*----------------------------------------角色操作----------------------------------------*/ connect角色具有一般应用开发人员需要的大部分权限,当建立了一个用户后, 多数情况下,只要给用户授予connect和resource角色就够了。 connect角色具有以下系统权限: alter session create cluster create database link create session create view create sequence resource角色具有应用开发人员所需要的其他权限,比如建立存储过程、触发器等。 这里需要注意的是resource角色隐含了unlimited tablespace系统权限。 resource角色包含以下系统权限: create cluster create indextype create table create sequence create type create procedure create trigger 1)创建角色 create role lee not identified; create role lee identified by tiger; 2)删除角色 drop role 角色名; /*----------------------------------------序列操作----------------------------------------*/ --创建序列 create sequence orcl_seq increment by 1 --每次增长幅度[默认:1] start with 1 --开始时的序列号[默认:1] maxvalue 999 --限制生成的最大值 minvalue 1 --限制生成的最小值 nocycle --达到最大值后,重新生成序列,[默认:nocycle] cache 20; --预先分配20个空间以便更快生成序列 --删除序列 drop sequence orcl_seq; --使用序列 select orcl_seq.nextval from dual; --查看序列的当前值(至少使用1次后才可查询) select orcl_seq.currval from dual; /*----------------------------------------表操作----------------------------------------*/ --建表 create table tb( id number(10) primary key, name varchar2(20), age number(3) default 0 not null --constraint CK_name check(name not in ('@','#','$')) ) tablespace system; --插入数据 insert into tb values(orcl_seq.nextval,'lee',26); --修改列 alter table tb modify age default 1 modify name not null; --添加约束 alter table tb add constraint CK_name check(name not in ('@','#','$')) add constraint UQ_name unique(name); --alter table tb add constraint FK_? foreign key(?) references tb2(?); --删除约束 alter table tb drop constraint UQ_name; --修改列名 alter table tb rename column name to tname; --修改表名(sqlplus) rename tb to tab; --删除表 drop table tab; --复制表(不会复制约束) create table copy_tb as select ename,sal from scott.emp [where 1=2]; --多行插入(将查询结果集直接插入现有表中) insert into copy_tb select ename,sal from emp where ename = 'KING'; /*----------------------------------------视图操作----------------------------------------*/ 视图是一张虚拟表,是对基表数据的引用, 如果基表数据发生改变,视图中的数据也随之改变, 对视图成功的增删改操作,也将影响基表的数据. 实际开发中,一般只对视图作查询. --创建视图 create or replace view v_emp as select * from emp with read only; --删除视图(注:对视图的DML操作会影响原表的数据) drop view v_emp /*----------------------------------------索引操作---------------------------------------*/ 索引基于表的列创建,给经常用于where条件、分组、排序的列添加索引 1)标准索引 create index 索引名 on 表名(列名); 2)唯一索引,
Oracle
自动为主键、唯一键创建唯一索引 create unique index 索引名 on 表名(列名); 3)组合索引,一般用于多个条件的组合查询 create index 索引名 on 表名(列名1,列名2...); 4)反向键索引,按字节形式将数据反转,用于经常从后匹配的列 create index 索引名 on 表名(列名) reverse; 5)基于函数的索引,在应用函数的列上创建索引 --eg 经常按入职月份查询员工信息,请创建索引 create index ix_hiredate1 on emp(to_char(hiredate,'fmmm')); --注意:Oracle自动根据查询语句应用索引进行优化 select * from emp where to_char(hiredate,'fmmm')='2'; 6)删除索引 drop index 索引名; /*----------------------------------------事务提交----------------------------------------*/ 1、概念 1)事务是1个最小的执行单元(逻辑单元) 2)该单元包含1个或1组数据库操作(增删改) 3)该组操作要么同时成功,要么同时失败 2、特点(ACID) A- 原子性,事务是不允许再分的单元 C- 一致性,事务前后的数据应该保持一致 I- 隔离性,事务之间是相互独立的 D- 永久性