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)0
0
相关文章
mysql并发事务中如何避免长事务_mysql性能风险说明
mysql主从复制中的复制链与多级复制配置
mysql数据库的恢复模式:完全恢复与部分恢复
mysql备份大数据量数据库如何优化_mysql性能备份技巧
mysql如何迁移数据到新服务器_mysql数据库迁移方法
本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门AI工具
幻方量化公司旗下的开源大模型平台
字节跳动自主研发的一系列大型语言模型
阿里巴巴推出的全能AI助手
腾讯混元平台推出的AI助手
文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。
基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿
一站式AI创作平台,免费AI图片和视频生成。
最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。
智谱清言 - 免费全能的AI助手
相关专题
本专题系统整理batoto漫画官方网站最新可用入口,涵盖最新官网地址、网页版登录页面及防走失访问方式说明,帮助用户快速找到batoto漫画官方平台,稳定在线阅读各类漫画内容。
57
2026.02.25
本专题系统整理Steam官网最新可用入口,涵盖网页版登录地址、新用户注册流程、账号登录方法及官方游戏商店访问说明,帮助新手玩家快速进入Steam平台,完成注册登录并管理个人游戏库。
6
2026.02.25
本专题面向全栈开发者,系统讲解基于 TypeScript 构建前后端统一技术栈的工程化实践。内容涵盖项目分层设计、接口协议规范、类型共享机制、错误码体系设计、接口自动化生成与文档维护方案。通过完整项目示例,帮助开发者构建结构清晰、类型安全、易维护的现代全栈应用架构。
5
2026.02.25
本专题聚焦 Python 在数据工程场景下的实际应用,系统讲解 ETL 流程设计、数据抽取与清洗、批处理与增量处理方案,以及数据质量校验与异常处理机制。通过构建完整的数据处理流水线案例,帮助开发者掌握数据工程中的性能优化思路与工程化规范,为后续数据分析与机器学习提供稳定可靠的数据基础。
0
2026.02.25
本专题围绕 Java 在复杂业务系统中的建模与架构设计展开,深入讲解领域驱动设计(DDD)的核心思想与落地实践。内容涵盖领域划分、聚合根设计、限界上下文、领域事件、贫血模型与充血模型对比,并结合实际业务案例,讲解如何在 Spring 体系中实现可演进的领域模型架构,帮助开发者应对复杂业务带来的系统演化挑战。
0
2026.02.25
《Golang 生态工具与框架》系统梳理 Go 语言在实际工程中的主流工具链与框架选型思路,涵盖 Web 框架、RPC 通信、依赖管理、测试工具、代码生成与项目结构设计等内容。通过真实项目场景解析不同工具的适用边界与组合方式,帮助开发者构建高效、可维护的 Go 工程体系,并提升团队协作与交付效率。
18
2026.02.24
《Golang 性能优化专题》聚焦 Go 应用在高并发与大规模服务中的性能问题,从 profiling、内存分配、Goroutine 调度、GC 机制到 I/O 与锁竞争逐层分析。结合真实案例讲解定位瓶颈的方法与优化策略,帮助开发者建立系统化性能调优思维,在保证代码可维护性的同时显著提升服务吞吐与稳定性。
9
2026.02.24
Golang 面试题精选》系统整理企业常见 Go 技术面试问题,覆盖语言基础、并发模型、内存与调度机制、网络编程、工程实践与性能优化等核心知识点。每道题不仅给出答案,还拆解背后的设计原理与考察思路,帮助读者建立完整知识结构,在面试与实际开发中都能更从容应对复杂问题。
5
2026.02.24
《Golang 运行与部署实战》围绕 Go 应用从开发完成到稳定上线的完整流程展开,系统讲解编译构建、环境配置、日志与配置管理、容器化部署以及常见运维问题处理。结合真实项目场景,拆解自动化构建与持续部署思路,帮助开发者建立可靠的发布流程,提升服务稳定性与可维护性。
5
2026.02.24
热门下载
相关下载
精品课程
