User variables, local variables, etc. in mysql

 

Reprinted from:

http://dev.mysql.com/doc/refman/5.1/zh/language-structure.html#variables

9.3. User variables

You can save a value in a user variable and refer to it later; this allows you to pass a value from one statement to another. User variables are connection related . That is, a client-defined variable cannot be seen or used by other clients. When a client exits, all variables connected to that client are automatically released.

User variables are of the form @var_name , where the variable name var_name can consist of alphanumeric characters of the current character set, ' . ', ' _ ', and ' $ '. The default character set is cp1252 (Latin1) . The character set can be changed with the --default-character-set option to mysqld . See Section 5.10.1, “Character Sets for Data and Sorting” . User variable names are not case-sensitive.

One way to set user variables is to execute the SET statement:

SET @var_name = expr [, @var_name = expr] ...

For SET , you can use = or := as the allocator. The expr assigned to each variable can be an integer, real, string, or NULL value.

Statements can also be used instead of SET to assign a value to a user variable. In this case, the assignment operator must be := and not =, because = is treated as a comparison operator in non-SET statements :

mysql> SET @t1=0, @t2=0, @t3=0;
mysql> SELECT @t1:=(@t2:=1)+@t3:=4,@t1,@t2,@t3;
+----------------------+------+------+------+
| @t1:=(@t2:=1)+@t3:=4 | @t1 | @t2 | @t3 |
+----------------------+------+------+------+
|                    5 |    5 |    1 |    4 |
+----------------------+------+------+------+

User variables can be used in expressions. Contexts that clearly require literal values ​​are currently excluded, such as the LIMIT clause of a SELECT statement , or the IGNORE number LINES clause of a LOAD DATA statement .

If an uninitialized variable is used, its value is NULL .

