基础Sql语句之三

2018-03-24  本文已影响0人  szn好色仙人
使用存储过程
使用游标


使用触发器
delimiter |

create table if not exists t1 (id int auto_increment, dealstr 
    varchar(1024), oldvalue int default null, newvalue int default null,
    primary key(id))|
delete from t0|

create trigger tg0 before insert on t0 for each row 
begin
set new.value = new.value + 1;
insert into t1(dealstr, newvalue) values("before insert", new.value);
end|
create trigger tg1 after insert on t0 for each row insert into t1(dealstr,
    newvalue) values("after insert", new.value)|
insert into t0(name, value) values ("szn", 11)|

create trigger tg2 before update on t0 for each row
begin
set new.value = new.value + 2;
insert into t1(dealstr, oldvalue, newvalue) values("before update", 
    old.value, new.value);
end|
create trigger tg3 after update on t0 for each row insert into t1(dealstr,
    oldvalue, newvalue) values("after update", old.value, new.value)|
update t0 set value = 20 where name = "szn"|

create trigger tg4 before delete on t0 for each row insert into t1(dealstr,
    oldvalue) values("before delete", old.value)|
create trigger tg5 after delete on t0 for each row insert into t1(dealstr,
    oldvalue) values("after delete", old.value)|
delete from t0 where name = "szn"|

drop trigger tg0|
drop trigger tg1|
drop trigger tg2|
drop trigger tg3|
drop trigger tg4|
drop trigger tg5|

select * from t1|
drop table t1|

delimiter ;


管理事务处理
create table if not exists t0(id int auto_increment, name varchar(100),
    value int, primary key(id)) engine = innodb;

start transaction;
insert into t0 (name, value) values("szn", 1);
rollback;

select * from t0;
-- 结果是空的(注释格式:-- 空格必须打)
drop table t0;
create table if not exists t0(id int auto_increment, name varchar(100), 
    value int, primary key(id)) engine = innodb;

start transaction;
insert into t0 (name, value) values("szn", 1);
insert into t0 (name, value) values("szn", 2);
commit;

select * from t0;
-- 返回两条新插入的行
drop table t0;
create table if not exists t0(id int auto_increment, name varchar(100), 
    value int, primary key(id)) engine = innodb;

start transaction;
insert into t0 (name, value) values("szn", 1);

savepoint sp;
insert into t0 (name, value) values("szn", 2);
rollback to sp;

select * from t0;
-- 只有第一次inser生效
drop table t0;
全球化与本地化
show variables like 'character%';
show variables like 'collation%';
安全管理
use mysql;
select user, host from user;
上一篇下一篇

猜你喜欢

热点阅读