mysql---存储过程_MySQL
了解存储过程之前,先了解一下mysql的控制结构。
类似C语言(if……else、while循环等)SQL也有自己的控制结构。
if……else控制结构:
例如:
(1)
1 |
|
(2)
1 |
|
(3)
1 |
|
需要注意所有的执行语句和end if都要以‘;’结束,而且判断表达式之后接then,还有一点与C语言不同的是elseif之间没有空格。
mysql中还有一些与if相关的函数
if(判断表达式,值1,值2) 如果表达式为“true”返回“值1”,表达式为“false”返回“值2”。类似于C语言中的三目运算符。
ifnull(表达式1,表达式2)如果表达式1不为空,则返回表达式1。如果表达式1为空,则返回表达式2
nullif(表达式1,表达式2)如果表达式1=表达式2,返回null ,否则返回表达式1。
case when控制结构:
有两种形式
(1)
1 |
|
(2)
1 |
|
while循环结构:
1 |
|
loop循环结构:无条件循环
1 |
|
repeat循环结构:
1 |
|
现在开始介绍存储过程,其实存储过程跟函数很像
查看当前存储过程的状态:show procedure status;
创建存储过程:
1 |
|
参数列表总是存在的,如果没有参数则应该是空参数列表(),参数必须指定数据类型而且每个参数默认都是一个in参数。要指定为其他参数,可以在参数前面加上out或inout关键字。默认的in类似于按值传递,在存储过程中对参数进行修改,调用者是看不到的。out参数只是用来从存储过程传回数据的,无论给参数传入什么值,这个参数的初始值始终是null。对于inout参数,调用者不仅可以设置参数的初始值,而且在过程中修改参数,调用者是看得到的类似与按地址传递。
删除存储过程:drop procedure 名称;
查看存储过程:show create procedure 名称/G 类似于show create table 表名 /G的作用是横向显示
调用存储过程:call 名称(参数);
声明变量:
(1)declare变量名 变量类型 默认值; 声明变量必须在开头定义,如果没有默认值,初始值为null。作用范围是在begin……end内
(2)set @变量名=初始值;定义的变量是用户变量,在存储过程之外的sql也是可以调用的
变量赋值:set 变量名=变量值 切忌直接给变量赋值(变量名= 变量值)
还有一种给一个或多个变量赋值的方法:利用“select 指定列 into 指定变量”,所以select的结果必须是单行。
示例:
所有示例,都实现将分界符设置为'$'
delimiter $
1、测试if-else控制结构
2、测试case……when
第一种情况:
输出是值,结尾用end。一般用于select
输出是语句,结尾用end case。一般用于存储过程
第二种情况:
输出是语句,结尾用end case。一般用于存储过程
输出是值,结尾用end。一般用于select
3、测试while循环
4、测试loop
5、测试repeat
6、带参数的存储过程
默认为in的参数:按值传递
初始值为0的变量tmp作为参数传入存储过程后,虽然在存储过程内对其进行修改,但调用者再次查看tmp时,值仍然为0,没有变化
out参数:
由第一个select可以看出,out参数不允许将实参的值传入存储过程。通过第二个和第三个select可以看出,存储过程内部修改变量后可以返回给调用者。
与按地址传递还有所不同,out只允许返回值,不允许传入值。
inout参数:按地址传递,形参值改变会改变实参的值
第一个select结果为0,说明实参的值传进存储过程。第二个和第三个select结果表明,inout可以在存储过程内部修改形参的值,从而影响实参,类似于按地址传递

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

MySQL supports four index types: B-Tree, Hash, Full-text, and Spatial. 1.B-Tree index is suitable for equal value search, range query and sorting. 2. Hash index is suitable for equal value searches, but does not support range query and sorting. 3. Full-text index is used for full-text search and is suitable for processing large amounts of text data. 4. Spatial index is used for geospatial data query and is suitable for GIS applications.

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.
