bitsCN.com
Mysql那些事儿之(十三)变量、条件的使用
相关链接:
Mysql那些事儿之(一)mysql的安装
http:///database/201210/162314.html;
Mysql那些事儿之(二)有关数据库的操作
http:///database/201210/162315.html;
Mysql那些事儿之(三)有关数据表的操作
http:///database/201210/162316.html;
Mysql那些事儿之(四)数据表数据查询操作
http:///database/201210/162317.html;
Mysql那些事儿之(五)操作时间
http:///database/201210/162318.html;
Mysql那些事儿之(六)字符串模式匹配
http:///database/201210/163969.html;
Mysql那些事儿之(七)深入select查询
http:///database/201210/163970.html;
Mysql那些事儿之(八)索引
http:///database/201210/163971.html;
Mysql那些事儿之(九)常用的函数
http:///database/201210/164229.html;
Mysql那些事儿之(十)触发器一
http:///database/201210/164516.html;
Mysql那些事儿之(十一)触发器二
http:///database/201210/164766.html;
Mysql那些事儿之(十二)存储过程
http:///database/201210/164795.html
存储过程中可以使用变量、条件
1.定义变量
通过DECLARE可以定义一个变量,不过是局部的。它的作用范围只能在BEGIN...END有效,可以用在嵌套的块中;当然可以用default设置默认值。
Sql代码
DECLARE var_name [,.....] type [DEFAULT value]
示例:定义一个变量
Sql代码
DECLARE last_day_start DATE --定义一个DATE型的变量,名称为last_day_start
2.变量赋值
可以直接赋值,可以查询赋值。
Sql代码
--直接赋值使用set,可以赋常量或者赋表达式
--语法格式
SET var_name = expr [,var_name = expr.....]
示例:给上面定义的变量赋值
Sql代码
SET last_day_start = DATE_SUB(CURRENT_DATE(),INTERVAL 1 MONTH) --给变量赋值
通过查询给变量赋值
Sql代码
--通过查询给变量赋值
--语法格式
SELECT col_name[,...] INTO var_name [,....] table_expr
Sql代码
--示例 将查询结果给变量赋值
Sql代码
DECLARE v_payment DECIMAL(5,2); --定义一个变量
--使用查询给变量赋值
SELECT IFNULL(SUM(payment.amount),0) INTO v_payment FROM payment
3.条件的定义
Sql代码
--定义条件的语法格式
DECLARE condition_name CONDITION FOR condition_value
condition_value:
SQLSTATE [VALUE] sqlstate_value
| mysql_error_code
4.条件的处理
Sql代码
---条件的处理
DECLARE handler_type HANDLER FOR condition_value [,....] sp_statement
handler_type:
CONTINUE | EXIT | UNDO
condition_value:
SQLSTATE [VALUE]
| condition_name
| SQLWARNING
| NOT FOUND
| SQLEXCEPTION
| mysql_error_code
示例:
Sql代码
DECLARE CONTINUE HANDLER FOR SQLSTATE '23000' SET @x2=1
CONTINUE 表示继续向下执行;
EXIT 表示执行中止。

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

MySQL/InnoDB supports four transaction isolation levels: ReadUncommitted, ReadCommitted, RepeatableRead and Serializable. 1.ReadUncommitted allows reading of uncommitted data, which may cause dirty reading. 2. ReadCommitted avoids dirty reading, but non-repeatable reading may occur. 3.RepeatableRead is the default level, avoiding dirty reading and non-repeatable reading, but phantom reading may occur. 4. Serializable avoids all concurrency problems but reduces concurrency. Choosing the appropriate isolation level requires balancing data consistency and performance requirements.

MySQL is suitable for web applications and content management systems and is popular for its open source, high performance and ease of use. 1) Compared with PostgreSQL, MySQL performs better in simple queries and high concurrent read operations. 2) Compared with Oracle, MySQL is more popular among small and medium-sized enterprises because of its open source and low cost. 3) Compared with Microsoft SQL Server, MySQL is more suitable for cross-platform applications. 4) Unlike MongoDB, MySQL is more suitable for structured data and transaction processing.

MySQL index cardinality has a significant impact on query performance: 1. High cardinality index can more effectively narrow the data range and improve query efficiency; 2. Low cardinality index may lead to full table scanning and reduce query performance; 3. In joint index, high cardinality sequences should be placed in front to optimize query.

The MySQL learning path includes basic knowledge, core concepts, usage examples, and optimization techniques. 1) Understand basic concepts such as tables, rows, columns, and SQL queries. 2) Learn the definition, working principles and advantages of MySQL. 3) Master basic CRUD operations and advanced usage, such as indexes and stored procedures. 4) Familiar with common error debugging and performance optimization suggestions, such as rational use of indexes and optimization queries. Through these steps, you will have a full grasp of the use and optimization of MySQL.

MySQL's real-world applications include basic database design and complex query optimization. 1) Basic usage: used to store and manage user data, such as inserting, querying, updating and deleting user information. 2) Advanced usage: Handle complex business logic, such as order and inventory management of e-commerce platforms. 3) Performance optimization: Improve performance by rationally using indexes, partition tables and query caches.


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

Dreamweaver Mac version
Visual web development tools

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

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

Atom editor mac version download
The most popular open source editor

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