mysql---存储过程 了解存储过程之前,先了解一下mysql的控制结构。 类C语言(if……else、while循环等)SQL也有自己的控制结构。 if……else控制结构: 例如: (1) span style=font-family:FangSong_GB2312;if 判断表达式 then 执行语句;end if;与c语言进
mysql---存储过程了解存储过程之前,先了解一下mysql的控制结构。
类似C语言(if……else、while循环等)SQL也有自己的控制结构。
if……else控制结构:
例如:
(1)
<span style="font-family:FangSong_GB2312;">if 判断表达式 then 执行语句; end if; 与c语言进行比较 if(判断表达式) 执行语句;</span>
(2)
<span style="font-family:FangSong_GB2312;">if 判断表达式1 then 执行语句1; else then 执行语句2; end if; 与c语言进行比较 if(判断表达式1) 执行语句1; else 执行语句2;</span>
(3)
<span style="font-family:FangSong_GB2312;">if 判断表达式1 then 执行语句1; elseif 判断表达式2 then 执行语句2; …… elseif 判断表达式N then 执行语句N; else 执行语句N+1; end if; 与c语言进行比较 if(判断表达式1) 执行语句1; else if(判断表达式2) 执行语句2; …… else if(判断表达式N) 执行语句N; else 执行语句N+1;</span>
需要注意所有的执行语句和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)
<span style="font-family:FangSong_GB2312;">case 待判断值 when 值1 then 输出1 when 值2 then 输出2 …… when 值N then 输出N else 默认输出 end; #如果输出时语句的话,最后的结尾要改成end case。输出的是值则是end 同C语言的switch相比较 switch(待判断值){ case 值1:输出1 break; case 值2:输出2 break; …… case 值N:输出N break; default:默认输出 }</span>
(2)
<span style="font-family:FangSong_GB2312;">case when 判断表达式1 then 输出1 when 判断表达式2 then 输出2 …… when 判断表达式N then 输出N else 默认输出 end case; #如果输出时语句的话,最后的结尾要是end case。输出的是值则是end。</span>
while循环结构:
<span style="font-family:FangSong_GB2312;">while 判断表达式 do 循环体 end while; C语言中的while循环 while(判断表达式){ 循环体; }</span>
loop循环结构:无条件循环
<span style="font-family:FangSong_GB2312;">标签:loop 循环体; end loop; 可以通过"leave 标签"来跳出loop循环。</span>
repeat循环结构:
<span style="font-family:FangSong_GB2312;">repeat 循环体; until 判断表达式 end repeat;</span>
现在开始介绍存储过程,其实存储过程跟函数很像
查看当前存储过程的状态:show procedure status;
创建存储过程:
<span style="font-family:FangSong_GB2312;">create procedure 名称(参数列表) begin 语句集 end;</span>
参数列表总是存在的,如果没有参数则应该是空参数列表(),参数必须指定数据类型而且每个参数默认都是一个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可以在存储过程内部修改形参的值,从而影响实参,类似于按地址传递

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Dreamweaver CS6
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

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