系統信息函數用來查詢mysql數據庫的系統信息
VERSION()返回數據庫版本號
復制代碼 代碼如下:
mysql> SELECT VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 5.5.28-0ubuntu0.12.10.2 |
+-------------------------+
row in set (0.00 sec)
我這裡用的是基於ubuntu發行版,Linux Mint
CONNECTION_ID()返回數據庫的連接次數
復制代碼 代碼如下:
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 36 |
+-----------------+
row in set (0.00 sec)
其實每次連接到mysql的時候就會有顯示
DATABASE()、SCHEMA()返回當前數據庫名
復制代碼 代碼如下:
mysql> SELECT DATABASE(), SCHEMA();
+------------+----------+
| DATABASE() | SCHEMA() |
+------------+----------+
| person | person |
+------------+----------+
row in set (0.00 sec)
USER()、SYSTEM_USER()、SESSION_USER()返回當前用戶
復制代碼 代碼如下:
mysql> SELECT USER(), SYSTEM_USER(), SESSION_USER();
+----------------+----------------+----------------+
| USER() | SYSTEM_USER() | SESSION_USER() |
+----------------+----------------+----------------+
| root@localhost | root@localhost | root@localhost |
+----------------+----------------+----------------+
row in set (0.00 sec)
CURRENT_USER()、CURRENT_USER返回當前用戶
復制代碼 代碼如下:
mysql> SELECT CURRENT_USER(), CURRENT_USER;
+----------------+----------------+
| CURRENT_USER() | CURRENT_USER |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
row in set (0.00 sec)
上面的三個和這兩個功能是一樣的
CHARSET(str)返回字符串str的字符集
復制代碼 代碼如下:
mysql> SELECT CHARSET('張三');
+-------------------+
| CHARSET('張三') |
+-------------------+
| utf8 |
+-------------------+
row in set (0.00 sec)
COLLATION(str)返回字符串str的字符排列方式
復制代碼 代碼如下:
mysql> SELECT COLLATION('張三');
+---------------------+
| COLLATION('張三') |
+---------------------+
| utf8_general_ci |
+---------------------+
row in set (0.00 sec)
LAST_INSERT_ID()返回最後生成的AUTO_INCREMENT值
復制代碼 代碼如下:
mysql> CREATE TABLE t1(id INT PRIMARY KEY AUTO_INCREMENT);
Query OK, 0 rows affected (0.10 sec)
mysql> INSERT INTO t1 VALUES(NULL);
Query OK, 1 row affected (0.04 sec)
mysql> INSERT INTO t1 VALUES(NULL);
Query OK, 1 row affected (0.03 sec)
mysql> INSERT INTO t1 VALUES(NULL);
Query OK, 1 row affected (0.04 sec)
mysql> SELECT * FROM t1;
+----+
| id |
+----+
| 1 |
| 2 |
| 3 |
+----+
rows in set (0.00 sec)
mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
| 3 |
+------------------+
row in set (0.00 sec)
上面的語句首先創建了一張表t1,其中有一個自增字段id
然後分三次插入NULL,使其自增
確認已經存在數據之後,使用LAST_INSERT_ID()獲取最後自動生成的值