1. 级联删除、置空
// 设置maintable与从表foreigntable级联删除 "alter table maintable add foreign key(fk) references foreigntable(id) on delete cascade"
// 设置maintable与从表foreigntable级联置空 "alter table maintable add foreign key(fk) references foreigntable(id) on delete set null"
2. 存储过程和函数
2.1 用户变量和局部变量
// 声明用户变量并赋值 "set @count = 0"
// 局部变量只能在begin..end语句使用 "declare m int default 1"
2.2 存储过程
// 创建存储过程 "create procedure procname(参数) begin...end"
// 示例:使用存储程序插入数据 "delimiter $create procedure mypro()begin insert into stuinfo(stuname,`password`) values('jhon','adssdafexg1251'),('smis','asdfdcccg425');end $call mypro() $delimiter ;"
// 删除存储程序 "drop procedure 过程名;"
2.3 存储过程使用
# 创建存储程序实现传入日期转化成固定格式输出。 # 使用dateformat或str_to_date函数,可参考以下链接:链接至更多信息。 "delimiter $CREATE PROCEDURE mystrtodate(IN myStr VARCHAR(20), IN modle VARCHAR(20), OUT myDate VARCHAR(20))BEGIN DECLARE m DATETIME DEFAULT NULL; # 局部变量,可改用用户变量 SELECT STR_TO_DATE(myStr,modle) INTO m; SELECT DATE_FORMAT(m,'%Y/%m/%d') INTO myDate; END $SELECT @myDate ; # 用户变量CALL mystrtodate('06/07/2019','%m/%d/%Y', @myDate);SELECT @myDate;"
3. 函数
// 创建函数 "create function myfun(参数) returns 类型begin...end"
# 示例:返回公司员工人数 "create function myfun() returns intbegin declare cou int default 0; select count(*) into cou from employees; return c; end"
// 删除函数 "drop function myfun;"