1. 目标
掌握如何创建存储过程
2. 语法
CREATE PROCEDURE sp_name([proc_parameter])
[characteristics...] routine_body
3. 说明
CREATE PROCEDURE为用来创建存储过程的关键字; sp_name为存储过程的名称; proc_parameter为指定存储过程的参数列表,参数列表的形式:[IN | OUT | INOUT] param_name typeIN:表示输入参数,OUT:表示输出参数,INOUT:表示既可以输入也可以输出;param_name表示参数的名称;type表示参数的类型,该类型可以是MySQL数据库中的任意类型。characteristics指定存储过程的特性,可以有以下几种取值方式:
LANGUAGE SQL: 说明routine_body部分由SQL语句组成,当前系统支持的语言为SQL, SQL是LANGUAGE特性的唯一值;[NOT] DETERMINISTIC: 指明存储过程执行的结果是否正确。DETERMINISTIC表示结果是确定的。每次执行存储过程时,相同的输入会得到相同的输出;而NOT DETERMINISTIC表示结果是不确定的,相同的输入可能得到不同的输入。如果没有指定任意一个值,默认为NOT DETERMINISTIC。{CONTAINS SQL | NO SQL | READS SQL DATA | MODIFIES SQL DATA}: 指明子程序使用SQL语句限制。CONTAINS SQL表明子程序包含SQL语句,但是不包含读写数据的语句;NO SQL表明子程序不包含SQL语句;READS SQL DATA说明子程序包含读写数据的语句;MODIFIES SQL DATA表明子程序包含写数据的语句;默认情况下,系统会指定为CONTAINS SQL;SQL SECURITY { DEFINER|INVOKER}: 指明谁有权限来执行。DEFINER表示只有定义存储过程者才能执行;INVOKER表示拥有权限的调用者可以执行。默认情况下,系统指定为DEFINER。COMMENT 'string': 注释信息,可以用来描述存储过程或者函数。routine_body是SQL代码内容,可以用BEGIN...END来表示SQL代码的开始与结束。
4. 示例
1) 创建示例数据库
create database hr; use hr;2) 创建示例用到的表并插入样例数据
create table employees ( employee_id int(11) primary key not null auto_increment, employee_name varchar(50) not null, employee_sex varchar(10) default '男', hire_date datetime not null default current_timestamp, employee_mgr int(11), employee_salary float default 3000, department_id int(11) );
insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('David Tian','男',10,7500,1); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Black Xie','男',10,6600,1); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Moses Wang','男',10,4300,1); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Rena Ruan','女',10,5300,1); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Sunshine Ma','女',10,6500,2); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Scott Gao','男',10,9500,2); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Warren Si','男',10,7800,2); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Kaishen Yang','男',10,9500,3); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Simon Song','男',10,5500,3); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Brown Guan','男',10,5000,3); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Eleven Chen','女',10,3500,2); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Cherry Zhou','女',10,5500,4); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Klause He','男',10,4500,5); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Maven Ma','男',10,4500,6); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Stephani Wang','女',10,5500,7); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Jerry Guo','男',10,8500,1); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Gerardo Garza','男',10,25000,8); insert into employees(employee_name,employee_sex,employee_mgr,employee_salary,department_id) values ('Derek Wu','男',10,5500,5);3) 查看插入的样例数据
select * from employees;

4) 创建计算平均工资的存储过程
DELIMITER // create procedure calculate_emp_sal_avg_p() begin select AVG(employee_salary) as average_salary from employees; end// DELIMITER ;
说明
DELIMETER //:该语句作用是将MySQL的结果结束符设置为//,因为MySQL默认的语句结束符为分号";",为了避免与存储过程中SQL语句的结束符相冲突,需要使用DELIMETER改变存储过程的结束符,并以"END //" 结束存储过程。存储过程定义完毕以后再使用"DELIMETER ; "恢复默认结束符。DELIMETER也可以指定其它符号为结束符。5. 调用存储过程
存储过程是通过CALL语句进行调用的,语法如下:
CALL sp_name([parameter[,...]])CALL语句调用一个先前用CREATE PROCEDURE创建的存储过程,其中sp_name为存储过程名称,parameter为存储过程参数。
CALL calculate_emp_sal_avg_p();

