事项开启和使用
实例操作
注释:事项可以帮助我们更安全的操作数据
视图的创建删除和使用
存储过程的创建删除查询和使用
//更变边界符
//更变边界符\d $//创建存储过程create procedure get_bid(inout n char(20) charset utf8)begin select bid from bank where name=n;end$//调用set @name='震'$call get_bid(@name)$//存储过程作业//1. 创建删除班级的存储过程//2. 实现删除班级时一并删除此班级中的学生//3. 调用方式call del_class(1);//创建表create table class( cid int unsigned primary key auto_increment, cname char(20) not null default '');create table stu( sid int unsigned primary key auto_increment, sname char(20) not null default '', cid int unsigned not null default 0);\d $create procedure del_class(inout id smallint)begin delete from class where cid=id; delete from stu where cid=id;end$set @id=1$call del_class(@id)$//1.in(输出外面传入的值,不能改变外面传入的值)create procedure a(in id int)begin select id; set id=100;end$//2.out(不可以输出外面传入的值,能改变外面传入的值)create procedure b(out id int)begin select id; set id=100;end$//3.inout(综合上述两种情况)create procedure insert_data(in num int)begin while num > 0 do insert into class set cname=num; set num = num - 1; end while;end$//查看状态show procedure status;//删除get_bid这个存储过程drop procedure get_bid;存储函数创建删除和使用
//创建create function hello(s char(20) charset utf8)returns char(50)reads sql databegin return concat('hello ',s,' !');end$//调用select hello('hdw')$+--------------+| hello('hdw') |+--------------+| hello hdw ! |+--------------+//删除drop function hello$//创建存储函数create function getcid(n char(20) charset utf8)returns intreads sql databegin return (select cid from stu where sname=n);end$//存储函数可以用在sql语句中select cname from class where cid=getcid('小猫')$触发器创建删除和使用
//删除班级自动触发删除学生create trigger del_class_stu after delete on classfor each rowbegin delete from stu where cid=old.cid;end$//触发器作业创建文章表含标题、作者、发布时间字段如果只添加了标题,发布时间字段自动设置为当前时间,作者字段设置为123网\d $create trigger this_name before insert on this_table for each rowbeginif new.uname is null thenset new.uname='123';end if;if new.timer is null thenset new.timer=unix_timestamp(now());end if;end$//查询已有触发器show triggers;注释:触发器是设置好当执行某一个行为时执行另一个方法!
以上这篇Mysql事项,视图,函数,触发器命令(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。