1.事务:一个或一系列的查询;

2.使用事务安全的表格类型(通过InnoDB):

①关闭自动提交: set autocommit=0;

//若自动提交被打开,须使用如下语句开始一个事务:

//  start transaction;  若自动提交是关闭的则不需要此句

②完成组成事务的语句输入后,提交给数据库:  commit;

③回到数据库之前的状态:  rollback;

④将表格转换成InnoDB表格(之前是MyISAM表格):

alter table orders type=innodb;

alter table order_items type=innodb;

//转成InnoDB表格后,需要再使用commit;语句才能完成提交到数据库的行为

3.(InnoDB下)添加外键:

要先创建一个使用外键的表格:

如:create table order_items{

……

}type=InnoDB;

再使用ALTER TABLE语句添加外键:

如:  alter table order_items type=InnoDB;

alter table order_items

add foreign key (orderid) references orders(orderid);

//orderid列是一个外键,包含orders表格中的orderid列值

4.存储:

①声明一个存储过程:

# basic_stored_procedure.sql
# Basic stored procedure example
delimiter //
# 分隔符//替换; 使得在存储过程中使用分号分隔符 create procedure total_orders (out total float)
# total_orders是存储过程名称
# out表示该参数将被传出或返回(对应的是in)
# total是被传递的参数,若有多个参数则用逗号分隔
# float是参数的类型
BEGIN
select sum(amount) into total from orders;
END
// delimiter;
# 过程声明完成,将分隔符重新设置为分号

过程声明结束后,使用call关键字:

如: call total_orders(@t);

//调用total_orders过程并传入一个用来保存结果的变量@t

//查看结果: select @t;

②声明一个存储函数:

# basic_function.sql
# Basic syntax to create a function
delimiter // create function add_tax (price float) returns float
begin
declare tax float default 0.10;
# declare用于在begin...end中声明局部变量
return price*(1+tax);
end
// delimiter;

查看结果: select add_tax(100); //100是传过去的price值

③查看定义存储过程和存储函数:

show create procedure total_orders;

show create function add_tax;

删除之:

drop procedure total_orders;

drop function add_tax;

④游标、控制结构:

# control_structures_cursors.sql

# Procedure to find the orderid with the largest amount
# could be done with max, but just to illustrate stored procedure principles
delimiter // create procedure largest_order(out largest_id int)
begin
declare this_id int; #当前行的orderid值
declare this_amount float; #当前行的amount值
declare l_amount float default 0.0; #最大的订单金额
declare l_id int; #最大订单金额对应的ID declare done int default 0; #循环标记
# 声明句柄,类似于存储过程中的一个异常
#(该句柄将在sqlstate '02000'语句被执行时调用)
declare continue handler for sqlstate '02000' set done =1;
# 游标c1,类似于一个数组从一个查询获得结果集
declare c1 cursor for select orderid, amount from orders; open c1; #open才是真正开始执行查询
repeat
fetch c1 into this_id, this_amount;
if not done then
if this_amount>l_amount then
set l_amount=this_amount;
set l_id=this_id;
end if;
end if;
until done end repeat;
close c1; set largest_id=l_id;
end
// delimiter;

调用过程: call largest_order(@l);

查看结果: select @l;

