The MySQL variable

Source: Internet
Author: User

The content of this article:

    • System variables
    • User variables
      • Local variables

Starting Date: 2018-04-18

System variables:
    • System variables are variables that the system has already defined in advance.
    • system variables generally have their special meanings. For example, some variables represent character sets, some variables represent certain MySQL file locations
    • System variables include session-level variables (variables that are in effect when a session is connected, such as names), and global variables (variables that are in effect) "The global variables and session variables in the system variables are actually using a set of variables, but the conversation variables are only used when the session takes effect." 】
      • Assignment of Session variables: Set variable name = value; "such as common set names =" UTF8 ";" or set @@ 变量 name = value
      • Assignment of global variables: SET global variable name = value;

To view system variables:
    • Show variables;
Calls to System variables:
    • Select @@ 变量 name;

User variables:

    • A user variable is a user-defined variable.
    • To differentiate system variables from custom variables, the system specifies that user-defined variables must use an @ symbol
    • How variables are defined:
      • set @变量名=1
      • SELECT @ Variable Name: = value;
      • Select value into @ variable name;
    • The user variable can be used without declaring the definition, but the default is a null value
    • User variables are session-level variables that take effect only in the secondary connection.

Local variables:
    • Because local variables are user-defined, you can think of local variables as user variables "but different, not in the local use @"
    • Local variables are commonly used in SQL statement blocks, such as stored procedure blocks, trigger blocks, and so on
    • How to define a local variable:
      • Use declare to declare a local variable, where the option default can be followed by a value that is paid to the variable: " a very important step, or it will be set to a user variable"
        • Example: declare MYQ int;
        • Example: declare myq int default 666;
      • Set the value of the variable:
        • Set variable name = value;
      • Gets the value of the variable:
        • Select variable name;
Create procedureMySet ()begin     DeclareMyaint; DeclareMyqint default 777; SelectMya,myq; SetMyq=6; SetMya=666; SelectMya,myq;End; call MySet ();

Add:
    • Some people may find the direct set variable name = value, or you can define a "user variable"; but this is a bad behavior "This behavior ignores the function of the respective variable", because you do not know whether it will conflict with the system variables, so the best user variable plus @
    • because =, there are many places to judge whether equal, in order to avoid ambiguity, you can also use: = To assign a value
    • "The above gives some other assignment methods, but it seems that some are not universal, such as: = only for user variables, so use caution ."

The MySQL variable

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.