MySQL学习笔记19:系统信息函数

系统信息函数用来查询mysql数据库的系统信息

 
VERSION()返回数据库版本号
mysql> SELECT VERSION();
+-------------------------+
| VERSION()               |
+-------------------------+
| 5.5.28-0ubuntu0.12.10.2 |
+-------------------------+
1 row in set (0.00 sec)

我这里用的是基于ubuntu发行版,Linux Mint

 

CONNECTION_ID()返回数据库的连接次数
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
|              36 |
+-----------------+
1 row in set (0.00 sec)

其实每次连接到mysql的时候就会有显示

 

DATABASE()、SCHEMA()返回当前数据库名
mysql> SELECT DATABASE(), SCHEMA();
+------------+----------+
| DATABASE() | SCHEMA() |
+------------+----------+
| person     | person   |
+------------+----------+
1 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 |
+----------------+----------------+----------------+
1 row in set (0.00 sec)

 

CURRENT_USER()、CURRENT_USER返回当前用户
mysql> SELECT CURRENT_USER(), CURRENT_USER;
+----------------+----------------+
| CURRENT_USER() | CURRENT_USER   |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
1 row in set (0.00 sec)

上面的三个和这两个功能是一样的

 

CHARSET(str)返回字符串str的字符集
mysql> SELECT CHARSET('张三');
+-------------------+
| CHARSET('张三')   |
+-------------------+
| utf8              |
+-------------------+
1 row in set (0.00 sec)

 

COLLATION(str)返回字符串str的字符排列方式
mysql> SELECT COLLATION('张三');
+---------------------+
| COLLATION('张三')   |
+---------------------+
| utf8_general_ci     |
+---------------------+
1 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 |
+----+
3 rows in set (0.00 sec)

mysql> SELECT LAST_INSERT_ID();
+------------------+
| LAST_INSERT_ID() |
+------------------+
|                3 |
+------------------+
1 row in set (0.00 sec)

上面的语句首先创建了一张表t1,其中有一个自增字段id

然后分三次插入NULL,使其自增

确认已经存在数据之后,使用LAST_INSERT_ID()获取最后自动生成的值

 

 

posted @ 2013-01-05 17:52  Nerxious  阅读(1414)  评论(0编辑  收藏  举报