Mysql variable introduction and use of

variable

System Variables

Variables provided by the system, not the user-defined, belong to the server level

  • Global Variables

    • View all global variables

      • SHOW GLOBAL VARIABLES;
        
    • Scope: Each time you start the server for all global variables will be assigned an initial value (connection) valid for all sessions, but can not cross restart

  • Session Variables

    • View all session variables

      • SHOW SESSION VARIABLES;
        
  • Check the condition of the system variables

    • SHOW GLOBAL|SESSION VARIABLES LIKE '%关键字%'
      
  • Check the specified value of a system variable

    • SELECT @@global|session.系统变量名;
      
  • Assigned to system variables

    • SET global|session 系统变量名=值;
      
    • SET @@global|session.系统变量名=值;
      
  • note

    • Global level requires home global, session level need to add session, do not write the default session

Custom Variables

  • Description: The user-defined variables are not assigned by the system

  • User Variables

    • Scope: Scope for the current session (connection) is valid, in the same session variables

    • Declare and initialize

      • SET 用户变量名=值;
        SET 用户变量名:=值;
        SELECT @用户变量名:=值;
        
    • Assignment

      • By SET or SELECT, ibid.

      • SELECT 字段 INTO 变量名
        FROM 表;
        
    • View

      • SELECT @变量名;
        
    • Steps for usage:

      • statement
      • Assignment
      • Use (view, compare, arithmetic)
  • Local variables

    • Scope: it is defined only in the effective begin end

    • Applications begin end of the first sentence

    • statement:

      • DECLARE 变量名 类型;
        DECLARE 变量名 类型 DEFAULT 值;
        
    • Assignment

      • @ symbol set manner may be omitted
      • can not be omitted select
    • use

      • SELECT 局部变量名;
        
Published 70 original articles · won praise 43 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_25884515/article/details/103962725