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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

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



Full table scanning may be faster in MySQL than using indexes. Specific cases include: 1) the data volume is small; 2) when the query returns a large amount of data; 3) when the index column is not highly selective; 4) when the complex query. By analyzing query plans, optimizing indexes, avoiding over-index and regularly maintaining tables, you can make the best choices in practical applications.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Yes, MySQL can be installed on Windows 7, and although Microsoft has stopped supporting Windows 7, MySQL is still compatible with it. However, the following points should be noted during the installation process: Download the MySQL installer for Windows. Select the appropriate version of MySQL (community or enterprise). Select the appropriate installation directory and character set during the installation process. Set the root user password and keep it properly. Connect to the database for testing. Note the compatibility and security issues on Windows 7, and it is recommended to upgrade to a supported operating system.

The difference between clustered index and non-clustered index is: 1. Clustered index stores data rows in the index structure, which is suitable for querying by primary key and range. 2. The non-clustered index stores index key values and pointers to data rows, and is suitable for non-primary key column queries.

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

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.
