Basic learning of MySQL database (1) Database operation instructions

The difficulty of things is far lower than the fear of things.
The internal storage structure of the database is mainly divided into database, data table and data . This article mainly explains the command operation of the database. The database version used is 8.0.23.

1. Log in to the database

Log in to the database using the following command:

mysql -h服务器地址 -u用户名 -p(回车后输入密码)
D:\software\mysql-8.0.23-winx64\bin>mysql -hlocalhost -uroot -p
Enter password: ******
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 8
Server version: 8.0.23 MySQL Community Server - GPL

Copyright (c) 2000, 2021, Oracle and/or its affiliates.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql>

2. Query all current databases

The command is as follows:

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

Shown here is the default database.

3. Create a new database

The command is as follows:

create database 新的数据库名称 编码;

Among them, the encoding can not be written, then it is the default encoding format. Let's first look at the command to create a new database without encoding:

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

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

Encoded command to create a new database:

mysql> create database person character set gbk;
Query OK, 1 row affected (0.01 sec)

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

4. View the encoding format of the database

The command is as follows:

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

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

Among them, the student database adopts the default encoding format, and the person database adopts the gbk encoding format.

5. Modify the encoding format of the database

The command is as follows:

alter database 数据库名称 character set 编码格式;
mysql> alter database person character set utf8;
Query OK, 1 row affected, 1 warning (0.01 sec)

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

The above code changes the encoding format of the person data from the original gbk to utf8 format.

6. Delete the database

The command is as follows:

drop database 数据库名称;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| person             |
| student            |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> drop database person;
Query OK, 0 rows affected (0.01 sec)

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

The above code successfully deletes the person database.

7. Modify the name of the database

After testing, the following code cannot be used in new data, and it can be used normally before version 5.1.23.

rename database 数据库的旧名称 to 数据库的新名称;

But there is a risk of data loss in this method. For high-version databases, you can refer to this blogger’s article Three methods of renaming MySQL databases

8. Switch database

The command is as follows:

use 数据库名称;
mysql> show databases;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| person             |
| student            |
| sys                |
+--------------------+
6 rows in set (0.00 sec)

mysql> use student;
Database changed

9. View the currently used database name

The command is as follows:

mysql> select database();
+------------+
| database() |
+------------+
| student    |
+------------+
1 row in set (0.00 sec)

Conclusion:
This article is mainly for some basic command operations of the database, and the follow-up will explain the command operations for data tables and data.

You can refer to my other MySQL articles listed below:

MySQL database 8.0.23 novice detailed installation steps

Basic learning of MySQL database (2) Operation instructions of data table

Basic learning of MySQL database (3) Data operation instructions

Guess you like

Origin blog.csdn.net/weixin_44355077/article/details/115698669