set语句的学习: 使用select定义用户变量的实践 将如下语句改成select的形式: set @VAR=(select sum(amount) from penalties); 我的修改: select @VAR:=(select sum(amount) from penalties); 我这样改,虽然是可以的。但是,对比的书中的答案,发现这样的
set语句的学习:
使用select定义用户变量的实践
将如下语句改成select的形式:
set @VAR=(select sum(amount) from penalties);
我的修改:
select @VAR:=(select sum(amount) from penalties);
我这样改,虽然是可以的。但是,对比的书中的答案,发现这样的方式太蹩脚了。说明没有很好地理解select定义变量的本质。
在select中,发现有一个用户变量,如果没有定义,那么就会初始化。
select子句原来的方式根本不受到影响。只是增加了用户变量。所以,还是按照原来的方式使用select子句。那么像:select sum(amount) from penalties。增加变量就成
了:select @VAR:=sum(amount) from penalties。
将sum(amount)的结果赋给变量@VAR:。变量前面有select,那用户么就是显示该变量了。
笔记部分:mysql变量的术语分类:
1.用户变量:以"@"开始,形式为"@变量名"
用户变量跟mysql客户端是绑定的,设置的变量,只对当前用户使用的客户端生效
2.全局变量:定义时,以如下两种形式出现,set GLOBAL 变量名 或者 set @@global.变量名
对所有客户端生效。只有具有super权限才可以设置全局变量
3.会话变量:只对连接的客户端有效。
4.局部变量:作用范围在begin到end语句块之间。在该语句块里设置的变量
declare语句专门用于定义局部变量。set语句是设置不同类型的变量,包括会话变量和全局变量
通俗理解术语之间的区别:
用户定义的变量就叫用户变量。这样理解的话,会话变量和全局变量都可以是用户定义的变量。只是他们是对当前客户端生效还是对所有客户端生效的区别了。所以,用户变量包括了会话变量和全局变量
局部变量与用户变量的区分在于两点:1.用户变量是以"@"开头的。局部变量没有这个符号。2.定义变量不同。用户变量使用set语句,局部变量使用declare语句定义 3.作用范围。局部变量只在begin-end语句块之间有效。在begin-end语句块运行完之后,局部变量就消失了。
所以,最后它们之间的层次关系是:变量包括局部变量和用户变量。用户变量包括会话变量和全局变量。
使用备忘,set @var 若没有指定GLOBAL 或SESSION ,那么默认将会定义用户变量
两种方式定义用户变量:
1."=",如 set @a =3,@a:=5
2.":="。select常常这样使用
总结:使用select 和set设置变量的区别,set可以使用以上两种形式设置变量。而select只能使用":="的形式设置变量
实践积累:用户变量在mysql客户端退出后,会自动消失。之后我打开客户端,使用"select @a;" 显示变了的值为null。说明,未定义的变量初始化是null
实际中的问题
设置常量对group_concat()的配置影响:
SET @@GROUP_CONCAT_MAX_LEN=4
手册中提到设置的语法是这样的:
SET [SESSION | GLOBAL] group_concat_max_len = val;
以下两种形式都能达到达到同样的效果,但是有什么区别?
SET @@global.GROUP_CONCAT_MAX_LEN=4;
global可以省略,那么就变成了:SET @@GROUP_CONCAT_MAX_LEN=4;
2011.2.25
之前的理解不怎么准确。现在对加深理解后的地方进行总结。
mysql中变量的层次关系是:大体包括用户变量和系统变量。系统变量包括系统会话变量和系统全局变量。
我是这样理解相互之间的区别:
因为用户变量就是用户定义的变量,系统变量就是mysql定义和维护的变量。所以,用户变量与系统变量的区别在于,是谁在管理这些变量。mysql一启动的时候就会读取系统变量(这样做目的是可以确定mysql的以何种机制或模式运行)。 系统会话变量与用户变量都是在当前客户端退出后消失。他们之间的区别可以这样理解,虽然常常看到"set @@varible"的形式去改变系统变量的值,但是并不涉及到定义系统变量。用户变量是可以自己定义(初始化)。系统变量按照只是在改变值。
局部变量只在begin-end语句块中定义并有效。执行到该语句块之后就消失了。定义的方式有明显的特点,使用declare语句。
为什么看到使用系统变量以"@@变量名"和"变量名"的形式出现,怎么理解两者形式的区别?
使用系统变量理论上是可以使用两种形式:1.前面带有符号"@@" 2.符号省略。比如我会看的如下形式:CURRENT_USER。但是,约定系统变量要使用"@@变量名"的形式,就是在前面加上符号"@@"。
为什么会出现CURRENT_USER这样没有符号的情况?看书籍《SQL For MySQL Developers A Comprehensive Tutorial and Reference》大致说明的原因,这样做是为了与其他的SQL产品保持一致。

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

MySQL is suitable for small and large enterprises. 1) Small businesses can use MySQL for basic data management, such as storing customer information. 2) Large enterprises can use MySQL to process massive data and complex business logic to optimize query performance and transaction processing.

InnoDB effectively prevents phantom reading through Next-KeyLocking mechanism. 1) Next-KeyLocking combines row lock and gap lock to lock records and their gaps to prevent new records from being inserted. 2) In practical applications, by optimizing query and adjusting isolation levels, lock competition can be reduced and concurrency performance can be improved.

MySQL is not a programming language, but its query language SQL has the characteristics of a programming language: 1. SQL supports conditional judgment, loops and variable operations; 2. Through stored procedures, triggers and functions, users can perform complex logical operations in the database.

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.


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.

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

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

SublimeText3 Linux new version
SublimeText3 Linux latest version

SublimeText3 Chinese version
Chinese version, very easy to use