6. 查看存储过程
1) SHOW STATUS 语句查看存储过程
语法
SHOW PROCEDURE STATUS [LIKE 'pattern']这个语句是一个MySQL的扩展,它返回子程序的特征,如数据库、名字、类型、创建者及创建日期和修改日期。
LIKE语句表示匹配存储过程的名称;
2) SHOW CREATE 语句查看存储过程定义
语法
SHOW CREATE PROCEDURE sp_name这个语句是一个MySQL的扩展,类似于SHOW CREATE TABLE,它返回一个可用来重新创建已命名存储过程的确切字符串。
3) 从information_schema.Routines表中查看存储过程
语法
SELECT * FROM information_schema.Routines WHERE ROUTINE_NAME='sp_name';
ROUTINE_NAME字段中存储的是存储过程或者函数的名称;sp_name指存储过程或函数名称;如果您们在尝试的过程中遇到什么问题或者我的代码有错误的地方,请给予指正,非常感谢!
联系方式:david.louis.tian@outlook.com
版权@:转载请标明出处!
TograntpermissionstonewMySQLusers,followthesesteps:1)AccessMySQLasauserwithsufficientprivileges,2)CreateanewuserwiththeCREATEUSERcommand,3)UsetheGRANTcommandtospecifypermissionslikeSELECT,INSERT,UPDATE,orALLPRIVILEGESonspecificdatabasesortables,and4)

ToaddusersinMySQLeffectivelyandsecurely,followthesesteps:1)UsetheCREATEUSERstatementtoaddanewuser,specifyingthehostandastrongpassword.2)GrantnecessaryprivilegesusingtheGRANTstatement,adheringtotheprincipleofleastprivilege.3)Implementsecuritymeasuresl

ToaddanewuserwithcomplexpermissionsinMySQL,followthesesteps:1)CreatetheuserwithCREATEUSER'newuser'@'localhost'IDENTIFIEDBY'password';.2)Grantreadaccesstoalltablesin'mydatabase'withGRANTSELECTONmydatabase.TO'newuser'@'localhost';.3)Grantwriteaccessto'

The string data types in MySQL include CHAR, VARCHAR, BINARY, VARBINARY, BLOB, and TEXT. The collations determine the comparison and sorting of strings. 1.CHAR is suitable for fixed-length strings, VARCHAR is suitable for variable-length strings. 2.BINARY and VARBINARY are used for binary data, and BLOB and TEXT are used for large object data. 3. Sorting rules such as utf8mb4_unicode_ci ignores upper and lower case and is suitable for user names; utf8mb4_bin is case sensitive and is suitable for fields that require precise comparison.

The best MySQLVARCHAR column length selection should be based on data analysis, consider future growth, evaluate performance impacts, and character set requirements. 1) Analyze the data to determine typical lengths; 2) Reserve future expansion space; 3) Pay attention to the impact of large lengths on performance; 4) Consider the impact of character sets on storage. Through these steps, the efficiency and scalability of the database can be optimized.

MySQLBLOBshavelimits:TINYBLOB(255bytes),BLOB(65,535bytes),MEDIUMBLOB(16,777,215bytes),andLONGBLOB(4,294,967,295bytes).TouseBLOBseffectively:1)ConsiderperformanceimpactsandstorelargeBLOBsexternally;2)Managebackupsandreplicationcarefully;3)Usepathsinst

The best tools and technologies for automating the creation of users in MySQL include: 1. MySQLWorkbench, suitable for small to medium-sized environments, easy to use but high resource consumption; 2. Ansible, suitable for multi-server environments, simple but steep learning curve; 3. Custom Python scripts, flexible but need to ensure script security; 4. Puppet and Chef, suitable for large-scale environments, complex but scalable. Scale, learning curve and integration needs should be considered when choosing.

Yes,youcansearchinsideaBLOBinMySQLusingspecifictechniques.1)ConverttheBLOBtoaUTF-8stringwithCONVERTfunctionandsearchusingLIKE.2)ForcompressedBLOBs,useUNCOMPRESSbeforeconversion.3)Considerperformanceimpactsanddataencoding.4)Forcomplexdata,externalproc


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

Atom editor mac version download
The most popular open source editor

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 Linux new version
SublimeText3 Linux latest version
