mysql variable definition (declare, set, @) Example explain

Mysql variable species

  1. User variables: the "@" start, in the form of "@ variable name." User Variables with the mysql client is bound to set variables, only take effect for the current user of the client
  2. Global variables: the definition, appears in two forms, set GLOBAL variable or set @@ global variable names, enter into force for all clients. Only a super privilege to set up a global variable
  3. Session variables: only valid for end customers connected.
  4. Local variables: in scope to begin between the end block. Variable set in the statement in the block. declare statements designed to define local variables. set statements are set different types of variables, including session and global variables

Popular understand the distinction between terms:

User-defined variables called user variables. So understand the words, session variables and global variables can be user-defined variables. They are to take effect only difference is still in effect for all clients of the current client. Therefore, the user variables include the session variables and global variables

Local variables is two things and distinguish user variables: 1 user variables begins with "@" is. Local variables do not have this symbol. 2. Define different variables. User variables set statements, local variables defined declare statement 3. scope. Local variables valid only between the begin-end block. After the begin-end statement block finishes running, the local variables disappeared.

So in the end hierarchical relationship between them is: variables include local variables and user variables. User variables including session and global variables.

 

mysql declare variables defined

MySQL declare variables used to define, in the stored procedures and functions, and prior to declare variables defined by the statement in the BEGIN ... END. And can be defined by repeating a plurality of variables

Declare variables scope Similar to the programming which here is generally between corresponding begin and end. After the end of this variable has no effect, and can not be used. The same programming the same.

Note: declare variable names can not be defined with '@' symbol, mysql do at this point does not intuitive, variable names are often mistaken as parameter or column name.

mysql stored procedure defined using declare variables, examples are as follows:

 

mysql SET-defined variables

mysql set can also be used to set variables, form variables in terms of "@" start, such as: "@ variable name."

mysql SET instance variables defined:

More complex examples:

 

The difference between mysql declare and set variables defined

mysql declare and define variables set, but one without a plus and @ @ this distinction, there are the following differences:

define local variables to declare

@ Is used to define the session variable

Declare variables scope Similar to the programming which here is generally between corresponding begin and end. After the end of this variable has no effect, and can not be used. The same programming the same.

Further kind of variable named session variable (session variable), also known as a user-defined variable (user defined variable). This variable preceding it with the variable name "@" symbol, called a session variable, representative of the session he is useful, this is somewhat similar to global variables. This broad purpose of the variable, because (a connection process is applied in a), this data can be shared between the variable stored procedure is called as long as the codes or a session.

Published 60 original articles · won praise 9 · views 30000 +

Guess you like

Origin blog.csdn.net/qq_44697035/article/details/104393790