事项开启和使用
//修改表的引擎alter table a engine=myisam;//开启事务begin;//关闭自动提交set autocommit=0;//扣100update bank set money=money-100 where bid=1;//回滚,begin开始的所有sql语句操作rollback;//开启事务begin;//关闭自动提交set autocommit=0;//扣100update bank set money=money-100 where bid=1;//加100update bank set money=money+100 where bid=2;//提交commit;
实例操作
$dsn = "mysql:host=127.0.0.1;dbname=c58";try { //通过pdo连接数据库 $pdo = new Pdo($dsn,'root',''); //把错误设置成异常模式,才能try catch接收 $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); //设置字符集 $pdo->query("SET NAMES utf8"); //开启事务 $pdo->query("BEGIN"); //关闭自动提交 $pdo->query("SET AUTOCOMMIT=0"); //转账 //扣掉100 $pdo->exec('UPDATE bank SET money=money-100 WHERE bid=1'); //加上100 $pdo->exec('UPDATE bank SET money=money+100 WHERE bid=2'); //提交 $pdo->query('COMMIT');} catch (PDOException $e) { $pdo->query('ROLLBACK'); echo $e->getMessage();}
注释:事项可以帮助我们更安全的操作数据
视图的创建删除和使用
//1.创建视图create view bankview as select bid,bname from bank;//2.查看视图show table status where comment='VIEW';//3.修改视图alter view bankview as select bid from bank;//4.删除视图drop view bankview;
存储过程的创建删除查询和使用
//更变边界符
//更变边界符/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事项,视图,函数,触发器命令(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持VeVb武林网。
新闻热点
疑难解答