MySQL复习08-级联删除置空、存储过程和函数
发布日期:2021-05-10 22:22:08 浏览次数:28 分类:精选文章

本文共 1637 字,大约阅读时间需要 5 分钟。

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;"    
上一篇:MySQL复习09-触发器
下一篇:MySQL复习07-savepoint、视图

发表评论

最新留言

路过,博主的博客真漂亮。。
[***.116.15.85]2025年04月19日 11时25分08秒