SQL/PLSQL
select upper('John');
select md5(MD5('123456'));
select id, name, salary + ifnull(bonus, 0) + ifnull(mobileBonus, 0) + ifnull(hotbonus, 0) + ifnull(allbonus, 0) + ifnull(oilbonus, 0) as salary
from employee;
set global log_bin_trust_function_creators=TRUE;
drop function allSalary;
create function allSalary(v_id int) returns int
begin
declare v_salary int;
select salary + ifnull(bonus, 0) + ifnull(mobileBonus, 0) +
ifnull(hotbonus, 0) + ifnull(allbonus, 0) + ifnull(oilbonus, 0) into v_salary
from employee where id = v_id;
return v_salary;
end;
select allSalary(2) ;
create function salary(salary int, bonus int, mobileBonus int,
hotbonus int, allbonus int, oilbonus int) returns int
begin
return salary + ifnull(bonus, 0) + ifnull(mobileBonus, 0) +
ifnull(hotbonus, 0) + ifnull(allbonus, 0) + ifnull(oilbonus, 0);
end;
select id, name, salary(salary, bonus, mobileBonus, hotbonus, allbonus, oilbonus) as salary
from employee;
drop function transferFun;
create function transferFun(myCode varchar(20), a_money double, tagetCode varchar(20)) returns int
begin
declare v_flag int default 0;
declare v_money int default 0;
declare v_targetId int default 0;
select money into v_money from account where id = myCode;
if v_money > a_money then
update account set money = money - a_money where id = mycode;
select id into v_targetId from account where id = tagetCode;
if v_targetId = 0 then
set v_flag = 2;
else
update account set money = money + a_money where id = tagetCode;
end if;
else
set v_flag = 1;
end if;
return v_flag;
end;
start transaction;
select transfer(2, 20000, 3);
commit;
rollback;
drop PROCEDURE transferPro;
create procedure transferPro(myCode int, tagetCode int,
a_money double, out v_flag int)
begin
declare v_money int default 0;
declare v_targetId int default 0;
select money into v_money from account where id = myCode;
if v_money > a_money then
select id into v_targetId from account where id = tagetCode;
if v_targetId = 0 then
set v_flag = 2;
else
update account set money = money - a_money where id = mycode;
update account set money = money + a_money where id = tagetCode;
set v_flag = 3;
end if;
else
set v_flag = 1;
end if;
end;
start transaction;
call transferPro(3, 20000, 2, @flag);
select @flag;
commit;
rollback;
create procedure itemPages(v_sid int, v_pageIndex int, v_pageNum int, out v_count long, out v_pages int)
begin
select count(*) into v_count from item where sid = v_sid;
select ceil(v_count / v_pageNum) into v_pages;
set v_pageIndex = (v_pageIndex - 1) * v_pageNum;
select * from item where sid = v_sid order by id desc limit v_pageIndex, v_pageNum;
end;
call itemPages(4, 1, 20, @count, @pages);
select @count;
select @pages;
drop procedure addAll;
create procedure addAll()
begin
declare v_id int;
declare v_name varchar(255);
declare v_salary int;
declare v_addMoney int;
declare done int default false;
declare allEmp cursor for select id, name, salary from employee;
declare continue handler for not found set done = true;
open allEmp;
fetch allEmp into v_id, v_name, v_salary;
while not done do
if v_salary > 10000 then
set v_addMoney = 1500;
elseif v_salary > 8000 then
set v_addMoney = 1200;
elseif v_salary > 5000 then
set v_addMoney = 1000;
elseif v_salary > 3000 then
set v_addMoney = 800;
else
set v_addMoney = 500;
end if;
update employee set salary = salary + v_addMoney where id = v_id;
fetch allEmp into v_id, v_name, v_salary;
end while;
close allEmp;
end;
call addAll();
create trigger upadteAccount
before update on account
for each row
begin
if old.id <= 5 then
insert into accountBak(id, money, adate, aid, updateDate)
values(old.id, old.money, old.adate, old.aid, now());
end if;
end;
create trigger deleteAccount
before delete on account
for each row
begin
insert into accountBak(id, money, adate, aid, updateDate)
values(old.id, old.money, old.adate, old.aid, now());
end;
start transaction;
update account set money = money + 800000000 where id = 3;
delete from account where id = 3;
commit;
rollback;
delete from item where id > 20;
select i.id, i.name, i.salePrice, s.`name` as smallKindName
from item i, smallKind s
where i.sid = s.sid;
insert into itemView (select i.id, i.name, i.salePrice, s.`name` as smallKindName
from item i, smallKind s
where i.sid = s.sid);
select * from itemView;
create trigger itemTrigger
after insert on item
for each row
begin
delete from itemView;
insert into itemView (select i.id, i.name, i.salePrice, s.`name` as smallKindName
from item i, smallKind s
where i.sid = s.sid);
end;
insert