热门标签 | HotTags
当前位置:  开发笔记 > 后端 > 正文

mysql学习记录(十)--存储过程_MySQL

mysqlusetest1;ReadingtableinformationforcompletionoftableandcolumnnamesYoucanturnoffthisfeaturetogetaquickerstartupwith-ADatabasechangemysql
mysql> use test1;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database change

mysql> select * from emp;
+------------+----------+------+--------+
| ename      | hiredate | sal  | deptno |
+------------+----------+------+--------+
| aaaaa      | NULL     | NULL |      1 |
| cccccccccc | NULL     | NULL |      2 |
| ddddddddd  | NULL     | NULL |      3 |
| ffffff     | NULL     | NULL |      4 |
| ggg        | NULL     | NULL |      5 |
| a1         | NULL     | NULL |      5 |
+------------+----------+------+--------+
6 rows in set (0.00 sec)

mysql> show create table emp \G;
*************************** 1. row ***************************
       Table: emp
Create Table: CREATE TABLE `emp` (
  `ename` varchar(10) DEFAULT NULL,
  `hiredate` date DEFAULT NULL,
  `sal` decimal(10,2) DEFAULT NULL,
  `deptno` int(2) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql> DELIMITER &&  
mysql> CREATE  PROCEDURE  num_from_employee (IN input_deptno int, OUT count_num INT )  
    -> READS SQL DATA  
    ->     BEGIN  
    ->     SELECT  COUNT(*) FROM emp WHERE  deptno=input_deptno ;  
    -> END  &&
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;

mysql> call num_from_employee(5,@a);
+----------+
| COUNT(*) |
+----------+
|        2 |
+----------+
1 row in set (0.02 sec)

Query OK, 0 rows affected (0.02 sec)

mysql> call num_from_employee(1,@a);
+----------+
| COUNT(*) |
+----------+
|        1 |
+----------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> create table inventory(
    -> film_id int(11),
    -> store_id int(11),
    -> inventory_in_stock varchar(50)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Query OK, 0 rows affected (0.02 sec)

mysql> insert into inventory(film_id,store_id,inventory_in_stock) values  (1,2,'aaaaaaaa'), (3,4,'bbbb'), (5,6,'cccccccccc'), (7,8,'dddddd'), (9,10,'fff');
Query OK, 5 rows affected (0.00 sec)
Records: 5  Duplicates: 0  Warnings: 0
 
mysql> select * from inventory;
+---------+----------+--------------------+
| film_id | store_id | inventory_in_stock |
+---------+----------+--------------------+
|       1 |        2 | aaaaaaaa           |
|       3 |        4 | bbbb               |
|       5 |        6 | cccccccccc         |
|       7 |        8 | dddddd             |
|       9 |       10 | fff                |
+---------+----------+--------------------+
5 rows in set (0.00 sec)

mysql> delimiter $$
mysql> create procedure film_in_stock(in p_film_id int,in p_store_id int,out p_film_count int) 
    -> reads sql data
    -> begin
    ->   select film_id
    ->   from inventory
    ->   where film_id = p_film_id
    ->   and store_id = p_store_id;
    ->   select found_rows() into p_film_count;
    -> end $$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;
mysql> call film_in_stock(5,6,@a);
+---------+
| film_id |
+---------+
|       5 |
+---------+
1 row in set (0.01 sec)

Query OK, 1 row affected (0.01 sec)

mysql> show create procedure film_in_stock \G;
*************************** 1. row ***************************
           Procedure: film_in_stock
            sql_mode: 
    Create Procedure: CREATE DEFINER=`root`@`localhost` PROCEDURE `film_in_stock`(in p_film_id int,in p_store_id int,out p_film_count int)
    READS SQL DATA
begin
  select film_id
  from inventory
  where film_id = p_film_id
  and store_id = p_store_id;
  select found_rows() into p_film_count;
end
character_set_client: utf8
collation_connection: utf8_general_ci
  Database Collation: utf8_general_ci
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql> create table actor(
    -> actor_id int(11)  NOT NULL AUTO_INCREMENT ,  
    -> first_name varchar(30),
    -> last_name varchar(30),
    ->   PRIMARY KEY (actor_id)  
    -> ) engine = innodb charset = utf8;
Query OK, 0 rows affected (0.02 sec)


mysql> delimiter $$
mysql> create procedure actor_insert()
    -> begin 
    -> set @x = 1;
    -> insert into actor(actor_id,first_name,last_name) values (201,'Test',201);
    -> set @x = 2;
    -> insert into actor(actor_id,first_name,last_name) values(1,'Test','1');
    -> set @x = 3;
    -> end $$
Query OK, 0 rows affected (0.01 sec)

mysql> call actor_insert();
Query OK, 0 rows affected (0.02 sec)

mysql> call actor_insert();
ERROR 1062 (23000): Duplicate entry '201' for key 'PRIMARY'
mysql> select @x;
+------+
| @x   |
+------+
|    1 |
+------+
1 row in set (0.00 sec)

mysql> delimiter $$
mysql> create procedure actor_insert_new()
    -> begin 
    -> DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2 = 1;
    -> set @x = 1;
    -> insert into actor(actor_id,first_name,last_name) values (201,'Test',201);
    -> set @x = 2;
    -> insert into actor(actor_id,first_name,last_name) values(1,'Test','1');
    -> set @x = 3;
    -> end $$
Query OK, 0 rows affected (0.02 sec)

mysql> delimiter ;

mysql> call actor_insert_new();
Query OK, 0 rows affected, 1 warning (0.01 sec)

mysql> call actor_insert_new();
Query OK, 0 rows affected, 1 warning (0.00 sec)

mysql> select @x,@x2;
+------+------+
| @x   | @x2  |
+------+------+
|    3 |    1 |
+------+------+
1 row in set (0.00 sec)

mysql> show create table payment \G;
*************************** 1. row ***************************
       Table: payment
Create Table: CREATE TABLE `payment` (
  `staff_id` int(11) DEFAULT NULL,
  `amount` int(11) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8
1 row in set (0.01 sec)

ERROR: 
No query specified

mysql> select * from payment;
+----------+--------+
| staff_id | amount |
+----------+--------+
|        1 |  10000 |
|        2 |  20000 |
|        3 |  30000 |
|        4 | 400000 |
|        5 | 500000 |
+----------+--------+
5 rows in set (0.01 sec)

mysql> delimiter $$
mysql> create procedure payment_stat()
    -> begin
    -> DECLARE i_staff_id int;
    -> DECLARE d_amount int;
    -> declare tmp_name varchar(30) default "";
    -> DECLARE cur_payment cursor for select staff_id,amount from payment;
    -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
    -> 
    -> set @x1 = 0 ;
    -> set @x2 = 0 ;
    -> 
    -> open cur_payment;
    -> fetch cur_payment into i_staff_id,d_amount;
    -> while(i_staff_id <=3  )
    -> do
    ->     if i_staff_id <3 then 
    ->         select i_staff_id,d_amount;
    ->     end if;
    -> fetch cur_payment into  i_staff_id,d_amount;
    -> end while;
    -> close cur_payment;
    -> 
    -> select @x1,@x2;
    -> end;
    -> $$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;

mysql> call  payment_stat();
+------------+----------+
| i_staff_id | d_amount |
+------------+----------+
|          1 |    10000 |
+------------+----------+
1 row in set (0.01 sec)

+------------+----------+
| i_staff_id | d_amount |
+------------+----------+
|          2 |    20000 |
+------------+----------+
1 row in set (0.01 sec)

+------+------+
| @x1  | @x2  |
+------+------+
|    0 |    0 |
+------+------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)

mysql> drop  procedure payment_stat;
Query OK, 0 rows affected (0.00 sec)


mysql> delimiter $$
mysql> create procedure payment_stat()
    -> begin
    -> DECLARE i_staff_id int;
    -> DECLARE d_amount int;
    -> declare tmp_name varchar(30) default "";
    -> DECLARE cur_payment cursor for select staff_id,amount from payment;
    -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
    -> 
    -> set @x1 = 0 ;
    -> set @x2 = 0 ;
    -> 
    -> open cur_payment;
    -> fetch cur_payment into i_staff_id,d_amount;
    -> while(i_staff_id <=3  )
    -> do
    ->     if i_staff_id <3 then 
    ->         set @x1 = @x1+ i_staff_id;
    ->     else
    ->         set @x2 = @x2+ d_amount;
    ->     end if;
    -> fetch cur_payment into  i_staff_id,d_amount;
    -> end while;
    -> close cur_payment;
    -> 
    -> select @x1,@x2;
    -> end;
    -> $$
Query OK, 0 rows affected (0.01 sec)

mysql> call  payment_stat();
    -> $$
+------+-------+
| @x1  | @x2   |
+------+-------+
|    3 | 30000 |
+------+-------+
1 row in set (0.01 sec)

Query OK, 0 rows affected (0.01 sec)


mysql> delimiter $$
mysql> create procedure payment_stat()
    -> begin
    -> DECLARE i_staff_id int;
    -> DECLARE d_amount int;
    -> 
    -> DECLARE cur_payment cursor for select staff_id,amount from payment;
    -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
    -> 
    -> set @x1 = 0 ;
    -> set @x2 = 0 ;
    -> 
    -> open cur_payment;
    -> fetch cur_payment into i_staff_id,d_amount;
    -> while(i_staff_id <=3  )
    -> do
    ->     if i_staff_id <3 then 
    ->         set @x1 = @x1+ i_staff_id + 1;
    ->     else
    ->         set @x2 = @x2+ d_amount ;
    ->     end if;
    -> fetch cur_payment into  i_staff_id,d_amount;
    -> end while;
    -> close cur_payment;
    -> 
    -> select @x1,@x2;
    -> end;
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> delimiter ;
mysql> call  payment_stat();
+------+-------+
| @x1  | @x2   |
+------+-------+
|    3 | 30000 |
+------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> drop  procedure payment_stat;
Query OK, 0 rows affected (0.00 sec)


mysql> delimiter $$
mysql> create procedure payment_stat()
    -> begin
    -> DECLARE i_staff_id int;
    -> DECLARE d_amount int;
    -> 
    -> DECLARE cur_payment cursor for select staff_id,amount from payment;
    -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
    -> 
    -> set @x1 = 0 ;
    -> set @x2 = 0 ;
    -> 
    -> open cur_payment;
    -> fetch cur_payment into i_staff_id,d_amount;
    -> while(i_staff_id <=3  )
    -> do
    ->     if i_staff_id <3 then 
    ->         set @x1 = @x1+ i_staff_id;
    ->     else
    ->         set @x2 = @x2+ d_amount;
    ->     end if;
    -> fetch cur_payment into  i_staff_id,d_amount;
    -> end while;
    -> close cur_payment;
    -> 
    -> select @x1,@x2;
    -> end;
    -> $$
Query OK, 0 rows affected (0.00 sec)


mysql> delimiter ;
mysql> call  payment_stat();
+------+-------+
| @x1  | @x2   |
+------+-------+
|    3 | 30000 |
+------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter $$
mysql> create procedure payment_stat()
    -> begin
    -> DECLARE i_staff_id int;
    -> DECLARE d_amount int;
    -> 
    -> DECLARE cur_payment cursor for select staff_id,amount from payment;
    -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
    -> 
    -> set @x1 = 0 ;
    -> set @x2 = 0 ;
    -> 
    -> open cur_payment;
    -> fetch cur_payment into i_staff_id,d_amount;
    -> while(i_staff_id <=3  )
    -> do
    ->     if i_staff_id <3 then 
    ->         set @x1 = @x1+ i_staff_id + 1;
    ->     else
    ->         set @x2 = @x2+ d_amount;
    ->     end if;
    -> fetch cur_payment into  i_staff_id,d_amount;
    -> end while;
    -> close cur_payment;
    -> 
    -> select @x1,@x2;
    -> end;
    -> $$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;
mysql> call  payment_stat();
+------+-------+
| @x1  | @x2   |
+------+-------+
|    5 | 30000 |
+------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> drop  procedure payment_stat;
Query OK, 0 rows affected (0.02 sec)


mysql> delimiter $$
mysql> create procedure payment_stat()
    -> begin
    -> DECLARE i_staff_id int;
    -> DECLARE d_amount int;
    -> 
    -> DECLARE cur_payment cursor for select staff_id,amount from payment;
    -> DECLARE EXIT HANDLER FOR NOT FOUND CLOSE cur_payment;
    -> 
    -> set @x1 = 0 ;
    -> set @x2 = 0 ;
    -> 
    -> open cur_payment;
    -> fetch cur_payment into i_staff_id,d_amount;
    -> while(i_staff_id <=3  )
    -> do
    ->     if i_staff_id <3 then 
    ->         set @x1 = @x1+ i_staff_id + 6;
    ->     else
    ->         set @x2 = @x2+ d_amount + 5;
    ->     end if;
    -> fetch cur_payment into  i_staff_id,d_amount;
    -> end while;
    -> close cur_payment;
    -> 
    -> select @x1,@x2;
    -> end;
    -> $$
Query OK, 0 rows affected (0.01 sec)

mysql> delimiter ;

mysql> call  payment_stat();
   
+------+-------+
| @x1  | @x2   |
+------+-------+
|   15 | 30005 |
+------+-------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> DELIMITER $$
mysql> 
mysql> CREATE PROCEDURE addNum()
    -> BEGIN
    -> DECLARE x  INT;
    -> SET x = 0;
    -> for_loop :  LOOP
    ->   SET  x = x + 1;
    ->   IF  x > 30 THEN
    ->    LEAVE  for_loop;
    ->   END IF;
    ->   IF mod(x,2) = 0 then 
    ->     select "num:",x;
    ->   ITERATE for_loop;
    ->   END IF;
    -> END LOOP;
    -> select "count:",x;
    -> END $$
Query OK, 0 rows affected (0.01 sec)

mysql> call addNum();
    -> $$
+------+------+
| num: | x    |
+------+------+
| num: |    2 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |    4 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |    6 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |    8 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   10 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   12 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   14 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   16 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   18 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   20 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   22 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   24 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   26 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   28 |
+------+------+
1 row in set (0.00 sec)

+------+------+
| num: | x    |
+------+------+
| num: |   30 |
+------+------+
1 row in set (0.00 sec)

+--------+------+
| count: | x    |
+--------+------+
| count: |   31 |
+--------+------+
1 row in set (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> delimiter $$
mysql> create procedure repeatPractise()
    ->      begin
    ->      set @v = 0 ;
    ->      REPEAT 
    ->         set @v =  @v+ 1;
    ->      UNTIL @v >=5  
    ->      END REPEAT;
    ->  END 
    ->  $$
Query OK, 0 rows affected (0.01 sec)

mysql> call repeatPractise();
    -> $$
Query OK, 0 rows affected (0.00 sec)

mysql> select @v;
    -> $$
+------+
| @v   |
+------+
|    5 |
+------+
1 row in set (0.00 sec)

推荐阅读
  • 推荐一个ASP的内容管理框架(ASP Nuke)的优势和适用场景
    本文推荐了一个ASP的内容管理框架ASP Nuke,并介绍了其主要功能和特点。ASP Nuke支持文章新闻管理、投票、论坛等主要内容,并可以自定义模块。最新版本为0.8,虽然目前仍处于Alpha状态,但作者表示会继续更新完善。文章还分析了使用ASP的原因,包括ASP相对较小、易于部署和较简单等优势,适用于建立门户、网站的组织和小公司等场景。 ... [详细]
  • 本文介绍了在开发Android新闻App时,搭建本地服务器的步骤。通过使用XAMPP软件,可以一键式搭建起开发环境,包括Apache、MySQL、PHP、PERL。在本地服务器上新建数据库和表,并设置相应的属性。最后,给出了创建new表的SQL语句。这个教程适合初学者参考。 ... [详细]
  • 本文介绍了如何在MySQL中将零值替换为先前的非零值的方法,包括使用内联查询和更新查询。同时还提供了选择正确值的方法。 ... [详细]
  • 在数据分析工作中,我们通常会遇到这样的问题,一个业务部门由若干业务组构成,需要筛选出每个业务组里业绩前N名的业务员。这其实是一个分组排序的 ... [详细]
  • 本文介绍了如何使用php限制数据库插入的条数并显示每次插入数据库之间的数据数目,以及避免重复提交的方法。同时还介绍了如何限制某一个数据库用户的并发连接数,以及设置数据库的连接数和连接超时时间的方法。最后提供了一些关于浏览器在线用户数和数据库连接数量比例的参考值。 ... [详细]
  • Oracle Database 10g许可授予信息及高级功能详解
    本文介绍了Oracle Database 10g许可授予信息及其中的高级功能,包括数据库优化数据包、SQL访问指导、SQL优化指导、SQL优化集和重组对象。同时提供了详细说明,指导用户在Oracle Database 10g中如何使用这些功能。 ... [详细]
  • 在说Hibernate映射前,我们先来了解下对象关系映射ORM。ORM的实现思想就是将关系数据库中表的数据映射成对象,以对象的形式展现。这样开发人员就可以把对数据库的操作转化为对 ... [详细]
  • 本文详细介绍了MysqlDump和mysqldump进行全库备份的相关知识,包括备份命令的使用方法、my.cnf配置文件的设置、binlog日志的位置指定、增量恢复的方式以及适用于innodb引擎和myisam引擎的备份方法。对于需要进行数据库备份的用户来说,本文提供了一些有价值的参考内容。 ... [详细]
  • 本文由编程笔记小编整理,介绍了PHP中的MySQL函数库及其常用函数,包括mysql_connect、mysql_error、mysql_select_db、mysql_query、mysql_affected_row、mysql_close等。希望对读者有一定的参考价值。 ... [详细]
  • Spring特性实现接口多类的动态调用详解
    本文详细介绍了如何使用Spring特性实现接口多类的动态调用。通过对Spring IoC容器的基础类BeanFactory和ApplicationContext的介绍,以及getBeansOfType方法的应用,解决了在实际工作中遇到的接口及多个实现类的问题。同时,文章还提到了SPI使用的不便之处,并介绍了借助ApplicationContext实现需求的方法。阅读本文,你将了解到Spring特性的实现原理和实际应用方式。 ... [详细]
  • 本文介绍了如何使用Power Design(PD)和SQL Server进行数据库反向工程的方法。通过创建数据源、选择要反向工程的数据表,PD可以生成物理模型,进而生成所需的概念模型。该方法适用于SQL Server数据库,对于其他数据库是否适用尚不确定。详细步骤和操作说明可参考本文内容。 ... [详细]
  • Java String与StringBuffer的区别及其应用场景
    本文主要介绍了Java中String和StringBuffer的区别,String是不可变的,而StringBuffer是可变的。StringBuffer在进行字符串处理时不生成新的对象,内存使用上要优于String类。因此,在需要频繁对字符串进行修改的情况下,使用StringBuffer更加适合。同时,文章还介绍了String和StringBuffer的应用场景。 ... [详细]
  • MyBatis错题分析解析及注意事项
    本文对MyBatis的错题进行了分析和解析,同时介绍了使用MyBatis时需要注意的一些事项,如resultMap的使用、SqlSession和SqlSessionFactory的获取方式、动态SQL中的else元素和when元素的使用、resource属性和url属性的配置方式、typeAliases的使用方法等。同时还指出了在属性名与查询字段名不一致时需要使用resultMap进行结果映射,而不能使用resultType。 ... [详细]
  • 本文详细介绍了在ASP.NET中获取插入记录的ID的几种方法,包括使用SCOPE_IDENTITY()和IDENT_CURRENT()函数,以及通过ExecuteReader方法执行SQL语句获取ID的步骤。同时,还提供了使用这些方法的示例代码和注意事项。对于需要获取表中最后一个插入操作所产生的ID或马上使用刚插入的新记录ID的开发者来说,本文提供了一些有用的技巧和建议。 ... [详细]
  • 本文介绍了一个在线急等问题解决方法,即如何统计数据库中某个字段下的所有数据,并将结果显示在文本框里。作者提到了自己是一个菜鸟,希望能够得到帮助。作者使用的是ACCESS数据库,并且给出了一个例子,希望得到的结果是560。作者还提到自己已经尝试了使用"select sum(字段2) from 表名"的语句,得到的结果是650,但不知道如何得到560。希望能够得到解决方案。 ... [详细]
author-avatar
爱夏花儿_915
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有