2021年11月22日
- 在--safe-updates模式下,更新记录,在有where语句的同时增加limit语句,limit不是limit=10,而是limit 10,代码如下:
update tb set sales=777 where sales=54 limit 10;
- 视图只是用户从基表中随意手机的列。对视图执行insert和update有限制,如果是使用了union、join、子查询的视图不能执行inset和update,但是如果只是从一个表中提取了列,那么inset和update没有问题。
2021年11月23日
- 在create view过程中增加with check option来防止误操作,是一个良好的习惯,防止不符合where条件的记录被插入,不接受与条件不匹配的记录。代码如下:
create view v4
as
select empid,sales
from tb
where sales>=100
with check option;
以上代码防止输入sales小于100的记录,如果输入insert into v4 values('恶意刁难',50);会出现报错。
- 存储过程时将一系列步骤归纳并存储起来的集合。看似方便,但是在存储了重要数据的数据库中,执行没有经过充分验证的存储过程是非常危险的,这一点一定要牢记。只有在5.0或更高的版本中才能使用存储过程,查看版本方法如下:
select version();
- 在创建存储过程时,需要先改变环境变量,事先将分隔符“;”变为“//”,在end滞后输入“//”就会执行create procedure,在存储过程创建结束后,使用“delimiter;”将分隔符恢复原始设置。
- 存储过程名后面要加(),作为参数放在存储过程中。
2021年11月25日
默认条件下log_bin_function_creators的初始值是0,这时不能使用存储函数,在设置为1是才可以使用存储函数。存储函数有可能对复制和数据的恢复产生影响。 查看log_bin_function_creators开启状态,查看二进制日志功能的环境:
show variables like 'log_bin_function_creators';
修改log_bin_function_creators=1,设置该功能为on
set global log_bin_function_creators=1';
在重启MySQL后需要重新启动log_bin_function_creators功能。
2021年11月26日
- 在创建存储函数时定义返回值数据类型使使用returns在,在返回值时使用return没有s,又浪费了这么长的时间找错误。代码如下:
delimiter //
create function fu2() returns double
begin
declare r double;
select avg(sales) into r from tb;
return r;
end
//
delimiter ;