bitsCN.com
MYSQL入门学习之十二:存储过程的基本操作
相关链接:
MYSQL入门学习之一:基本操作
http:///database/201212/173868.html
MYSQL入门学习之二:使用正则表达式搜索
http:///database/201212/173869.html
MYSQL入门学习之三:全文本搜索
http:///database/201212/173873.html
MYSQL入门学习之四:MYSQL的数据类型
http:///database/201212/175536.html
MYSQL入门学习之五:MYSQL的字符集
http:///database/201212/175541.html
MYSQL入门学习之六:MYSQL的运算符
http:///database/201212/175862.html
MYSQL入门学习之七:MYSQL常用函数
http:///database/201212/175864.html
MYSQL入门学习之八:数据库及表的基本操作
http:///database/201212/175867.html
MYSQL入门学习之九:索引的简单操作
http:///database/201212/176772.html
MYSQL入门学习之十:视图的基本操作
http:///database/201212/176775.html
MYSQL入门学习之十一:触发器的基本操作
http:///database/201212/176781.html
存储过程简单来说,就是为以后的使用而保存的一条或多条MySQL语句的集合。可将其视为批文件,虽然它们的作用不仅限于批处理。
使用存储过程需要MySQL5及以后的版本支持。
一、为什么要使用存储过程
通过把处理封闭在容易使用的单元中,简化复杂的操作;
将一系列处理步骤放到同一存储过程中,保证了数据的完整性和操作的安全性;
简化对变更的管理;
提高性能。使用存储过程比使用单独的SQL语句要快;
存在一些只能用在单个请求中的MySQL元素和特性,存储过程可以使用它们来编写功能更强更灵活的代码;
二、基本操作
1、创建存储过程
CREATE PROCEDURE sp_name ([proc_parameter[,...]])
[characteristic ...] routine_body
proc_parameter:
[ IN | OUT | INOUT ] param_name type
示例:
[sql]
mysql>create procedure sp_test()
->begin
-> select userid,username from newname where userid=215;
->end
->//
2、执行存储过程
CALL sp_name;
示例:
[sql]
mysql> call sp_test();
+--------+----------+
| userid | username |
+--------+----------+
| 215 | NULL |
+--------+----------+
3、删除存储过程
DROP PROCEDURE [ IF EXISTS ] sp_name;
示例:
[sql]
mysql> drop procedure if exists sp_test;
4、查看存储过程创建信息
SHOW CREATE PROCEDURE sp_name;
示例:
[sql]
mysql> show create procedure sp_test;
+-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+
| Procedure | sql_mode | Create Procedure | character_set_client | collation_connection | Database Collation |
+-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+
| sp_test | | CREATE DEFINER=`root`@`localhost` PROCEDURE `sp_test`()
begin
select userid,username from newname where userid=215;
end | latin1 | latin1_swedish_ci | latin1_swedish_ci |
+-----------+----------+--------------------------------------------------------+----------------------+----------------------+--------------------+
5、查看存储过程状态
SHOW PROCEDURE STATUS [ LIKE '' ];
示例:
[sql]
mysql> show procedure status like 'sp_test';
+------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+-
| Db | Name | Type | Definer | Modified | Created | Security_type | Comment | character_set_client |
+------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+-
| test | sp_test | PROCEDURE | root@localhost | 2012-12-17 23:57:38 | 2012-12-17 23:57:38 | DEFINER | | latin1 |
+------+---------+-----------+----------------+---------------------+---------------------+---------------+---------+----------------------+-
6、使用存储过程参数
示例:
[sql]
mysql> delimiter //
mysql> create procedure sp_type_cnt(
-> IN in_type int,
-> OUT out_cnt int
-> )
-> begin
-> select count(*)
-> from newname
-> where type = in_type
-> into out_cnt;
-> end;
-> //
mysql> delimiter ;
mysql> call sp_type_cnt(0,@cnt);
mysql> select @cnt;
+------+
| @cnt |
+------+
| 159 |
+------+
bitsCN.com

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.

ToaddauserremotelytoMySQL,followthesesteps:1)ConnecttoMySQLasroot,2)Createanewuserwithremoteaccess,3)Grantnecessaryprivileges,and4)Flushprivileges.BecautiousofsecurityrisksbylimitingprivilegesandaccesstospecificIPs,ensuringstrongpasswords,andmonitori

TostorestringsefficientlyinMySQL,choosetherightdatatypebasedonyourneeds:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseTEXTforlong-formtextcontent.4)UseBLOBforbinarydatalikeimages.Considerstorageov

When selecting MySQL's BLOB and TEXT data types, BLOB is suitable for storing binary data, and TEXT is suitable for storing text data. 1) BLOB is suitable for binary data such as pictures and audio, 2) TEXT is suitable for text data such as articles and comments. When choosing, data properties and performance optimization must be considered.

No,youshouldnotusetherootuserinMySQLforyourproduct.Instead,createspecificuserswithlimitedprivilegestoenhancesecurityandperformance:1)Createanewuserwithastrongpassword,2)Grantonlynecessarypermissionstothisuser,3)Regularlyreviewandupdateuserpermissions

MySQLstringdatatypesshouldbechosenbasedondatacharacteristicsandusecases:1)UseCHARforfixed-lengthstringslikecountrycodes.2)UseVARCHARforvariable-lengthstringslikenames.3)UseBINARYorVARBINARYforbinarydatalikecryptographickeys.4)UseBLOBorTEXTforlargeuns


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

SublimeText3 English version
Recommended: Win version, supports code prompts!

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft
