[MySQL] Basic operations of the database
1. Create a database—create
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
- capitalized keywords
- [] is optional
- CHARACTER SET: Specifies the character set used by the database
- COLLATE: Specifies the validation rules for the database character set
1.1 Character set and verification rules
When creating a database, there are two code sets:
1. Database code set - the database will store data in the future
2. Database verification set——supports the encoding used by the database for field comparison, and is essentially an encoding format used to read data in the database
No matter what operation is performed on the data, the database must ensure that the operation and encoding must match to prevent garbled characters
1.1.1 View the system default character set and verification rules
show variables like 'character_set_database';//查看默认字符集
show variables like 'collation_database';//查看默认校验规则
show charset; View the character set supported by the database
show collation; View the character set verification rules supported by the database
1.1.2 Create database by default
if not exists: Create if it does not exist, and return warning if it exists
1.1.3 Specify code set to build database
key statement
charset=utf8/设置字符集*/ collate utf8_general_ci/*设置校验规则*/
1.2 The essence of database construction
In the previous article,show databases
it was said that the essence of a database is a directory, so it can be found by directly creating a directory under the specified folder , but it is not recommended to do so
2. View the database and its related properties—show
2.1 Display all databases
As mentioned earlier, show databases
display all databases under mysql
2.2 Display the database creation statement
show create database 数据库名;
/ ... / is not a comment, he said that if the mysql version is greater than 4.01, the d2 creation statement becomes
CREATE DATABASE
d2 DEFAULT CHARACTER SET utf8 COLLATE utf8_bin;
3.2 Display the current database
select database();
3. Modify the database—alter
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
The modification of the database mainly refers to modifying the character set of the database, and the verification rules
As shown in the figure below, the original check code of database d2 is utf8_bin, and its check code is changed to utf8 by ALTER
4. Database deletion—drop
If the database exists, delete it
DROP DATABASE [IF EXISTS] db_ name;
The result after performing the delete:
- The corresponding database cannot be seen inside the database
- The corresponding database folder is deleted, cascaded deletion, and all the data tables in it are deleted
Note: Don't delete the database at will, otherwise you will become a man who gets 600,000 in 3 months
V. Supplement: The impact of verification rules on the database
What is the role of the aforementioned encoding set in the database? Take a look at a case
Create a database with validation rules using utf8_ general_ ci [case insensitive]
Case-sensitive queries and results [utf8_bin]
6. Database backup and restoration (the most important)
6.1 Backup and restore of database
As mentioned earlier, the essence of the database is the file directory under Linux. The easiest way is to copy that directory directly, but this method is not very portable. Let me introduce the commonly used backup methods
mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径
Step 1: Back up the database.
Not only the data in the database is backed up in the sql file, but also the previous effective operations are also backed up.
Step 2: Delete the database
Step Three: Restoring the Database
6.2 Other situations
6.2.1 Backup table only
mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
6.2.2 Backing up multiple databases
mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径
6.2.3 -B option
If the -B parameter is not included when backing up a database, when restoring the database, you need to create an empty database first, then use (use db_name) the database, and then use source to restore