The system information function is used to query the system information of the mysql database.
VERSION () returns the database VERSION number.
Copy codeThe Code is as follows:
Mysql> select version ();
+ ------------------------- +
| VERSION () |
+ ------------------------- +
| 5.5.28-0ubuntu0. 12.10.2 |
+ ------------------------- +
Row in set (0.00 sec)
I am using the ubuntu-based release, Linux Mint
CONNECTION_ID () returns the number of database connections
Copy codeThe Code is as follows:
Mysql> SELECT CONNECTION_ID ();
+ ----------------- +
| CONNECTION_ID () |
+ ----------------- +
| 36 |
+ ----------------- +
Row in set (0.00 sec)
In fact, each connection to mysql will display
DATABASE (), SCHEMA () returns the current DATABASE name
Copy codeThe Code is as follows:
Mysql> select database (), SCHEMA ();
+ ------------ + ---------- +
| DATABASE () | SCHEMA () |
+ ------------ + ---------- +
| Person |
+ ------------ + ---------- +
Row in set (0.00 sec)
USER (), SYSTEM_USER (), SESSION_USER () returns the current USER
Copy codeThe Code is as follows:
Mysql> select user (), SYSTEM_USER (), SESSION_USER ();
+ ---------------- +
| USER () | SYSTEM_USER () | SESSION_USER () |
+ ---------------- +
| Root @ localhost |
+ ---------------- +
Row in set (0.00 sec)
CURRENT_USER (), CURRENT_USER returns the current user
Copy codeThe Code is as follows:
Mysql> SELECT CURRENT_USER (), CURRENT_USER;
+ ---------------- +
| CURRENT_USER () | CURRENT_USER |
+ ---------------- +
| Root @ localhost |
+ ---------------- +
Row in set (0.00 sec)
The above three functions are the same as the two functions.
CHARSET (str) returns the character set of the str string
Copy codeThe Code is as follows:
Mysql> select charset ('zhang san ');
+ ------------------- +
| CHARSET ('zhang san') |
+ ------------------- +
| Utf8 |
+ ------------------- +
Row in set (0.00 sec)
COLLATION (str) returns the character arrangement of the str string.
Copy codeThe Code is as follows:
Mysql> select collation ('zhang san ');
+ --------------------- +
| COLLATION ('zhang san') |
+ --------------------- +
| Utf8_general_ci |
+ --------------------- +
Row in set (0.00 sec)
LAST_INSERT_ID () returns the final AUTO_INCREMENT value.
Copy codeThe Code is as follows:
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 preceding statement first creates a table t1 with an auto-increment field id.
Insert NULL three times to enable auto-increment.
After confirming that the data already exists, use LAST_INSERT_ID () to obtain the last automatically generated value.