zoukankan      html  css  js  c++  java
  • Mysql(Mariadb)之SET语法分析以及系统变量和用户变量分析(英文&中文)(转载)

    SET

    Syntax

    SET variable_assignment [, variable_assignment] ...
    
    variable_assignment:
          user_var_name = expr
        | [GLOBAL | SESSION] system_var_name = expr
        | [@@global. | @@session. | @@]system_var_name = expr
    

    Contents

    1. Syntax
    2. Description
      1. GLOBAL / SESSION
      2. DEFAULT
    3. Examples
    4. See Also

    One can also set a user variable in any expression with this syntax:

    user_var_name:= expr
    

    Description

    The SET statement assigns values to different types of variables that affect the operation of the server or your client. Older versions of MySQL employed SET OPTION, but this syntax was deprecated in favor of SET without OPTION, and was removed in MariaDB 10.0.

    Changing a system variable by using the SET statement does not make the change permanently. To do so, the change must be made in a configuration file.

    For setting variables on a per-query basis (from MariaDB 10.1.2), see SET STATEMENT.

    See SHOW VARIABLES for documentation on viewing server system variables.

    See Server System Variables for a list of all the system variables.

    GLOBAL / SESSION

    When setting a system variable, the scope can be specified as either GLOBAL or SESSION.

    A global variable change affects all new sessions. It does not affect any currently open sessions, including the one that made the change.

    A session variable change affects the current session only.

    If the variable has a session value, not specifying either GLOBAL or SESSION will be the same as specifying SESSION. If the variable only has a global value, not specifying GLOBAL or SESSION will apply to the change to the global value.

    DEFAULT

    Setting a global variable to DEFAULT will restore it to the server default, and setting a session variable to DEFAULT will restore it to the current global value.

    Examples

    SELECT VARIABLE_NAME, SESSION_VALUE, GLOBAL_VALUE FROM
     INFORMATION_SCHEMA.SYSTEM_VARIABLES WHERE 
      VARIABLE_NAME LIKE 'max_error_count' OR 
      VARIABLE_NAME LIKE 'skip_parallel_replication' OR 
      VARIABLE_NAME LIKE 'innodb_sync_spin_loops';
    +---------------------------+---------------+--------------+
    | VARIABLE_NAME             | SESSION_VALUE | GLOBAL_VALUE |
    +---------------------------+---------------+--------------+
    | MAX_ERROR_COUNT           | 64            | 64           |
    | SKIP_PARALLEL_REPLICATION | OFF           | NULL         |
    | INNODB_SYNC_SPIN_LOOPS    | NULL          | 30           |
    +---------------------------+---------------+--------------+
    

    Setting the session values:

    SET max_error_count=128;Query OK, 0 rows affected (0.000 sec)
    
    SET skip_parallel_replication=ON;Query OK, 0 rows affected (0.000 sec)
    
    SET innodb_sync_spin_loops=60;
    ERROR 1229 (HY000): Variable 'innodb_sync_spin_loops' is a GLOBAL variable 
      and should be set with SET GLOBAL
    
    SELECT VARIABLE_NAME, SESSION_VALUE, GLOBAL_VALUE FROM
     INFORMATION_SCHEMA.SYSTEM_VARIABLES WHERE 
      VARIABLE_NAME LIKE 'max_error_count' OR 
      VARIABLE_NAME LIKE 'skip_parallel_replication' OR 
      VARIABLE_NAME LIKE 'innodb_sync_spin_loops';
    +---------------------------+---------------+--------------+
    | VARIABLE_NAME             | SESSION_VALUE | GLOBAL_VALUE |
    +---------------------------+---------------+--------------+
    | MAX_ERROR_COUNT           | 128           | 64           |
    | SKIP_PARALLEL_REPLICATION | ON            | NULL         |
    | INNODB_SYNC_SPIN_LOOPS    | NULL          | 30           |
    +---------------------------+---------------+--------------+
    

    Setting the global values:

    SET GLOBAL max_error_count=256;
    
    SET GLOBAL skip_parallel_replication=ON;
    ERROR 1228 (HY000): Variable 'skip_parallel_replication' is a SESSION variable 
      and can't be used with SET GLOBAL
    
    SET GLOBAL innodb_sync_spin_loops=120;
    
    SELECT VARIABLE_NAME, SESSION_VALUE, GLOBAL_VALUE FROM
     INFORMATION_SCHEMA.SYSTEM_VARIABLES WHERE 
      VARIABLE_NAME LIKE 'max_error_count' OR 
      VARIABLE_NAME LIKE 'skip_parallel_replication' OR 
      VARIABLE_NAME LIKE 'innodb_sync_spin_loops';
    +---------------------------+---------------+--------------+
    | VARIABLE_NAME             | SESSION_VALUE | GLOBAL_VALUE |
    +---------------------------+---------------+--------------+
    | MAX_ERROR_COUNT           | 128           | 256          |
    | SKIP_PARALLEL_REPLICATION | ON            | NULL         |
    | INNODB_SYNC_SPIN_LOOPS    | NULL          | 120          |
    +---------------------------+---------------+--------------+
    

    SHOW VARIABLES will by default return the session value unless the variable is global only.

    SHOW VARIABLES LIKE 'max_error_count';
    +-----------------+-------+
    | Variable_name   | Value |
    +-----------------+-------+
    | max_error_count | 128   |
    +-----------------+-------+
    
    SHOW VARIABLES LIKE 'skip_parallel_replication';
    +---------------------------+-------+
    | Variable_name             | Value |
    +---------------------------+-------+
    | skip_parallel_replication | ON    |
    +---------------------------+-------+
    
    SHOW VARIABLES LIKE 'innodb_sync_spin_loops';
    +------------------------+-------+
    | Variable_name          | Value |
    +------------------------+-------+
    | innodb_sync_spin_loops | 120   |
    +------------------------+-------+
    

    Using the inplace syntax:

    SELECT (@a:=1);
    +---------+
    | (@a:=1) |
    +---------+
    |       1 |
    +---------+
    
    SELECT @a;
    +------+
    | @a   |
    +------+
    |    1 |
    +------+

    原文:https://mariadb.com/kb/en/set/

    tag1:【系统变量】

    0.系统变量是mysql启动时需要初始化的具有某种特定功能的变量,如error_log系统变量定义了错误日志是否记录。系统变量可以认为有2种形式,一种是系统全局变量(system global variables),一种是系统会话变量(system session variables)。有的系统变量只有全局变量,没有会话变量形式,那么此时会话变量的取值默认就是系统变量的默认值。

    1.系统全局变量与系统会话变量的影响范围,系统全局变量是设置后对所有的新产生的会话产生影响(新会话的系统全局变量和系统会话变量都会变为最新的系统全局变量值)。系统会话变量,仅仅对当前用户的当前会话的系统会话变量产生影响。

    2.系统会话变量的修改,仅仅对当前用户的当前会话的系统会话变量产生影响,set (session) 系统变量名 = 变量值,对当前会话的global 系统变量名的值不产生影响,同理,set global 系统变量名,对当前会话的系统会话变量名不产生影响,但set global 系统变量名会对新建立的会话的global系统变量和session系统变量构成影响。使用set修改系统变量只是动态修改,比如set global error_log = on;设置后,mysql重启后该修改会丢失,mysql启动时会加载配置文件如/etc/my.cnf中的系统变量预置值作为系统变量的初始值。

    3.系统变量的作用原理,同一个系统变量,有的同时有系统会话变量值和系统全局变量值(分别存储在global_variables和session_variables表中),有的只有系统全局变量,没有系统会话变量(此时当前会话的当前系统会话变量的值默认效果就是该全局变量的值的效果)。我们知道某个系统变量对系统的实际作用是通过系统会话变量为准的,比如error_log选项变量,如果系统全局变量值与系统会话变量值不同那么当前会话以系统会话变量值的效果为准,比如error_log的系统会话变量值是错误日志是否开启的标志。

    tag2:【用户变量】

    0.用户变量就是用户自定义的变量,区别于系统变量。

    1.用户变量一般分为用户会话变量和用户局部变量。
    2.用户会话变量,仅仅在整个会话有效期内有效,可以在程序任何地方修改定义。定义方法为set @变量名=变量值;如set @var1=2;select @var2;仅仅在当前会话中有效。当前会话结束则变量销毁。与系统会话变量的定义修改方式基本一样。
    3.用户局部变量,用户局部变量只能在某种特殊代码块中定义。  a:用户局部变量仅仅在当前会话有效。b:用户局部变量仅仅在某个程序段内部有效,其它地方无效,而不是像用户会话变量那样,在任意地方都可以定义和修改(通过set @变量名,如set  @var1=44;),定义用户局部变量需要在某种特殊的区域中,如procedure内部(如果非特殊区域内只能定义和修改用户自定义的用户局部变量,报ERROR 1054 (42S22): Unknown column 'd' in 'field list'错误)。比如我们在一个procedure中set var1=2;select var1;procedure中定义的会话变量也叫做过程变量procedure variables,是用户局部变量的一种,这种变量仅仅在call调用当前procedure时候有效,调用结束过程变量销毁。但是在procedure中定义和修改的用户会话变量仍然是全局有效的。用户局部变量类似于php的函数内定义的变量。

    案例:

    CREATE PROCEDURE prc_test ()
    BEGIN
        DECLARE var2 INT DEFAULT 1;
        SET var2 := var2 + 1;
        SET @var2 := @var2 + 1;
        SELECT  var2, @var2;
    END;
    
    SET @var2 = 1;
    
    CALL prc_test();
    
    var2  @var2
    ---   ---
    2     2
    
    
    CALL prc_test();
    
    var2  @var2
    ---   ---
    2     3
    
    
    CALL prc_test();
    
    var2  @var2
    ---   ---
    2     4
    
    
    
    
    
    
    
    
    

    ​欢迎加我们微信wang1415035017进入微信高级技术群共同进步,或者扫码加入我们哦(V_V) 

  • 相关阅读:
    小程序后端获取openid (php实例)
    原生sql查询返回结果集处理方法
    关于生成的时间戳和当前时间不相符的问题
    数据结构的基本概念学习
    TensorFlow框架(6)之RNN循环神经网络详解
    TensorFlow框架(5)之机器学习实践
    TensorFlow框架(4)之CNN卷积神经网络详解
    TensorFlow框架(3)之MNIST机器学习入门
    TensorFlow框架(2)之TensorBoard详解
    TensorFlow框架(1)之Computational Graph详解
  • 原文地址:https://www.cnblogs.com/qiangshangkeji/p/12422404.html
Copyright © 2011-2022 走看看