您的位置:首页 > 数据库 > MySQL

Mysql事项,视图,函数,触发器命令(详解)

2016-11-18 09:01 656 查看

事项开启和使用

//修改表的引擎
alter table a engine=myisam;
//开启事务
begin;
//关闭自动提交
set autocommit=0;
//扣100
update bank set money=money-100 where bid=1;
//回滚,begin开始的所有sql语句操作
rollback;
//开启事务
begin;
//关闭自动提交
set autocommit=0;
//扣100
update bank set money=money-100 where bid=1;
//加100
update 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 data
begin
return concat('hello ',s,' !');
end
$
//调用
select hello('hdw')$
+--------------+
| hello('hdw') |
+--------------+
| hello hdw ! |
+--------------+
//删除
drop function hello$
//创建存储函数
create function getcid(n char(20) charset utf8)
returns int
reads sql data
begin
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 class
for each row
begin
delete from stu where cid=old.cid;
end
$
//触发器作业
创建文章表含标题、作者、发布时间字段
如果只添加了标题,发布时间字段自动设置为当前时间,
作者字段设置为123网
\d $
create trigger this_name before insert on this_table for each row
begin
if new.uname is null then
set new.uname='123';
end if;
if new.timer is null then
set new.timer=unix_timestamp(now());
end if;
end
$
//查询已有触发器
show triggers;

注释:触发器是设置好当执行某一个行为时执行另一个方法!

以上这篇Mysql事项,视图,函数,触发器命令(详解)就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  mysql 触发器 命令