Basic operations of mysql database

First, the basic use of mysql

1. Connect to mysql
mysql [-h host] -u user -p
write picture description here
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
write picture description here

Second, the operation of the library

1. Check which databases are available:
show databases;
write picture description here
2. Create a database:
create database class_1;
write picture description here
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
write picture description here
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;
write picture description here
4. Check which users are currently connected
show processlist;
write picture description here
5. Backup and restore the database:
Backup the database, in a common command line:
mysqldump -u root -p database name> path.sql
write picture description here
restore :
source C:\Users\lenovo\Desktop\class_1.sql
write picture description here
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 tablett1(
idint,
namevarchar(20)
);
write picture description here
2. Insert data into the table:
insert into tt1 values(3,'C'),(4,'B'),(5,'c') ,(6,'b');
write picture description here
3. View the contents of the table:
select * from tt1;
write picture description here
4. Sort:
select * from tt1 order by name;
write picture description here
The default check rule is utf8_general_ci, according to the character order.
write picture description here
5. View the statement used to create the database:
show create database class_1 \G
write picture description here
6. View the data table:
show tables;

7. View warnings:
show warnings;

8. View the character set:
show charset;
write picture description here

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326829507&siteId=291194637