System information function of MySQL notes
- 2020-05-17 06:41:04
- OfStack
The system information function queries the system information of the mysql database
VERSION() returns the database version number
mysql> SELECT VERSION();
+-------------------------+
| VERSION() |
+-------------------------+
| 5.5.28-0ubuntu0.12.10.2 |
+-------------------------+
row in set (0.00 sec)
I'm using the ubuntu based distribution here, Linux Mint
CONNECTION_ID() returns the number of connections to the database
mysql> SELECT CONNECTION_ID();
+-----------------+
| CONNECTION_ID() |
+-----------------+
| 36 |
+-----------------+
row in set (0.00 sec)
It actually shows up every time you connect to mysql
DATABASE(), SCHEMA() returns the current database name
mysql> SELECT DATABASE(), SCHEMA();
+------------+----------+
| DATABASE() | SCHEMA() |
+------------+----------+
| person | person |
+------------+----------+
row in set (0.00 sec)
USER(), SYSTEM_USER(), SESSION_USER() return the current 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 return the current user
mysql> SELECT CURRENT_USER(), CURRENT_USER;
+----------------+----------------+
| CURRENT_USER() | CURRENT_USER |
+----------------+----------------+
| root@localhost | root@localhost |
+----------------+----------------+
row in set (0.00 sec)
The three above are identical to these two functions
CHARSET(str) returns the character set of the string str
mysql> SELECT CHARSET(' zhang 3');
+-------------------+
| CHARSET(' zhang 3') |
+-------------------+
| utf8 |
+-------------------+
row in set (0.00 sec)
COLLATION(str) returns the character arrangement of the string str
mysql> SELECT COLLATION(' zhang 3');
+---------------------+
| COLLATION(' zhang 3') |
+---------------------+
| utf8_general_ci |
+---------------------+
row in set (0.00 sec)
LAST_INSERT_ID() returns the last generated AUTO_INCREMENT value
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)
The above statement first creates a table t1 with a self-incrementing field id
Then insert NULL three times to make it self-increment
After confirming that the data already exists, use LAST_INSERT_ID() to get the last automatically generated value