存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给定参数(如果该存储过程带有参数)来调用执行它。
MySQL 存储过程是从 MySQL 5.0 开始增加的新功能。大大提高数据库的处理速度,同时也可以提高数据库编程的灵活性。
过程:封装起来的若干条语句,调用时这些封装体执行。把此过程存储到数据库中即存储过程.
函数:是一个有返回值的“过程”。
过程:没有返回值的函数。
存储过程的优点:
1、存储过程增强了SQL语言的功能和灵活性(可以SQL编程,能使用变量,表达式、控制结构体)
2、存储过程能实现较快的执行速度。(存储过程要比批处理的执行速度快很多,因为存储过程是预编译的。在首次运行一个存储过程时查询,优化器对其进行分析优化,并且给出最终被存储在系统表中的执行计划。而批处理的Transaction-SQL语句在每次运行时都要进行编译和优化,速度相对要慢一些。)
3、存储过程能过减少网络流量。针对同一个数据库对象的操作(如查询、修改),如果这一操作所涉及的Transaction-SQL语句被组织程存储过程,那么当在客户计算机上调用该存储过程时,网络中传送的只是该调用语句,从而大大增加了网络流量并降低了网络负载。
4、存储过程可被作为一种安全机制来充分利用。系统管理员通过执行某一存储过程的权限进行限制,能够实现对相应的数据的访问权限的限制,避免了非授权用户对数据的访问,保证了数据的安全。
5、存储过程允许标准组件是编程。存储过程被创建后,可以在程序中被多次调用,而不必重新编写该存储过程的SQL语句。而且数据库专业人员可以随时对存储过程进行修改,对应用程序源代码毫无影响。
存储过程的创建:
DELIMITER $$ //定义分隔符 CREATE PROCEDURE p() //p为过程名 BEGIN --sql语句 //封装语句体 END $$
查看已有的存储过程:show procedure status \G; 调用存储过程:call 存储过程名称(); 删除存储过程:drop procedure 存储过程名称;
变量声明:存储过程中用declare声明变量 格式:declare 变量名 变量类型 [default 默认值] 存储过程中,变量可以在SQL语句中合法的运算,如+ - * / 运算结果如何赋值 set 变量 := 表达式 存储过程传参:存储过程的括号里,可以声明参数。 语法是 create procedure p([in/out/inout] 参数名 参数类型 ..) in out inout详细分析见下一篇博文(链接下午放出)
实例一:创建 运算 赋值
CREATE PROCEDURE p1() BEGIN DECLARE age INT DEFAULT 18; DECLARE height INT DEFAULT 180; SET age := age +20; SELECT CONCAT('年龄是', age, '身高是', height); END$$

实例二 :if/else 控制结构
CREATE PROCEDURE p2() BEGIN DECLARE age INT DEFAULT 18; DECLARE height INT DEFAULT 180; IF age >= 18 THEN SELECT "已成年"; ELSE SELECT "未成年"; END IF; END$$

实例三:while/do 控制结构 求1-100的和
CREATE PROCEDURE p3() BEGIN DECLARE total INT DEFAULT 0; DECLARE num INT DEFAULT 0; WHILE num<=100 DO SET total := num + total; SET num := num + 1; END WHILE; SELECT total; END$$

实例四:case 控制结构
CREATE PROCEDURE p4() BEGIN DECLARE num INT DEFAULT 0; SET num := FLOOR(4*RAND()); CASE num WHEN 1 THEN SELECT "cat"; WHEN 2 THEN SELECT "dog"; WHEN 3 THEN SELECT "sheep"; ELSE SELECT "pig"; END CASE; END$$

实例五:repeat循环
CREATE PROCEDURE p5() BEGIN DECLARE num INT DEFAULT 0; DECLARE total INT DEFAULT 0; REPEAT SET total = num + total; SET num := num + 1; UNTIL num>100 END REPEAT; SELECT total; END$$


MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

MySQL is an open source relational database management system suitable for data storage, management, query and security. 1. It supports a variety of operating systems and is widely used in Web applications and other fields. 2. Through the client-server architecture and different storage engines, MySQL processes data efficiently. 3. Basic usage includes creating databases and tables, inserting, querying and updating data. 4. Advanced usage involves complex queries and stored procedures. 5. Common errors can be debugged through the EXPLAIN statement. 6. Performance optimization includes the rational use of indexes and optimized query statements.

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

InnoDB's lock mechanisms include shared locks, exclusive locks, intention locks, record locks, gap locks and next key locks. 1. Shared lock allows transactions to read data without preventing other transactions from reading. 2. Exclusive lock prevents other transactions from reading and modifying data. 3. Intention lock optimizes lock efficiency. 4. Record lock lock index record. 5. Gap lock locks index recording gap. 6. The next key lock is a combination of record lock and gap lock to ensure data consistency.

The main reasons for poor MySQL query performance include not using indexes, wrong execution plan selection by the query optimizer, unreasonable table design, excessive data volume and lock competition. 1. No index causes slow querying, and adding indexes can significantly improve performance. 2. Use the EXPLAIN command to analyze the query plan and find out the optimizer error. 3. Reconstructing the table structure and optimizing JOIN conditions can improve table design problems. 4. When the data volume is large, partitioning and table division strategies are adopted. 5. In a high concurrency environment, optimizing transactions and locking strategies can reduce lock competition.

In database optimization, indexing strategies should be selected according to query requirements: 1. When the query involves multiple columns and the order of conditions is fixed, use composite indexes; 2. When the query involves multiple columns but the order of conditions is not fixed, use multiple single-column indexes. Composite indexes are suitable for optimizing multi-column queries, while single-column indexes are suitable for single-column queries.

To optimize MySQL slow query, slowquerylog and performance_schema need to be used: 1. Enable slowquerylog and set thresholds to record slow query; 2. Use performance_schema to analyze query execution details, find out performance bottlenecks and optimize.

MySQL and SQL are essential skills for developers. 1.MySQL is an open source relational database management system, and SQL is the standard language used to manage and operate databases. 2.MySQL supports multiple storage engines through efficient data storage and retrieval functions, and SQL completes complex data operations through simple statements. 3. Examples of usage include basic queries and advanced queries, such as filtering and sorting by condition. 4. Common errors include syntax errors and performance issues, which can be optimized by checking SQL statements and using EXPLAIN commands. 5. Performance optimization techniques include using indexes, avoiding full table scanning, optimizing JOIN operations and improving code readability.


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

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SublimeText3 Chinese version
Chinese version, very easy to use

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

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