一、Postgresql
1、启用Postgresql中的UUID
CREATE EXTENSION "UUID-OSSP";
SELECT uuid_generate_v4();
2、创建测试表
-- Table: sys_user
CREATE TABLE sys_user
(
id character varying(50) NOT NULL,
name character varying(50),
status character varying(20),
CONSTRAINT user_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE sys_user
OWNER TO postgres;
-- Table: sys_role
CREATE TABLE sys_role
(
id character varying(50) NOT NULL,
name character varying(50),
type character varying(21),
CONSTRAINT sys_role_pkey PRIMARY KEY (id)
)
WITH (
OIDS=FALSE
);
ALTER TABLE sys_role
OWNER TO postgres;
3、创建触发器函数
CREATE OR REPLACE FUNCTION user_function()
RETURNS trigger AS
$BODY$
declare ii integer;
declare roletype character varying(20);
begin
IF(TG_OP = 'INSERT') THEN
INSERT INTO sys_role values(uuid_generate_v4(), '角色', '0');
RETURN NEW;
ELSIF (TG_OP = 'UPDATE') THEN
IF(NEW.status = '1') THEN
INSERT INTO sys_role values(uuid_generate_v4(), '角色', '1');
RETURN NEW;
ELSIF (NEW.status = '2') THEN
ii = 1;
FOR ii IN 1..2 LOOP
INSERT INTO sys_role values(uuid_generate_v4(), '角色', '2');
END LOOP;
RETURN NEW;
ELSIF (NEW.status = '3') THEN
roletype = '';
FOR roletype IN (SELECT status FROM sys_user) LOOP
INSERT INTO sys_role values(uuid_generate_v4(), '角色', roletype);
END LOOP;
RETURN NEW;
END IF;
END IF;
RETURN NULL;
end;
$BODY$
LANGUAGE plpgsql VOLATILE
COST 100;
ALTER FUNCTION user_function()
OWNER TO postgres;
4、创建触发器(sys_user)
CREATE TRIGGER user_trigger
AFTER INSERT OR UPDATE OF status
ON sys_user
FOR EACH ROW
EXECUTE PROCEDURE user_function();
5、测试触发器
insert into sys_user values(uuid_generate_v4(), 'lisi', '0');
执行sql,sys_role表中增加一条type为0的数据。
update sys_user set status='1' where name='lisi';
执行sql,sys_role表中增加一条type为1的数据。
update sys_user set status='2' where name='lisi';
执行sql,sys_role表中增加二条type为2的数据。
update sys_user set status='3' where name='lisi';
执行sql,sys_role表中增加一条type为3的数据。
6、创建存储过程(Postgresql中存储过程和函数一样)
CREATE OR REPLACE FUNCTION user_insert()
RETURNS integer AS $total$
declare
total integer;
ii integer default 1;
BEGIN
FOR ii IN 1..2 LOOP
insert into sys_user values(uuid_generate_v4(), 'lisi'||ii, '0');
END LOOP;
SELECT count(*) into total FROM sys_user;
RETURN total;
END;
$total$ LANGUAGE plpgsql;
7、调用存储过程
select user_insert();
二、Mysql
1、创建测试表
-- ----------------------------
-- Table structure for sys_role
-- ----------------------------
DROP TABLE IF EXISTS `sys_role`;
CREATE TABLE `sys_role` (
`id` varchar(50) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`type` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
-- ----------------------------
-- Table structure for sys_user
-- ----------------------------
DROP TABLE IF EXISTS `sys_user`;
CREATE TABLE `sys_user` (
`id` varchar(50) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`status` varchar(20) DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2、创建触发器
实列一
create trigger user_insert_trigger
after insert on sys_user
for each row
begin
INSERT INTO sys_role values(uuid(), '角色', '0');
end;
实列二
create trigger user_update_trigger
after update on sys_user
for each row
begin
declare i int default 0;
if new.status='1' then
INSERT INTO sys_role values(uuid(), '角色', '1');
elseif new.status='2' then
while i < 2 do
INSERT INTO sys_role values(uuid(), '角色', '2');
set i=i+1;
end while;
end if;
end;
3、删除触发器
drop trigger user_insert_trigger;
drop trigger user_update_trigger;
4、测试触发器
insert into sys_user values(uuid(), 'lisi', '0');
执行sql,sys_role表中增加一条type为0的数据。
update sys_user set status='1' where name='lisi';
执行sql,sys_role表中增加一条type为1的数据。
update sys_user set status='2' where name='lisi';
执行sql,sys_role表中增加二条type为2的数据。
5、创建存储过程
create procedure user_procedure()
begin
DECLARE name VARCHAR(32);
DECLARE status VARCHAR(32);
DECLARE s int DEFAULT 0;
DECLARE user CURSOR FOR SELECT a.name, a.status from sys_user a ;
DECLARE CONTINUE HANDLER FOR SQLSTATE '02000' SET s=1;
open user;
fetch user into name, status;
while s<>1 do
INSERT INTO sys_role VALUES(uuid(), 'role', '5');
fetch user into name, status;
end while;
close user;
end;
6、删除存储过程
drop PROCEDURE user_procedure;
7、调用存储过程
call user_procedure();
三、Oracle
1、创建测试表
CREATE TABLE "SYS_USER"
(
"ID" VARCHAR2(50),
"NAME" VARCHAR2(50),
"STATUS" VARCHAR2(20),
CONSTRAINT "SYS_USER_PRIMARY_KEY" PRIMARY KEY ("ID")
)
CREATE TABLE "SYS_ROLE"
(
"ID" VARCHAR2(50),
"NAME" VARCHAR2(50),
"TYPE" VARCHAR2(20),
CONSTRAINT "SYS_ROLE_PRIMARY_KEY" PRIMARY KEY ("ID")
)
2、创建触发器
实例一
create or replace trigger user_trigger
after insert on sys_user
begin
insert into sys_role values((select sys_guid() from dual), '角色', '0');
end;
实列二
CREATE OR REPLACE TRIGGER "USER_TRIGGER"
after insert or update or delete on sys_user
for each row
declare
cursor c_role
is
select * from sys_role;
temp_row c_role%rowtype;
begin
if inserting then
insert into sys_role values((select sys_guid() from dual), '角色', '0');
elsif updating then
if :new.status='1' then
insert into sys_role values((select sys_guid() from dual), '角色', '1');
elsif :new.status='2' then
insert into sys_role values((select sys_guid() from dual), '角色', '2');
elsif :new.status='3' then
for temp_row in c_role loop
insert into sys_role values((select sys_guid() from dual), '角色', temp_row.type);
end loop;
end if;
elsif deleting then
delete from sys_role where type='3';
end if;
end;
3、删除触发器
drop trigger user_trigger;
4、测试触发器
insert into sys_user values((select sys_guid() from dual), 'lisi', '0');
执行sql,sys_role表中增加一条type为0的数据。
update sys_user set status='1' where name='lisi';
执行sql,sys_role表中增加一条type为1的数据。
update sys_user set status='2' where name='lisi';
执行sql,sys_role表中增加二条type为2的数据。
update sys_user set status='3' where name='lisi';
执行sql,sys_role表中所有数据重新插入。
5、游标介绍
FOR循环游标测试
declare
cursor c_user
is
select *
from sys_user;
c_row c_user%rowtype;
begin
for c_row in c_user loop
dbms_output.put_line(c_row.id||'-'||c_row.name||'-'||c_row.status);
end loop;
end;
Fetch游标测试
declare
cursor c_user
is
select * from sys_user;
c_row c_user%rowtype;
begin
open c_user;
loop
fetch c_user into c_row;
exit when c_user%notfound;
dbms_output.put_line(c_row.id||'-'||c_row.name||'-'||c_row.status);
end loop;
close c_user;
end;
6、创建存储过程
create or replace procedure user_insert
as
begin
for v_temp in 1..2 loop
insert into sys_user values((select sys_guid() from dual), 'lisi'||v_temp, '0');
end loop;
end;
7、调用存储过程
方式一
begin
user_insert;
end;
方式二-命令
execute user_insert;