第13章 MySQL高级编程的更多相关文章

  1. 第四章 MySQL高级查询(二)

    第四章 MySQL高级查询(二) 一.EXISTS子查询 在执行create 或drop语句之前,可以使用exists语句判断该数据库对像是否存在,返回值是true或false.除此之外,exists ...

  2. 第三章 MySQL高级查询(一)

    第三章 MySQL高级查询(一) 一.SQL语言的四个分类 1.       DML(Data Manipulation Language)(数据操作语言):用来插入,修改和删除表中的数据,如INSE ...

  3. Learning Spark中文版--第六章--Spark高级编程(2)

    Working on a Per-Partition Basis(基于分区的操作) 以每个分区为基础处理数据使我们可以避免为每个数据项重做配置工作.如打开数据库连接或者创建随机数生成器这样的操作,我们 ...

  4. Learning Spark中文版--第六章--Spark高级编程(1)

    Introduction(介绍) 本章介绍了之前章节没有涵盖的高级Spark编程特性.我们介绍两种类型的共享变量:用来聚合信息的累加器和能有效分配较大值的广播变量.基于对RDD现有的transform ...

  5. MySQL基础之第13章 MySQL函数

    13.1.数学函数 随机数可能会用到,其他基本无视. 13.2.字符串函数 重点CONCAT(S1,S2….) 13.3.日期和时间函数 一.MySQL 获得当前日期时间 函数 1.1 获得当前日期+ ...

  6. 温故而知新-MySQL高级编程

    1 load data infile语句 MySQL下的命令  登录mysql命令行模式 load data infile "/var/www/1.txt" into table ...

  7. 第13章 MySQL服务器的状态--高性能MySQL学习笔记

    13.1 系统变量 -- 服务器配置变量 MySQL通过SHOW VARIABLES  SQL命令显示许多系统变量. 13.2 状态变量--SHOW STATUS SHOW STATUS 命令会在一个 ...

  8. 第12章 MySQL高级管理

    1.手动更新权限后,需向服务器指出已对权限进行修改: (在MySQL提示符下)flush privileges; 2.查看用户所拥有的权限: 如: show grants for bookorama; ...

  9. accp8.0转换教材第3章MySQL高级查询(一)理解与练习

    一.单词部分 ①constraint约束②foreign外键③references参考 ④subquery子查询⑤inner内部的⑥join连接 二.预习部分 1.修改表SQL语句的关键字是什么 RE ...

随机推荐

  1. Javascript Array API

    JS数组对象提供了很多API方法,由于前段时间要用到某一些方法,但是突然一时又想不起来该怎么用了,上网找有很多资料都不全,所以就自己整理了一篇,完全是自己写的的JS,只是复制到这里来了 ,要用到的朋友 ...

  2. PHP GD 库 缩略图 添加水印

    class cls_image { var $error_no = 0; var $error_msg = ''; //var $images_dir = IMAGE_DIR; //var $data ...

  3. [转]Resolving kernel symbols

    原:http://ho.ax/posts/2012/02/resolving-kernel-symbols/ KXLD doesn't like us much. He has KPIs to mee ...

  4. c语言:将二进制数按位输出

    问题: 1.输入int 20,其二进制为10100,按位输出10100; 2.或者将1转化为“+”,0转化为“-”,输出就是” + - + - - “; int biTofh(int bi,int l ...

  5. C#制作高仿360安全卫士窗体3

    C#制作高仿360安全卫士窗体(三)   距上篇C#制作高仿360安全卫士窗体(二)也将近一个多月了,这个月事情还是像往常一样的多.不多我也乐在其中,毕竟我做的是我喜欢做的东西.今天特地抽空把怎么制作 ...

  6. 使ie6的漂浮栏滑动右侧滚动条的时候不抖动

    body {_background-attachment: fixed;}html {_background-image: url(about:blank);}

  7. (转)JS中公共/私有变量和方法

    私有变量 在对象内部使用'var'关键字来声明,而且它只能被私有函数和特权方法访问. 私有函数 在对象的构造函数里声明(或者是通过var functionName=function(){...}来定义 ...

  8. Koala Framework

    Koala Framework是什么?我为什么要写这个框架?   当时的监管组,技术力量累积的很少,还在直连DB,使用着DataTable.DataSet作为数据的承载,监管是公司最近几年主推的项目, ...

  9. TCP/IP详解学习笔记- 概述

    TCP/IP详解学习笔记(1)-- 概述1.TCP/IP的分层结构      网络协议通常分不同层次进行开发,每一层分别负责不同的同信功能.TCP/IP通常被认为是一个四层协议系统.      如图所 ...

  10. About Spring

    “Spring is the most popular application development framework for enterprise Java.”这是Spring官方首页上的第一句 ...