记录、同义词、序列、异常及授权的使用

2010年8月31日 18:22:45

 记录

--记录的用法 即一个多字段复合的写法(前提是对应字段与表中的字段类型长度一致)
--g3e_attribute表的g3e_ano类型是number,g3e_username类型是varchar2
declare
type myrecord is record(
id varchar2(10),
name varchar2(10));
real_record myrecord;
begin
select g3e_ano,g3e_username into real_record from g3e_attribute where g3e_ano=1;
dbms_output.put_line(real_record.id||','||real_record.name);
end;

--用%type直接定义字段类型与某表的字段一样
declare
type myrecord is record(
id g3e_attribute.g3e_ano%type,
name varchar2(10));
real_record myrecord;
begin
select g3e_ano,g3e_username into real_record from g3e_attribute where g3e_ano=1;
dbms_output.put_line(real_record.id||','||real_record.name);
end;

--定义一个记录直接与表的字段都一致
declare
myrecord g3e_attribute%rowtype;--表示myrecord与g3e_attribute表的各个字段的类型都一样
begin
select * into myrecord from g3e_attribute where g3e_ano=1;
dbms_output.put_line(myrecord.g3e_ano||','||myrecord.g3e_username||','||myrecord.g3e_cno);
end; 

同义词

 同义词即创建表的另一名字,这样就可以在别的用户下直接采用所创建的同义词进行查询
--专有的同义词,即scott用户专有,创建该同义词后,就可以在当前用户下直接访问Scott用户下的dept表
create synonym dept for scott.dept;

--删除同义词
drop synonym dept;

--公共的同义词,创建该同义词后,就可以在任意用户下直接访问Scott用户下的dept表
create public synonym dept for scott.dept;
创建该公共同义词后,就可以在任意用户下用dept这个表名进行查询
select * from dept;而不用写select * from Scott.dept(假设dept表是在Scott用户下创建的)

--查看同义词
select * from user_synonyms;

序列

--创建一个序列
create  sequence myseq
start with 1 --从1开始
increment by 1 --递增1
order          --排序(从小到大)
nocycle;       --避免重复,不循环

select myseq.nextval from dual;--查询一次就递增一个
select myseq.currval from dual;--查看当前的序列值
select * from dba_sequences;--查看序列
alter sequence myseq increment by 3;--修改递增量
--注:不能修改序列的当前值 

 异常

--系统自定义的异常写法
declare
test varchar2(10);
begin
select g3e_username into test from g3e_attribute where g3e_ano=100000;
dbms_output.put_line(test);
exception
when NO_DATA_FOUND then
dbms_output.put_line('没有找到数据');
end;

--自定义异常的写法
DECLARE
TNAME VARCHAR2(100);
e EXCEPTION;
BEGIN
select g3e_username into TNAME from g3e_attribute where g3e_ano=6603;
IF TNAME<>'备注'  then
raise e;
end if;
dbms_output.put_line(tname);
exception
when e then
dbms_output.put_line('错误,不是备注');
end; 

授权

grant
grant select on dept to tt;--将表dept表的查询权限授权给tt用户

grant privilege to public;--将权限立即授以每个oracle用户

grant privilege to tt [with admin option];-- 如果指定了with admin option,该用户也可以向其它用户授予权限

revoke
revoke select on dept from tt;--将权限收回

revoke privilege on dept from tt [cascade constraints] [force]; --如果指定了cascade constraints,并且要被取消的是references权限,那么tt使用该权限

创建的所有引用完整性限制都会被取消。当取消一个具有表依赖性的对象类型的excute权限时要使用force关键字。

revoke privilege from tt;--取消一个系统的权限

原文地址:https://www.cnblogs.com/lanzi/p/1813962.html