Detailed explanation of system information functions in MySQL notes

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.