[MySQL] Addition, deletion, and modification of MySQL library

1. Library operations

1.1Create database

grammar:

CREATE DATABASE [IF NOT EXISTS] db_name [create_specification [,
create_specification] ...]

create_specification:
	[DEFAULT] CHARACTER SET charset_name
	[DEFAULT] COLLATE collation_name

Notice:

Capitalize keywords

[] is optional

CHARACTER SET: Specifies the character set used by the database

COLLATE: Specifies the verification rules for the database character set

1.2 Create database case

Create a database named db1

Create a database: create database db_name; essentially creates a directory in /var/lib/mysql

create database db1;

When we create a database without specifying a character set and verification rules, the system uses the default character set: utf8, and the verification rules are: utf8_ general_ ci

Create a db2 database using utf8 character set

create database db2 charset=utf8;

Create a db3 database using UTF character set and proofreading rules.

create database db3 charset=utf8 collate utf8_general_ci;

2.Character set and verification rules

When creating a database, there are two encoding sets:

1. Database encoding set – the database will store data in the future

2. Database check set – supports databases and is the encoding used for field comparison. It is essentially an encoding format used to read data in the database.

No matter what operation the database performs on data, it must ensure that the operation and encoding must be consistent.

2.1 View the system default character set and verification rules

show variables like 'character_set_database';
show variables like 'collation_database';

Insert image description here

2.2 Check the character sets supported by the database

show charset;

The character set mainly controls what language is used. For example, utf8 can use Chinese.

2.3 Check the character set verification rules supported by the database

show collation;

2.4 Impact of verification rules on database

not case sensitive

Create a database and use utf8_ general_ ci [case-insensitive] for verification rules.

create database test1 collate utf8_general_ci;

use this database

use test1;

Create a person table in this database

create table person(
	name varchar(20)
	);

Insert some data into the person table

insert into person values('a');
insert into person values('A');
insert into person values('b');
insert into person values('B');
insert into person values('c');
insert into person values('d');

case sensitive

Create a database and use utf8_bin[case-sensitive] for verification rules.

create database test2 collate utf8_bin;
use test2;
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');
insert into person values('c');
insert into person values('d');

Make a query

Case-insensitive queries and results

use test1;
select * from person where name='a';

Insert image description here

Case-sensitive queries and results

use test2;
select * from person where name='a';

Insert image description here

Sort results

Case-insensitive sorting and results:

use test1;
select * from person order by name;

Insert image description here

Case-sensitive sorting and results:

use test2;
select * from person order by name;

Insert image description here

3. Manipulate the database

3.1 View database

show databases;

Insert image description here

3.2 Display the create statement

show create database 数据库名;

Insert image description here

MySQL recommends that we use uppercase keywords, but it is not required.

The backtick `` in the database name is to prevent the database name used from being a keyword.

/*!40100 default… */ This is not a comment, it means that the current mysql version is greater than version 4.01, just execute this sentence

3.3 Modify the database

grammar:

ALTER DATABASE db_name
[alter_spacification [,alter_spacification]...]
alter_spacification:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name

Modifying the database mainly refers to modifying the character set and verification rules of the database

Change the test1 database character set to gbk

alter database test1 charset set gbk;
alter database test1 charset=gbk;

show create database test1;

Insert image description here

3.4 Database deletion

grammar:

DROP DATABASE [IF EXISTS] db_ name;

Result after deletion:

The corresponding database cannot be seen inside the database

The corresponding database folder is deleted, cascade deleted, and all data tables inside are deleted.

Note: Do not delete the database at will

Delete database: drop database dn_name; essentially delete the directory

3.5 Backup and recovery

backup

grammar:

mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径

Back up the mytest library to a file (exit the connection)

mysqldump -P3306 -u root -p -B test1 > ./test1.sql;

At this time, you can open and take a look at the contents of the mytest.sql file. In fact, all our statements for creating a database, building tables, and importing data are loaded into this file.

Insert image description here

reduction

source ./test1.sql;

If the backup is not the entire database, but one of the tables

mysqldump -u root -p 数据库名 表名1 表名2 > 数据库备份存储的文件路径

Back up multiple databases simultaneously

mysqldump -u root -p -B 数据库名1 数据库名2 ... > 数据库存放路径

If you back up a database without the -B parameter, when restoring the database, you need to create an empty database first, then use the database, and then use source to restore.

3.6 Check the connection status

show processlist;

Insert image description here

It can tell us which users are currently connected to our MySQL. If it is found that a user is not your normal login, it is very likely that your database has been invaded. In the future, when you find that your database is slow, you can use this command to check the database connection status.

Guess you like

Origin blog.csdn.net/qq_67582098/article/details/135006741