MySQL: database related operations

Create a database

In MySQL, you can use the CREATE DATABASEstatement to create a database, syntax is as follows:

CREATE DATABASE [IF NOT EXISTS] <数据库名>
[[DEFAULT] CHARACTER SET <字符集名>] 
[[DEFAULT] COLLATE <校对规则名>];

[ ]The content in is optional. The syntax is as follows:

  • <数据库名>: Create the name of the database. MySQL's data storage area will represent the MySQL database in the form of a directory, so the database name must conform to the operating system's folder naming rules, and cannot begin with a number. Try to be practical. Note that MySQL is not case sensitive.
  • IF NOT EXISTS: Judge before creating a database, and only perform operations if the database does not yet exist. This option can be used to avoid repeated creation errors that already exist in the database.
  • [DEFAULT] CHARACTER SET: Specify the character set of the database. The purpose of specifying the character set is to avoid garbled data stored in the database. If you do not specify a character set when creating a database, then the system's default character set is used.
  • [DEFAULT] COLLATE: Specify the default proofreading rules of the character set.

MySQL's character set (CHARACTER) and proofreading rules (COLLATION) are two different concepts. The character set is used to define how MySQL stores strings, and the proofreading rules define how strings are compared. Later we will explain MySQL's character set and proofreading rules separately.

View or display the database

In MySQL, you can use the SHOW DATABASESstatement to view or display the database user privileges within the current range. The syntax format for viewing the database is:

SHOW DATABASES [LIKE '数据库名'];

The syntax is as follows:

  • LIKEThe clause is optional and is used to match the specified database name. LIKEThe clauses can be matched partially or completely.
  • The database name is ' 'surrounded by single quotes .
mysql> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| mysql              |
| performance_schema |
| sakila             |
| sys                |
| world              |
+--------------------+
6 row in set (0.22 sec)

It can be found that there are 6 databases in the above list. They are all created automatically by the system when MySQL is installed. Their respective functions are as follows:

  • information_schema: Mainly stores some database object information in the system, such as user table information, column information, authority information, character set information, and partition information.
  • mysql: The core database of MySQL is similar to the master table in SQL Server. It is mainly responsible for storing database users, user access rights, and other control and management information that MySQL itself needs to use. Commonly used is to modify the root user password in the user table of the mysql database.
  • performance_schema: Mainly used to collect database server performance parameters.
  • sakila: Sample database provided by MySQL, the database has a total of 16 tables, these data tables are relatively common, when designing the database, you can refer to these sample data tables to quickly complete the required data tables.
  • sys: MySQL 5.7 will have an additional sys database after installation. The sys database mainly provides some views, and the data comes from the performance_schema, mainly to allow developers and users to more easily view performance issues.
  • world: The world database is a database automatically created by MySQL. The database includes only 3 data tables, which respectively save the city, country, and language used by the country.

Modify the database

In MySQL, you can be used ALTER DATABASEto modify or there has been created a database of the relevant parameters. Modify the syntax format of the database as:

ALTER DATABASE [数据库名] { 
[ DEFAULT ] CHARACTER SET <字符集名> |
[ DEFAULT ] COLLATE <校对规则名>
}

The syntax is as follows:

  • ALTER DATABASE Used to change the global characteristics of the database.
  • Use ALTER DATABASEneed to get the database ALTERpermissions.
  • The database name can be ignored, and the statement corresponds to the default database.
  • CHARACTER SET The clause is used to change the default database character set.

Delete the database

In MySQL, when you need to delete a database has been created, you can use DROP DATABASEstatement. The syntax format is:

DROP DATABASE [ IF EXISTS ] <数据库名>

18 MySQL select database
19 MySQL storage engine
20 Introduction to MySQL data type
21 MySQL integer type
22 MySQL decimal type
23 MySQL date and time type
24 MySQL string type
25 MySQL binary type
26 MySQL create data table
27 MySQL modify data table
28 MySQL delete data table
29 MySQL primary key
30 MySQL foreign key constraint
31 MySQL unique constraint
32 MySQL Check Constraints
33 MySQL Default
34 MySQL Non-Null Constraints
35 MySQL View Constraints in
Tables 36 MySQL Query Data Table
37 MySQL Deduplication
38 MySQL Set Alias
39 MySQL Limit Number of
Records in Query Results
40 MySQL Sort Query Results 41 MySQL Conditional Query
42 MySQL Common Operators
43 MySQL Inner Connection Query
44 MySQL external connection query
45 MySQL subquery
46 MySQL group query
47 MySQL specified filter conditions
48 MySQL regular expression query
49 MySQL insert data
50 MySQL modify data
51 MySQL delete data
52 Introduction to
MySQL view
53 MySQL create view 54 MySQL modify view
55 MySQL delete view
56 MySQL custom function
57 MySQL stored procedure introduction
58 MySQL create stored procedure
59 MySQL modify stored procedure
60 MySQL delete stored procedure
61 MySQL trigger introduction
62 MySQL create trigger
63 MySQL modify and delete trigger
64 MySQL Index Introduction
65 MySQL Create Index
66 MySQL Modify and Delete Index
67 MySQL Create User
68 MySQL Modify User
69 MySQL Delete User
70 MySQL User Authorization
71 MySQL Delete User Permission
72 MySQL Transaction
73 MySQL Database Backup
74 MySQL Database Recovery
75
MySQL Workbench Tutorial Home> MySQL
Reading Number: 77344
MySQL Delete Database (DROP DATABASE statement)
<MySQL modify database MySQL select database>

C language Chinese website has launched tutorial classes, including "C language tutorial class, C ++ tutorial class, algorithm / data structure tutorial class", all of which are one-to-one teaching: one-to-one tutorial + one-to-one question answering + homework assignment + project Practice + permanent learning. QQ online, respond at any time!

When the database is no longer in use, it should be deleted to ensure that valid data is stored in the database storage space. Deleting a database deletes the existing database from the disk space. After the deletion, all data in the database will also be deleted.

In MySQL, when you need to delete an already created database, you can use the DROP DATABASE statement. The syntax format is:
DROP DATABASE [IF EXISTS] <database name>

The syntax is as follows:

  • <数据库名>: Specify the database name to be deleted.
  • IF EXISTS: Used to prevent errors when the database does not exist.
  • DROP DATABASE: Delete all tables in the database and delete the database at the same time. Be very careful when using this statement to avoid erroneous deletion. If you want to use DROP DATABASE, you need to get the database DROPpermissions.

Note: After MySQL installation, the system will automatically create a file named information_schemaand mysqltwo database systems, database systems and databases related to store some information, if you delete these two databases, MySQL will not work properly.

Use the DROP DATABASEcommand to be very careful, after executing the command, MySQL will not give any confirmation prompt. DROP DATABASEAfter deleting the database, all data tables and data stored in the database will also be deleted together and cannot be recovered. Therefore, it is best to back up the database before deleting it. The method of backing up the database will be explained later in the tutorial.

Select database

In MySQL, USEstatements are used to complete the jump from one database to another.

When used CREATE DATABASEafter creating a database statement, the database does not automatically become the database, need USEto specify the current database. The syntax format is:

USE <数据库名>

This statement can tell MySQL to use <数据库名>the indicated database as the current database. The database remains the default database until the end of the discourse, or until I met a different USEstatement.

Only use USEafter the statement to specify a database as the current database to the data objects stored in the database and perform operations.

Published 94 original articles · liked 0 · visits 722

Guess you like

Origin blog.csdn.net/qq_46578181/article/details/105420504