Use of MySQL database (1) - database addition, deletion, modification and query

Basic concepts of database for beginners

foreword

Through the previously installed database, we can use it to learn SQLsome usages of the statement. This article mainly explains some methods and formats of adding, deleting, modifying and querying the database.
Note: SQLThe format of the sentence is actually relatively easy to understand. Its sentence is completely composed of highly descriptive English words. Let’s take a look together.

1. Database information

first let's log into the database management system first

1.1 Login to the database

-uDirectly use and parameters in the server -pto log in to the database

mysql -u用户 -p密码

insert image description here
The meaning in the picture is:

commands end with ; or \g means that the end of the command needs to be added; or \g.
version: The version information of the database is 8.0.30.
At the same time, there are queries that display some help information: help, \h, etc. When a statement is written and found to be wrong, you can also use \c to prevent it from executing.

You can also use SQL statements to display version information

mysql> select version();
+-----------+
| version() |
+-----------+
| 8.0.30    |
+-----------+
1 row in set (0.00 sec)

1.2 Display database character set information

MySQL8.0The default is to support the character set of utf-8. If you use the MySQL5.0left and right databases, you need to set the format in the database utf-8to display Chinese content.

mysql> show variables like '%character%';
+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8mb4                    |
| character_set_connection | utf8mb4                    |
| character_set_database   | utf8mb4                    |
| character_set_filesystem | binary                     |
| character_set_results    | utf8mb4                    |
| character_set_server     | utf8mb4                    |
| character_set_system     | utf8mb3                    |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+
8 rows in set (0.23 sec)

If it is on MySQL5.0the left or right version, you can set the utf-8 character set when creating the database.

create database (数据库名) character set utf8 collate utf8_bin;

1.3 Display the storage engine of the database

mysql8.0innodbThe storage engine used by default above is

mysql> show global variables like '%storage_engine%';
+---------------------------------+-----------+
| Variable_name                   | Value     |
+---------------------------------+-----------+
| default_storage_engine          | InnoDB    |
| default_tmp_storage_engine      | InnoDB    |
| disabled_storage_engines        |           |
| internal_tmp_mem_storage_engine | TempTable |
+---------------------------------+-----------+
4 rows in set (0.02 sec)

Of course, you can also check mysqlwhich storage engines the database supports

mysql> show engines;
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| Engine             | Support | Comment                                                        | Transactions | XA   | Savepoints |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
| ARCHIVE            | YES     | Archive storage engine                                         | NO           | NO   | NO         |
| BLACKHOLE          | YES     | /dev/null storage engine (anything you write to it disappears) | NO           | NO   | NO         |
| MRG_MYISAM         | YES     | Collection of identical MyISAM tables                          | NO           | NO   | NO         |
| FEDERATED          | NO      | Federated MySQL storage engine                                 | NULL         | NULL | NULL       |
| MyISAM             | YES     | MyISAM storage engine                                          | NO           | NO   | NO         |
| PERFORMANCE_SCHEMA | YES     | Performance Schema                                             | NO           | NO   | NO         |
| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |
| MEMORY             | YES     | Hash based, stored in memory, useful for temporary tables      | NO           | NO   | NO         |
| CSV                | YES     | CSV storage engine                                             | NO           | NO   | NO         |
+--------------------+---------+----------------------------------------------------------------+--------------+------+------------+
9 rows in set (0.00 sec)

2. Database SQL statement

2.1 Display database information

mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

By default, these databases are library files that come with the installation. Let’s take a look at their specific functions.

  • information_schema is mainly used to provide access to database metadata, which is data about data, such as database name and table name, column data type or access rights. Information about all other databases in the mysql management system is stored here.
  • performance_schema is used to monitor the resource consumption and resource waiting of MySQL server in a lower-level running process, which can be regarded as a storage engine.
  • sys: view, help development, monitor MySQL performance.
  • mysql: mainly used to store information such as MySQL user accounts, permissions, stored procedures, and transaction definitions.

I still remember what I said at the beginning, you can add a semicolon at the end of the statement, or you can add it \g, let’s see \gwhat the effect is.

mysql> show databases \g
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sys                |
+--------------------+
4 rows in set (0.00 sec)

The effect is the same as that displayed by the semicolon, then replace it with \G and have a look again

mysql> show databases \G
*************************** 1. row ***************************
Database: information_schema
*************************** 2. row ***************************
Database: mysql
*************************** 3. row ***************************
Database: performance_schema
*************************** 4. row ***************************
Database: sys
4 rows in set (0.00 sec)

The result displayed by changing to G does not have the previous frame, but is displayed in the form of horizontal lines line by line.

Generally, if we create a database, we can use this syntax to display the information of the database.
command syntax

show create database 数据库名称

Example:

mysql> show create database book;
+----------+--------------------------------------------------------------------------------------------------------------------------------+
| Database | Create Database                                                                                                                |
+----------+--------------------------------------------------------------------------------------------------------------------------------+
| book     | CREATE DATABASE `book` /*!40100 DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci */ /*!80016 DEFAULT ENCRYPTION='N' */ |
+----------+--------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

The information that can be seen includes the name of the database, character set information, or some information such as whether to encrypt or not.

2.2 Create a database

Syntax for creating a database

create database 数据库名;

example

mysql> create database book;
Query OK, 1 row affected (0.00 sec)

The output Query OK.1 row affected (0.00 sec) indicates that the creation is successful

Suppose you want to create a create database, whether it can be created successfully

mysql> create database create;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'create' at line 1

You can see that it cannot be created successfully. This is because create is a reserved word in the database, so if you want to create successfully, you need to add backticks ``, continue to look at it.

mysql> create database `create`;
Query OK, 1 row affected (0.01 sec)

Because some special characters are included when creating the database, the creation will also fail;

mysql> create database student!;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '!' at line 1
mysql> create database `student!`;
Query OK, 1 row affected (0.07 sec)

In the same way, the solution is to add backticks to create a successful.

2.3 Delete the database

Syntax for dropping a database

drop database 数据库名称

Example:

mysql> drop database book;
Query OK, 3 rows affected (0.05 sec)

Since deleting a database is a particularly serious matter, it is not recommended to do so unless necessary.

Summarize

That's all for the above content, and the next article will talk about the SQL statement of the database table. If you think the content is okay, you can give it a thumbs up and support it!insert image description here

Guess you like

Origin blog.csdn.net/rhn_111/article/details/129526435