MySQL introduces system information function
System information function
(1) Function to get the MySQL version number, number of connections and database name
(2) Function to get the user name
(3) Function to obtain the character set and sorting method of a string
(4) Function to obtain the last automatically generated ID value
(Related free learning recommendations: mysql video tutorial)
(1), function to obtain the MySQL version number, number of connections and database name
1.version()
- version() returns the instruction MySQL Server version of the string, this string uses the utf8 character set.
[Example] View the current MySQL version number, the SQL statement is as follows:
mysql> select version();+-----------+| version() |+-----------+| 8.0.16 |+-----------+1 row in set (0.05 sec)
2.connection_id()
- connection_id() returns the current MySQL server The number of connections, each connection has its own unique ID.
[Example] To check the number of connections of the current user, the SQL statement is as follows:
mysql> select connection_id();+-----------------+| connection_id() |+-----------------+| 18 |+-----------------+1 row in set (0.05 sec)
3.show processlist and show full processlist
- processlist command The output results show which threads are running. Not only can you view the number of current connections, but you can also view the current connection status to help identify problematic query statements, etc.
- If you have a root account, you can see the current connections of all users. If you have other ordinary accounts, you can only see the connections you occupy. show processlist only lists the first 100 entries. If you want to list them all, use the show full processlist command.
[Example] Use the showprocesslist command to output the connection information of the current user. The SQL statement is as follows:
mysql> show processlist;+----+-----------------+-----------------+---------+---------+--------+------------------------+------------------+| Id | User | Host | db | Command | Time | State | Info |+----+-----------------+-----------------+---------+---------+--------+------------------------+------------------+| 4 | event_scheduler | localhost | NULL | Daemon | 381487 | Waiting on empty queue | NULL || 18 | root | localhost:60272 | company | Query | 0 | starting | show processlist |+----+-----------------+-----------------+---------+---------+--------+------------------------+------------------+2 rows in set (0.05 sec)
The meaning of each column:
Column | Meaning |
---|---|
id column | The "connection id" assigned by the system when the user logs in to MySQL |
User | Displays the current user. If it is not root, displays the MySQL statements within the user's permissions |
Host | Display which IP and which port this statement was issued from, which can be used to track the user who issued the problematic statement |
db | Display the current status of this process Which database is connected |
Command | Displays the execution command of the current connection. The general values are sleep, query, and connect |
Time | Displays the duration of this state, the unit is seconds |
State | Displays the current use The state of the connected SQL statement, State is just a certain state in the execution of the statement |
Info | Displaying this SQL statement is an important statement for judging the problem statement |
4.database() and schema()
[Example] View the currently used database, the SQL statement is as follows:
mysql> select database(),schema();+------------+----------+| database() | schema() |+------------+----------+| company | company |+------------+----------+1 row in set (0.00 sec)
(2 ), functions to obtain user names
- user(), current_user(), system_user() and session_user() functions return the username and hostname combination currently verified by the MySQL server. This value corresponds to the MySQL account that determines the access rights of the currently logged in user.
[Example] Get the current login user name, the SQL statement is as follows:
- The return result value shows the user name when the current account connects to the server and the connected client host , root is the currently logged in user name, and localhost is the logged in host name.
mysql> select user(),current_user,system_user();+----------------+----------------+----------------+| user() | current_user | system_user() |+----------------+----------------+----------------+| root@localhost | root@localhost | root@localhost |+----------------+----------------+----------------+1 row in set (0.05 sec)
(3), function to obtain the character set and sorting method of a string
1.charset(str)
[Example] Use the charset() function to return the character set used by the string. The SQL statement is as follows:
mysql> select charset('abc'), -> charset(convert('abc' USING latin1 ) ), -> charset(version());+----------------+----------------------------------------+--------------------+| charset('abc') | charset(convert('abc' USING latin1 ) ) | charset(version()) |+----------------+----------------------------------------+--------------------+| gbk | latin1 | utf8 |+----------------+----------------------------------------+--------------------+1 row in set (0.00 sec)
2.collation(str)
[Example] Use the collation() function to return the string arrangement. The SQL statement is as follows:
mysql> select collation('abc'),collation(convert('abc' USING utf8 ));+------------------+---------------------------------------+| collation('abc') | collation(convert('abc' USING utf8 )) |+------------------+---------------------------------------+| gbk_chinese_ci | utf8_general_ci |+------------------+---------------------------------------+1 row in set, 1 warning (0.05 sec)
(4), function to obtain the last automatically generated ID value
[Example] Use select last_insetr_id checks the last automatically generated column value. The execution process is as follows:
①Insert one record at a time
First create the table worker, whose id field has auto_increment constraints, and the SQL statement is as follows:
mysql> create table worker -> ( -> id int auto_increment not null primary key, -> Name varchar(30) -> );Query OK, 0 rows affected (0.21 sec)
Insert two records into the table worker separately:
mysql> insert into worker values(null,'jimy');Query OK, 1 row affected (0.07 sec)mysql> insert into worker values(null,'Tom');Query OK, 1 row affected (0.05 sec)mysql> select * from worker;+----+------+| id | Name |+----+------+| 1 | jimy || 2 | Tom |+----+------+2 rows in set (0.00 sec)
Check the inserted data and find that the id field value of the last inserted record is 2. Use last_insert_id() to view the last automatically generated id value. :
mysql> select last_insert_id();+------------------+| last_insert_id() |+------------------+| 2 |+------------------+1 row in set (0.05 sec)
You can see that when inserting one record at a time, the return value is the id value of the last inserted record.
②Insert multiple records at once
Insert multiple records into the table:
mysql> insert into worker values -> (null,'Kevin'), -> (null,'Michal'), -> (null,'NICK');Query OK, 3 rows affected (0.05 sec)Records: 3 Duplicates: 0 Warnings: 0mysql> SELECT * FROM WORKER;+----+--------+| id | Name |+----+--------+| 1 | jimy || 2 | Tom || 3 | Kevin || 4 | Michal || 5 | NICK |+----+--------+5 rows in set (0.00 sec)
Use last_insert_id to view the last automatically generated id value:
mysql> SELECT LAST_INSERT_ID();+------------------+| LAST_INSERT_ID() |+------------------+| 3 |+------------------+1 row in set (0.00 sec)
When using an insert statement to insert multiple rows, last_insert_id()
only returns the value generated when the first row of data is inserted, here the third record is inserted. The reason for this is that it makes it easy to rely on other servers to replicate the same insert statement.
Tips:
-
last_insert_id
is independent of the table. First insert data into table a, then insert data into table b,last_insert_id
Return the id value in table b.
Related free learning recommendations: mysql database(Video)
The above is the detailed content of MySQL introduces system information function. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting
