MySQL培训教程:MySQL系统信息函数有哪些
最新学讯:近期OCP认证正在报名中,因考试人员较多请尽快报名获取最近考试时间,报名费用请联系在线老师,甲骨文官方认证,报名从速!
我要咨询MySQL系统信息函数有哪些?VERSION()返回数据库版本号,CONNECTION_ID()返回数据库的连接次数,DATABASE()、SCHEMA()返回当前数据库名,USER()、SYSTEM_USER()、SESSION_USER()返回当前用户,CURRENT_USER()、CURRENT_USER返回当前用户,CHARSET(str)返回字符串str的字符集,COLLATION(str)返回字符串str的字符排列方式,LAST_INSERT_ID()返回最后生成的AUTO_INCREMENT值
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()获取最后自动生成的值