Oracle 建立序列以及触发器的建立
序列:序列的创建方法,以及插入数据时的使用; --序列的创建create sequence sqincrement by 1start with 1maxvalue 10minvalue
序列:序列的创建方法,以及插入数据时的使用;
--序列的创建
create sequence sq
increment by 1
start with 1
maxvalue 10
minvalue 1
cycle
cache 5
--一般(一个序列可以用在多张表,但是一般情况下,一张表对应一个序列)
create sequence sq
increment by 1
start with 1
nocache
nocycle
--使用序列的方法
insert into emp(empno,ename)
values(sq.nextval,'Tim');
--查看数据
select * from emp;
触发器:
--触发器:特殊的存储过程。
--特点:无法直接手动调用,只能自动触发(由一个动作去触发)。
--类型:dml触发器、instead of替代触发器、系统触发器
--dml触发器
--1、语句级(执行操作语句时,只触发一次)
--语法:
create or replace trigger tri_XXX
动作 on 表
declare
......
begin
......
end;
--例子:给30号部门的员工集体涨工资200元(触发一个语句级触发器)。
--建立触发器(类似于先把存储过程建好,等着被调用)
create or replace trigger tri_update_emp
after update on emp --修改动作完成后执行触发器
begin
dbms_output.put_line('涨工资了......');
end;
--触发
update emp set sal=sal+200 where deptno=30;
--建立触发器(类似于先把存储过程建好,等着被调用)
create or replace trigger tri_update_emp
before update on emp --修改之前触发
begin
dbms_output.put_line('涨工资了......');
end;
--触发
update emp set sal=sal+200 where deptno=30;
--2、行级(没修改一行,都要触发一次)
create or replace trigger tri_update_emp
after update on emp --修改动作完成后执行触发器
for each row
begin
dbms_output.put_line('涨工资了......');
end;
--触发
update emp set sal=sal+200 where deptno=30;
--例子
create or replace trigger tri_up_del_ins_emp
after delete or insert or update on emp --修改动作完成后执行触发器
for each row
begin
dbms_output.put_line('触发了......');
end;
--触发
delete from emp where deptno=30;
--条件谓词(布尔类型):inserting 、updating、 deleting
create or replace trigger tri_up_del_ins_emp
after delete or insert or update on emp --修改动作完成后执行触发器
for each row
begin
if inserting then
dbms_output.put_line('又来新人了,oo......');
elsif updating then
dbms_output.put_line('修改了,能行不......');
else
dbms_output.put_line('被开除了......');
end if;
end;
--触发
delete from emp where deptno=30;
--实例:简易图书管理系统
select * from book;
select * from borrow;
--增加一列
alter table book
add countOfBook integer check(countOfBook>=0);
--完成借书功能,需要borrow表插入一行,book表对应书籍库存-1。
--问题1、触发器建在哪个表上? borrow
--问题2、怎么把插入borrow表的数据传给book? :NEW
--*****行级触发器,自带了两个特殊变量
-- :new --自动存放新插入的数据记录(一行数据),和修改之后的记录行
-- :old --自动存放被删除的数据记录(一行数据),和修改之前的记录行
--例子
create or replace trigger tr_up_emp
after update on emp
for each row
begin
dbms_output.put_line(:old.ename||:old.sal);
dbms_output.put_line(:new.ename||:new.sal);
end;
--触发
update emp set ename='ao-smith',sal=250
where ename='SMITH';
SELECT * FROM book;
--实现借书功能
--第一步: 在borrow上建立触发器,,用来自动修改book表
create or replace trigger tri_in_borrow
after insert on borrow
for each row
begin
update book set countOfBook=countOfBook-1
where bid=:new.bid;
end;
--第二步:只需在borrow中插入数据就OK
insert into borrow
values('T013','1002','B003',sysdate,null);
--作业:P322 11、12, 上面例子中的借书功能
更多详情见请继续阅读下一页的精彩内容:
相关阅读:
Oracle触发器的使用
Oracle触发器给表自身的字段重新赋值出现ORA-04091异常
Oracle创建触发器调用含参数存储过程
Oracle触发器查询统计本表

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

This article explores optimizing MySQL memory usage in Docker. It discusses monitoring techniques (Docker stats, Performance Schema, external tools) and configuration strategies. These include Docker memory limits, swapping, and cgroups, alongside

This article addresses MySQL's "unable to open shared library" error. The issue stems from MySQL's inability to locate necessary shared libraries (.so/.dll files). Solutions involve verifying library installation via the system's package m

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

This article compares installing MySQL on Linux directly versus using Podman containers, with/without phpMyAdmin. It details installation steps for each method, emphasizing Podman's advantages in isolation, portability, and reproducibility, but also

This article provides a comprehensive overview of SQLite, a self-contained, serverless relational database. It details SQLite's advantages (simplicity, portability, ease of use) and disadvantages (concurrency limitations, scalability challenges). C

This guide demonstrates installing and managing multiple MySQL versions on macOS using Homebrew. It emphasizes using Homebrew to isolate installations, preventing conflicts. The article details installation, starting/stopping services, and best pra

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]
