wordpress整站搬家首页空白问题,什么网站可以做片头,网站建设 青海,郑州网站制作报价假设你在开发一个应用#xff0c;应用有一个数据库#xff0c;你要在哪里写SQL语句#xff1f;你不会在你的应用代码里写语句#xff0c;它会让你的应用代码很混乱且难以维护。具体在哪里呢#xff1f;在存储过程中或函数中。存储过程是一组为了完成特定功能的SQL语句集合…假设你在开发一个应用应用有一个数据库你要在哪里写SQL语句你不会在你的应用代码里写语句它会让你的应用代码很混乱且难以维护。具体在哪里呢在存储过程中或函数中。存储过程是一组为了完成特定功能的SQL语句集合经编译存储在数据库中常用于执行复杂的业务逻辑和事务处理创建一个存储过程 -- 创建一个存储进程delimiter $$
create procedure get_invoices_balance()
beginselect *from invoiceswhere invoice_total-payment_total0;
end$$delimiter ;
-- 使用 $$ 改变默认分隔符告诉MySQL$$之间是一个整体end$$结束call get_clients() -- call 语句 可以调用 存储进程 删除存储过程 drop procedure if exists 名字;参数 我们一般使用参数为存储过程传递值我们也可以使用参数为调用程序赋值 -- 参数
drop procedure if exists get_clients_by_state;
delimiter $$
create procedure get_clients_by_state(state char(2))
beginselect * from clients cwhere c.state state;
end $$
delimiter ;call get_clients_by_state(ca) 带默认值的参数 存储过程调用者无法提供参数我们为参数配置默认值 -- 提供默认参数
drop procedure if exists get_clients_by_state;
delimiter $$
create procedure get_clients_by_state(state char(2))
beginif state is null then set stateca;end if ; -- 表示 if 语句结束因为if语句可以是多个select * from clients cwhere c.state state;
end $$
delimiter ;call get_clients_by_state(null)-- 使用 if - else
drop procedure if exists get_clients_by_state;
delimiter $$
create procedure get_clients_by_state(state char(2))
beginif state is null then select * from clients;else select * from clients c where c.statestate;end if;
end $$
delimiter ;call get_clients_by_state(null)-- 使用 ifnull()
drop procedure if exists get_payments;
delimiter $$
create procedure get_payments(client_id int,payment_method_id tinyint)
beginselect *from payments pwhere p.client_idifnull(client_id,p.client_id) and p.payment_methodifnull(payment_method_id,p.payment_method);
end$$
delimiter ;call get_payments(null,null); 参数验证 当我们使用存储进程来插入、更新、删除数据时我们要进行参数验证确保我们的过程不会意外地往数据库存储错误数据。 -- 参数验证
drop procedure if exists make_payment;
delimiter $$
create procedure make_payment
( invoice_id int,payment_amount decimal(9,2),payment_date date)
beginif payment_amount0 then signal sqlstate Data Exceptionset message_text 不合理; -- 错误抛出异常,终止执行 end if ;update invoices i set i.payment_totalpayment_amount,i.payment_datepayment_datewhere i.invoice_idinvoice_id;
end$$
delimiter ;call make_payment(1, -20, 2019-03-10);输出参数 我们可以 使用 参数 给 调用程序 返回 值 drop procedure if exists get_unpaid_invoices_for_client;
delimiter $$
create procedure get_unpaid_invoices_for_client (client_id int,out invoices_count int, -- 标记输出参数out invoices_total decimal(9,2))
beginselect count(*),sum(invoice_total)into invoices_count,invoices_total -- 读取数据复制到这些输出参数上from invoices iwhere i.client_idclient_id and payment_total0;
end$$
delimiter ;-- 可以使用 MYSQL工作台提供的图形化工具更简便
set invoices_count 0; -- 用户变量
set invoices_total 0;
call sql_invoicing.get_unpaid_invoices_for_client(3, invoices_count, invoices_total);
select invoices_count, invoices_total; 变量 1.用户变量(会话变量) 调用有输出参数的存储过程时使用这些变量通过传递这些变量来获取输出参数值set 语句定义变量set invoices_count02.本地变量 在存储过程或函数中的变量一旦结束就被清空declare 语句声明变量 -- 本地变量
drop procedure if exists get_risk_factor;
delimiter $$
create procedure get_risk_factor()
begindeclare riskfactor decimal(9,2) default 0;declare invoices_total decimal;declare invoices_count int;select count(*),sum(invoice_total)into invoices_count,invoices_totalfrom invoices;set riskfactorinvoices_total/invoices_count*5;select riskfactor;
end$$
delimiter ; 函数 函数只能返回单一值通常用于计算和转换数据 -- 设置函数
drop function if exists get_risk_factor_for_client;
delimiter $$
create function get_risk_factor_for_client (client_id int)
returns int
-- 设置函数属性确定性能读能更改
-- deterministic
-- modifies sql data
reads sql data
begin
declare riskfactor decimal(9,2) default 0;declare invoices_total decimal;declare invoices_count int;select count(*),sum(invoice_total)into invoices_count,invoices_totalfrom invoices iwhere i.client_idclient_id;set riskfactorinvoices_total/invoices_count*5;return ifnull(riskfactor,0);
end$$
delimiter ;-- 正常内置函数使用
select client_id,name,get_risk_factor_for_client(client_id)
from clients; 其他约定 驼峰命名法下划线命名法