1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > oracle pl/sql学习笔记(三) 主键 约束 游标 触发器 序列等

oracle pl/sql学习笔记(三) 主键 约束 游标 触发器 序列等

时间:2019-08-29 01:59:51

相关推荐

oracle pl/sql学习笔记(三) 主键 约束 游标 触发器 序列等

--主键 primary key

1、实体完整性,防止出现重复数据

2、检索有利

3、支持外键

一个数据表,往往有一个与业务无关的逐渐,保证数据的唯一性

建表时添加约束 create table XX(列名 类型 primary key, ......)

建表后添加约束 add primary key(employee_id);

select table_name, constraint_name, constraint_type, status

from user_constraints where table_name = "表名"

--禁用/启动主键约束

alter table 表名 disable/enable primary key

外键约束 唯一性约束 检查约束 默认值约束 非空约束 略

索引——提高数据性能的机制,不会为主键的改名而自动改名,但会随着主键的删除而被删除

--游标

delcare cursor 游标名称 is 查询语句 --声明游标

变量名 类型 或者 变量名 表名.列名%type --声明列表变量

变量名 表名%rowtype --声明行变量

显式游标 open 游标名称 --打开游标

fetch 游标名称 into 变量 --通过游标抓取数据

--列变量例子

set serverout on;

declare cursor ex is select id, name, age from emp; 声明游标ex

声明三个列变量

id number;

name varchar2(20);

age number;

begin

open cu_employee;

fetch cu_employee into id, name, age;

循环输出

while cu_employee%found loop

dbms_output.put_line(id || ":" || name || ":" || age);

fetch ex into id, name, age;

end loop;

end;

--行变量获取游标信息

declare cursor ex is select * from emp;

employee employees%rowtype;

begin

open cu_employee;

fetch cu_employee into employee;

while cu_employee%found loop

dbms_output.put_line(employee.employee_id || ":" || employee.employee_name

|| ":" || employee.employee_age);

fetch ex into employee;

end loop;

close ex;

end;

--语句触发器

create trigger 触发器名称

before/after 触发动作(不能为视图创建before和after的触发器,也无法对select操作创建触发器,多个操作用or连接)

on 作用对象(数据表、视图等)

触发器操作(begin ..... end)

谓词 updating inserting deleting

--行触发器

create trigger 触发器名称

before or after or delete 触发动作

on 作用对象(数据表、视图等)

for each row

触发器操作(begin ..... end)

行触发器的变量引用 : old :new

:old存储被delete和upate操作所影响的值,:new变量用于存储insert和update操作所影响的值

instance of 触发器 替代触发动作,可以应用于视图

alter trigger 触发器名称 disable/enable

oracel允许的触发器级联数是35个

--序列

create sequence 序列名 start with 初始值 increment by 步长

alter sequence 序列名 minvalue 最小值/maxvalue 最大值

cycle 设为可循环

cache 缓存大小 存储于内存中提高效率

序列名.nextval

序列名.currval

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。