Table of contents
2. Character set and verification rules
1. Check the system default character set and verification rules
2. Check the character sets and verification rules supported by the database
3. The impact of verification rules on the database
1. View the database and the current database
1. Create a database
1. Create database rules
grammar:
CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]
create_specification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
2. Create a case
create database db1;
create database db2 charset=utf8;
create database db3 charset=utf8 collate utf8_general_ci;
2. Character set and verification rules
The character set of the database refers to the encoding method used for characters stored in the database . Different character sets can represent different character ranges and sizes. The verification rules of the database refer to the rules followed when comparing and sorting characters in the database . Different verification rules will affect the query results and performance.
1. Check the system default character set and verification rules
show variables like 'character_set_database';
show variables like 'collation_database';
2. Check the character sets and verification rules supported by the database
show charset;
\
show collation;
3. The impact of verification rules on the database
create database option1 collate utf8_general_ci;
use option1;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
create database option2 collate utf8_bin;
use option2;
create table person(name varchar(20));
insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
(3) Search and sort the tables in the two databases
The database option1 verification rule uses utf8_ general_ ci [ case-insensitive ]
use option1;
mysql> select * from person where name='a';
select * from person order by name;
The database option2 verification rule uses utf8_ general_ ci [ case-sensitive ]
use option2;
mysql> select * from person where name='a';
select * from person order by name;
3. Manipulate the database
1. View the database and the current database
show databases;
select database();
2. Display the creation statement
show create database option1;
3. Modify the database
Modifying the database mainly refers to modifying the character set and verification rules of the database
ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
alter database option1 charset=gbk;
The above code changes the option1 database character set to gbk.
4. Delete the database
drop database option2;
5. Backup and recovery
(1) Backup
mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径
Example: Back up the option1 library to a file (exit the connection)
mysqldump -P3306 -uroot -p -B option1 >test1.sql
You can see that after the backup, there is a test.sql file in the path. Next, use vim to open it and take a look at the content.
2 (restore database)
source /var/lib/mysql/test1.sql;
(3) Precautions
mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
# mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径
6. Check the connection status
show processlist;