


Let's talk about the basics of MySQL custom variables and statement end delimiters
This article brings you relevant knowledge about custom variables and statement end delimiters in mysql. I hope it will be helpful to you.
Stored Program
Sometimes in order to complete a common function, you need to execute many statements, and each time you enter these statements one by one in the client. Multiple statements are annoying. The uncle who designed MySQL
very thoughtfully provided us with something called stored program
. This so-called stored program
can encapsulate some statements. Then provide the user with a simple way to call this stored procedure to execute these statements indirectly. Depending on the calling method, we can divide stored procedures
into the following types: stored routines
, triggers
and events
. Among them, Stored routines
can be subdivided into Stored functions
and Stored procedures
. Let’s draw a picture to show it:
Don’t be afraid that there are many unfamiliar concepts, we will break them down later. However, before formally introducing stored procedures
, we need to first understand the concepts of custom variables and statement end delimiters in MySQL
.
Introduction to custom variables
In life we often encounter some fixed values, such as numbers 100
, strings 'Hello'
, we call these things with fixed values constants
. But sometimes for convenience, we will use a certain symbol to represent a value, and the value it represents can change. For example, we stipulate that the symbol a
represents the number 1
, and then we can let the symbol a
represent the number 2
. We can make this value happen The changed stuff is called variable
, and the symbol a
is called the variable name
of this variable. In MySQL
, we can customize some of our own variables through the SET
statement, for example:
mysql> SET @a = 1; Query OK, 0 rows affected (0.00 sec) mysql>
The above statement indicates that we have defined a name It is a variable of a
, and the integer 1
is assigned to this variable. However, everyone needs to pay attention to the fact that the uncle who designed MySQL stipulated that we must add a @
symbol in front of our custom variables (although it is a bit strange, this is what others stipulated, so everyone should just abide by it).
If we want to check the value of this variable later, just use the SELECT
statement, but we still need to add a @
symbol before the variable name:
mysql> SELECT @a; +------+ | @a | +------+ | 1 | +------+ 1 row in set (0.00 sec) mysql>
The same variable can also store different types of values. For example, we assign a string value to the variable a
:
mysql> SET @a = '哈哈哈'; Query OK, 0 rows affected (0.01 sec) mysql> SELECT @a; +-----------+ | @a | +-----------+ | 哈哈哈 | +-----------+ 1 row in set (0.00 sec) mysql>
In addition to assigning a constant to a In addition to variables, we can also assign one variable to another variable:
mysql> SET @b = @a; Query OK, 0 rows affected (0.00 sec) mysql> select @b; +-----------+ | @b | +-----------+ | 哈哈哈 | +-----------+ 1 row in set (0.00 sec) mysql>
In this way, variables a
and b
will have the same value'Wahaha'
!
We can also assign the result of a certain query to a variable, provided that the result of the query has only one value:
mysql> SET @a = (SELECT m1 FROM t1 LIMIT 1); Query OK, 0 rows affected (0.00 sec) mysql>
We can also use another form of statement to assign the query result The result is assigned to a variable:
mysql> SELECT n1 FROM t1 LIMIT 1 INTO @b; Query OK, 1 row affected (0.00 sec) mysql>
Because the query results of the statements SELECT m1 FROM t1 LIMIT 1
and SELECT n1 FROM t1 LIMIT 1
have only one value, so they It can be directly assigned to variable a
or b
. Let’s check the values of these two variables:
mysql> SELECT @a, @b; +------+------+ | @a | @b | +------+------+ | 1 | a | +------+------+ 1 row in set (0.00 sec) mysql>
If our query result is a record with multiple column values in the record, we want to assign these values to different variables. , only the INTO
statement can be used:
mysql> SELECT m1, n1 FROM t1 LIMIT 1 INTO @a, @b; Query OK, 1 row affected (0.00 sec) mysql>
The result set of this query statement only contains one record, we put the value of the m1
column of this record The value is assigned to variable a
, and the value of column n1
is assigned to variable b
.
End of statement delimiter
At the interactive interface of the MySQL
client, when we complete the keyboard input and press the Enter key, MySQL
The client will detect whether the content we input contains one of the three symbols ;
, \g
or \G
. If so, it will input us. content is sent to the server. In this way, if we want to send multiple statements to the server at once, we need to write these statements in one line, such as this:
mysql> SELECT * FROM t1 LIMIT 1;SELECT * FROM t2 LIMIT 1;SELECT * FROM t3 LIMIT 1; +------+------+ | m1 | n1 | +------+------+ | 1 | a | +------+------+ 1 row in set (0.00 sec) +------+------+ | m2 | n2 | +------+------+ | 2 | b | +------+------+ 1 row in set (0.00 sec) +------+------+ | m3 | n3 | +------+------+ | 3 | c | +------+------+ 1 row in set (0.00 sec) mysql>
The reason for this inconvenience is that, MySQL
The symbol used by the client to detect the end of input is the same as the symbol that separates each statement! In fact, we can also use the delimiter
command to customize the symbol at the end of the MySQL
detection statement input, which is the so-called statement end delimiter
, such as this:
mysql> delimiter $ mysql> SELECT * FROM t1 LIMIT 1; -> SELECT * FROM t2 LIMIT 1; -> SELECT * FROM t3 LIMIT 1; -> $ +------+------+ | m1 | n1 | +------+------+ | 1 | a | +------+------+ 1 row in set (0.00 sec) +------+------+ | m2 | n2 | +------+------+ | 2 | b | +------+------+ 1 row in set (0.00 sec) +------+------+ | m3 | n3 | +------+------+ | 3 | c | +------+------+ 1 row in set (0.00 sec) mysql>
delimiter $
命令意味着修改语句结束分隔符为$
,也就是说之后MySQL
客户端检测用户语句输入结束的符号为$
。上边例子中我们虽然连续输入了3个以分号;
结尾的查询语句并且按了回车键,但是输入的内容并没有被提交,直到敲下$
符号并回车,MySQL
客户端才会将我们输入的内容提交到服务器,此时我们输入的内容里已经包含了3个独立的查询语句了,所以返回了3个结果集。
我们也可以将语句结束分隔符
重新定义为$
以外的其他包含单个或多个字符的字符串,比方说这样:
mysql> delimiter EOF mysql> SELECT * FROM t1 LIMIT 1; -> SELECT * FROM t2 LIMIT 1; -> SELECT * FROM t3 LIMIT 1; -> EOF +------+------+ | m1 | n1 | +------+------+ | 1 | a | +------+------+ 1 row in set (0.00 sec) +------+------+ | m2 | n2 | +------+------+ | 2 | b | +------+------+ 1 row in set (0.00 sec) +------+------+ | m3 | n3 | +------+------+ | 3 | c | +------+------+ 1 row in set (0.00 sec) mysql>
我们这里采用了EOF
作为MySQL
客户端检测输入结束的符号,是不是很easy啊!当然,这个只是为了方便我们一次性输入多个语句,在输入完成之后最好还是改回我们常用的分号;
吧:
mysql> delimiter ;
小贴士: 我们应该避免使用反斜杠(\)字符作为语句结束分隔符,因为这是MySQL的转义字符。
推荐学习:mysql视频教程
The above is the detailed content of Let's talk about the basics of MySQL custom variables and statement end delimiters. For more information, please follow other related articles on the PHP Chinese website!

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

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.

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),

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.

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

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