系统信息函数用来查询mysql数据库的系统信息
VERSION()返回数据库版本号
复制代码 代码如下:
mysql> SELECT VERSION();
+-------------------------+
| VERSION()|
+-------------------------+
| 5.5.28-0ubuntu0.12.10.2 |
+-------------------------+
row in set (0.00 sec)
CONNECTION_ID()返回数据库的连接次数
复制代码 代码如下:
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
|36 |
+-----------------+
row in set (0.00 sec)
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)
复制代码 代码如下:
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)
复制代码 代码如下:
mysql> SELECT COLLATION('张三');
+---------------------+
| COLLATION('张三') |
+---------------------+
| utf8_general_ci|
+---------------------+
row in set (0.00 sec)
复制代码 代码如下:
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)
然后分三次插入NULL,使其自增
确认已经存在数据之后,使用LAST_INSERT_ID()获取最后自动生成的值
新闻热点
疑难解答