First, the basic use of mysql
1. Connect to mysql
mysql [-h host] -u user -p
Description :
① If you do not write [-h host], the default connection is the host.
②If you need to log in to another mysql, you need to modify the configuration. Generally, remote login is not allowed.
2.mysql shutdown and startup
Second, the operation of the library
1. Check which databases are available:
show databases;
2. Create a database:
create database class_1;
Complete syntax:
create database [if not exists] db_name [create_specification [, create_specification] …]
create_specification includes:
[DEFAULT] CHARACTER SET charset_name
[DEFAULT] COLLATE collation_name
description:
①[] is optional, you can write it yourself or use the default one;
②CHARACTER SET is the character set used by the database;
③COLLATE is the comparison method of the specified database character set, the default is utf8_general_ci; ④Check the
system default character set and Validation rules command:
show variables like 'character_set_database';
show variables like 'collation_database';
Verification rules:
1. Case sensitive
utf8_general_ci case insensitive (default verification rule)
utf8_bin case sensitive
2. Affect sorting
① utf8_general_ci is sorted by character
② utf8_bin is sorted by ascii
Description:
①Keywords are best to use uppercase
②/ !40100 DEFAULT CHARACTER SET utf8 / means the version is greater than 4.0
3. Delete the database:
drop database class_1;
4. Check which users are currently connected
show processlist;
5. Backup and restore the database:
Backup the database, in a common command line:
mysqldump -u root -p database name> path.sql
restore :
source C:\Users\lenovo\Desktop\class_1.sql
backup table:
mysqldump -u root -p database name table name 1 table name 2...> path.sql
Back up multiple databases at the same time :
mysqldump -u root -p -B database name 1 database name 2... > path.sql
When backing up the database, with the -B option, there is no need to recreate a new database.
6. Modify the database:
a. Change the character set
alter database test charset=gbk;
b. Change the validation rules
alter database test collate utf8_bin;
7. Delete the library
drop database test;
3. Table operation
Before table operation, you must first use the database
use class_25 that needs to be operated;
1. Create a table:
create table
tt1(
id
int,
name
varchar(20)
);
2. Insert data into the table:
insert into tt1 values(3,'C'),(4,'B'),(5,'c') ,(6,'b');
3. View the contents of the table:
select * from tt1;
4. Sort:
select * from tt1 order by name;
The default check rule is utf8_general_ci, according to the character order.
5. View the statement used to create the database:
show create database class_1 \G
6. View the data table:
show tables;
7. View warnings:
show warnings;
8. View the character set:
show charset;