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

所属分类: 数据库 / Mysql 阅读数: 1212
收藏 0 赞 0 分享

事项开启和使用

//修改表的引擎
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多表连接查询实例讲解

本篇文章中给大家通过实例代码讲述了mysql多表连接查询的方法,有需要的朋友们可以参考学习下。
收藏 0 赞 0 分享

MySQL设置global变量和session变量的两种方法详解

这篇文章主要介绍了MySQL设置global变量和session变量的两种方法,每种方法给大家介绍的非常详细 ,需要的朋友可以参考下
收藏 0 赞 0 分享

8种手动和自动备份MySQL数据库的方法

作为流行的开源数据库管理系统,MySQL的使用者众多,为了维护数据安全性,数据备份是必不可少的。本文就为大家介绍几种适用于企业的数据备份方法,需要的朋友可以参考下
收藏 0 赞 0 分享

使用JDBC连接Mysql数据库会出现的问题总结

这篇文章主要给大家介绍了关于使用JDBC连接Mysql数据库会出现的问题的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

Ubuntu中MySQL的参数文件my.cnf示例详析

这篇文章主要给大家介绍了关于Ubuntu中MySQL的参数文件my.cnf的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用mysql具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享

解决启动MongoDB错误:error while loading shared libraries: libstdc++.so.6:cannot open shared object file:

本文提供了解启动MongoDB时提示:error while loading shared libraries: libstdc++.so.6: cannot open shared object file: 错误的解决方案
收藏 0 赞 0 分享

PHP定时备份MySQL与mysqldump语法参数详解

本文为大家介绍了PHP利用mysqldump命令定时备份MySQL与mysqldump语法参数大全以及定时备份的PHP实例代码
收藏 0 赞 0 分享

定时备份 Mysql并上传到七牛的方法

常见的 MySQL 数据备份方式有,直接打包复制对应的数据库或表文件(物理备份)、mysqldump 全量逻辑备份、xtrabackup 增量逻辑备份等。这篇文章主要介绍了定时备份 MySQL 并上传到七牛 ,需要的朋友可以参考下
收藏 0 赞 0 分享

MySQL锁(表锁,行锁,共享锁,排它锁,间隙锁)使用详解

本文全面讲解了MySQL中锁包括表锁,行锁,共享锁,排它锁,间隙锁的详细使用方法
收藏 0 赞 0 分享

MySQL中的排序函数field()实例详解

这篇文章主要给大家介绍了关于MySQL中排序函数field()的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
收藏 0 赞 0 分享
查看更多