If the user variable is assigned a string value, its character set and collation are the same as those of the string. The coercibility of user variables is implicit. ( i.e. the same coercibility of table column values .

Note: In a SELECT statement, expressions are not evaluated until they are sent to the client. This means that expressions containing variables set in the SELECT list cannot be used in the HAVING , GROUP BY or ORDER BY clausesFor example, the following statement does not work as expected:

mysql> SELECT (@aa:=id) AS a(@aa+3) AS b tbl_name HAVING b=5

Aliases of expressions in the SELECT list are referenced in the HAVING clause , using @aa . does not work as expected: @aa does not contain the value of the current row, but the id value of the previously selected row.

The general rule is not to assign a value to a user variable in one part of a statement and use that variable in another part of the same statement. Expected results may be obtained, but are not guaranteed.

Another problem with setting a variable and using it in the same statement is that the type of the variable's default result depends on the type of the variable preceding the statement. The following example illustrates this point:

mysql> SET @a='test';
mysql> SELECT @a,(@a:=20) FROM tbl_name;

For this SELECT statement, MySQL reports to the client that column 1 is a string, and converts all accesses of @a to strings, even though @a is set to a number in row 2 . After executing the SELECT statement, @a is treated as a number for the next statement. 

To avoid this problem, either do not set and use the same variable in the same statement, or set the variable to 0 , 0.0 , or '' to define its type before use.

Unassigned variables have a value of NULL and are of type string.

9.4. System variables

MySQL has access to many system and connection variables. Many variables can be changed dynamically while the server is running. This usually allows you to modify server operation without stopping and restarting the server.

The mysqld server maintains two types of variables. Global variables affect the overall operation of the server. Session variables affect the operation of a specific client connection.

When the server starts, it initializes all global variables to default values. These defaults can be changed in an options file or by specifying options on the command line. These global variables can be changed dynamically after the server is started by connecting to the server and executing the SET GLOBAL var_name  statement. To change global variables, you must have SUPER privilege.

The server also maintains a series of session variables for each connected client. The client's session variables are initialized at connect time with the current values ​​of the corresponding global variables. For dynamic session variables, clients can change them through the SET SESSION var_name  statement. Setting session variables does not require special permissions, but clients can only change their own session variables, not those of other clients.

Changes to a global variable can be seen by any client accessing the global variable. However, it only affects the corresponding session variable initialized from this global variable for the connected client after the change. Does not affect the session variables of the currently connected client ( even if the client executes the SET GLOBAL statement ) .

Global or session variables can be set or retrieved using several syntactic forms. The following example uses sort_buffer_size as an example variable name.

To set the value of a GLOBAL variable, use the following syntax:

mysql> SET GLOBAL sort_buffer_size=value;
mysql> SET @@global.sort_buffer_size=value;

To set the value of a SESSION variable, use the following syntax:

mysql> SET SESSION sort_buffer_size=value;
mysql> SET @@session.sort_buffer_size=value;
mysql> SET sort_buffer_size=value;

LOCAL is synonymous with SESSION .

If you do not specify GLOBAL , SESSION , or LOCAL when setting the variable , SESSION is used by default . See Section 13.5.3, “SET Syntax” .

To retrieve the value of a GLOBAL variable, use the following syntax:

mysql> SELECT @@global.sort_buffer_size;
mysql> SHOW GLOBAL VARIABLES like 'sort_buffer_size';

To retrieve the value of a SESSION variable, use the following syntax:

mysql> SELECT @@sort_buffer_size;
mysql> SELECT @@session.sort_buffer_size;
mysql> SHOW SESSION VARIABLES like 'sort_buffer_size';

Here, LOCAL is also a synonym for SESSION .

When you search for a variable with SELECT @@ var_name ( that is, without specifying global. , session. , or local. ) , MySQL returns the SESSION value if it exists, and the GLOBAL value otherwise.

For SHOW VARIABLES , if GLOBAL , SESSION , or LOCAL is not specified , MySQL returns the SESSION value.

The reason the GLOBAL keywords are required when setting a GLOBAL variable but not when retrieving is to prevent future problems. If we remove a SESSION variable with the same name as a GLOBAL variable , a client with SUPER privilege may accidentally change the GLOBAL variable instead of its own connection's SESSION variable. If we add a SESSION variable with the same name as a GLOBAL variable, customers who want to change the GLOBAL variable may find that only their SESSION variable has been changed.

The study of set statement:

the practice of using select to define user variables Change
the following statement to the form of select:
 set @VAR=(select sum(amount) from penalties);
My modification:
 select @VAR:=(select sum(amount) from penalties);

I change it this way, although it is possible. However, comparing the answers in the book, I found that this method is too lame. The description does not have a good understanding of the nature of the variable defined by select.
In select, it is found that there is a user variable, if it is not defined, it will be initialized.
The original way the select clause is not affected at all. Just added user variables. Therefore, use the select clause in the original way. So like: select sum(amount) from penalties. Adding variables

becomes : select @VAR:=sum(amount) from penalties.

Assign the result of sum(amount) to the variable @VAR:. There is select in front of the variable, then the user will display the variable.


Notes section: Terminology classification of mysql variables:
1. User variables: start with "@", in the form of "@variable name"
User variables are bound to the mysql client, and the set variables are only for the client used by the current user Effective
2. Global variable: When defined, it appears in the following two forms, set GLOBAL variable name or set @@global. The variable name is effective for all clients. Only with super privileges can set global variables 3. Session variables: only valid for connected clients. 




4. Local variables: The scope of action is between the begin and end statement blocks. Variables set in this block
declare statements are used exclusively to define local variables. The set statement sets different types of variables, including session variables and global variables


Popular understanding of the difference between terms:

User-defined variables are called user variables. So understood, both session variables and global variables can be user-defined variables. The only difference is whether they are valid for the current client or for all clients. So, user variables include session variables and global variables

The difference between local variables and user variables lies in two points: 1. User variables start with "@". Local variables do not have this symbol. 2. Define variables differently. User variables use the set statement, and local variables use the declare statement to define 3. Scope. Local variables are only valid between begin-end blocks. After the begin-end block runs, the local variables disappear.

Therefore, the final hierarchical relationship between them is: variables include local variables and user variables. User variables include session variables and global variables.


Use memo, if set @var does not specify GLOBAL or SESSION, then user variables will be defined by default .
There are two ways to define user variables:
1."=", such as set @a =3,@a:=5
2.": =". Select is often used to
summarize: the difference between using select and set to set variables, set can use the above two forms to set variables. And select can only set variables in the form of ":=".
Accumulation of practice: user variables will disappear automatically after the mysql client exits. Then I open the client and use "select @a;" to show that the changed value is null. Description, undefined variable initialization is null

practical problems

Setting constants affects the configuration of group_concat():
SET @@GROUP_CONCAT_MAX_LEN=4
The syntax mentioned in the manual is as follows:
SET [SESSION | GLOBAL] group_concat_max_len = val;

The following two forms can achieve the same effect, but What's the difference?

SET @@global.GROUP_CONCAT_MAX_LEN=4;
global can be omitted, then it becomes: SET @@GROUP_CONCAT_MAX_LEN=4;

2011.2.25

The previous understanding was not very accurate. Now, we will summarize the places after deepening our understanding.

The hierarchical relationship of variables in mysql is: generally including user variables and system variables. System variables include system session variables and system global variables.

This is how I understand the difference between each other:

Because user variables are user-defined variables, and system variables are variables defined and maintained by MySQL. So, the difference between user variables and system variables is who manages these variables. When mysql is started, system variables are read (the purpose of this is to determine which mechanism or mode mysql is running in). Both system session variables and user variables disappear after the current client exits. The difference between them can be understood in this way, although the form of "set @@varible" is often seen to change the value of system variables, it does not involve defining system variables. User variables can be defined (initialized) by themselves. System variables are just changing values.

Local variables are only defined and valid in begin-end blocks. It disappears after the execution of the statement block. The way of definition has obvious characteristics, using the declare statement.

Why do I see the use of system variables in the form of "@@variable name" and "variable name", and how to understand the difference between the two forms?

Using system variables can theoretically use two forms: 1. There is a symbol "@@" in front of it 2. The symbol is omitted. For example, I would look at the following form: CURRENT_USER. However, it is agreed that the system variable should be in the form of "@@variable name", which is to add the symbol "@@" in front.

Why is there no symbol like CURRENT_USER? See the book "SQL For MySQL Developers A Comprehensive Tutorial and Reference" for general reasons, this is for consistency with other SQL products.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326423872&siteId=291194637