使用者變數

1、使用者變數,使用者變數和資料庫連線有關,連線後宣告變數,連線斷開後,自動消失。

2、使用者變數以@開頭,select一個沒有賦值的使用者變數,返回NULL,也就是沒有值,注意取值為NULL與取值為0或者""的區別。

mysql> select @var;

+------+

| @var |

+------+

| NULL |

+------+

3、使用者變數賦值有三種方式:

   a、set @var=(select user from user limit 0,1);

   b、select user into @var from user limit 0,1;

   c、select @var:=user from user limit 0,1;

  注意:a和bc的區別,比如當前@var取值為123,查詢出來的記錄為空,執行a,@var取值為NULL,而執行bc,取值還是123

4、需要注意的是,mysql的變數類似於動態語言,在賦值的時候,確定變數型別。也就是說,int、string都可以賦值給同一個變數。如下:

mysql> set @a=123;

Query OK, 0 rows affected (0.00 sec)

mysql> select @a;

+------+

| @a    |

+------+

|  123  |

+------+

1 row in set (0.00 sec)

mysql> set @a='abc';

Query OK, 0 rows affected (0.00 sec)

mysql> select @a;

+------+

| @a    |

+------+

| abc   |

+------+

1 row in set (0.00 sec)


系統變數

系統變數兩個@@,系統變數的作用域分為全域性作用域和會話作用域,以autocommit為例,說明如下:
1、新建一個連線,只有全域性autocommit,會話autocommit還不存在,這個時候會從全域性autocommit拷貝過來。
2、修改全域性autocommit,只會對新建立的連線起作用,對已經存在的連線不起作用。
3、修改會話autocommit,只對當前連線起作用。
4、不管是全域性autocommit還是會話autocommit,通過客戶端命令設定下去,只對當前執行的服務起作用。mysql服務重啟,還是原來的配置。要想對服務起作用,必須寫入到配置檔案中。

5、注意:系統變數和連線有關,才有會話作用域。有些系統變數和連線無關,比如event_scheduler ,和連線無關,只有全域性作用域,沒有會話作用域。

全域性autocommit的設定和獲取

mysql> set @@global.autocommit=1;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)

mysql> set @@global.autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
| 0 |
+---------------------+
1 row in set (0.00 sec)

會話autocommit的設定和獲取
mysql> set @@session.autocommit =1;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@session.autocommit;
+----------------------+
| @@session.autocommit |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)

mysql> set @@session.autocommit =0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@session.autocommit;
+----------------------+
| @@session.autocommit |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)

注意:不帶作用域修飾,預設是指會話作用域。(特別注意:有些系統變數,不帶作用域,設定不成功,比如tx_isolation,因此最好都要帶上作用域)如下:
mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)

mysql> select @@session.autocommit;
+----------------------+
| @@session.autocommit |
+----------------------+
| 1 |
+----------------------+
1 row in set (0.00 sec)

mysql> set @@autocommit=0;
Query OK, 0 rows affected (0.00 sec)

mysql> select @@autocommit;
+--------------+
| @@autocommit |
+--------------+
| 0 |
+--------------+
1 row in set (0.00 sec)

mysql> select @@global.autocommit;
+---------------------+
| @@global.autocommit |
+---------------------+
| 1 |
+---------------------+
1 row in set (0.00 sec)

mysql> select @@session.autocommit;
+----------------------+
| @@session.autocommit |
+----------------------+
| 0 |
+----------------------+
1 row in set (0.00 sec)

對於系統變數,除了select,還有一種辦法:show variables like '%autocom%'; 如下:
mysql> show global variables like '%autocommit%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | ON |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show session variables like '%autocommit%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)

mysql> show variables like '%autocommit%';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| autocommit | OFF |
+---------------+-------+
1 row in set (0.00 sec)

和select類似,可以show global和session,沒有指定作用域,就是指session作用域

系統變數,對應於啟動時的選項,也就是系統引數。
sql執行過程中,還有狀態變數。


狀態變數