Table of contents
1. Library operations
1.Create database
create database db1; //方法1
create database is not exists db1;//表示如果没有则创建,方法一不写表示默认携带,与方法1完全相同
Character set and validation rules
Character set: character set is a set of symbols representing characters and the underlying encoding of these symbols
Validation rules: collation is a set of rules for comparing characters within a character set.
Check the character sets supported by the database
show charset;
Check the character set verification rules supported by the database
show collation;
The impact of validation rules on the database
Use utf8_general_ci case-insensitive
Use utf8_bin for case sensitivity
At this point we can bring the character set and verification rules when creating
If we do not specify it, the database will be created according to the rules in the configuration file by default.
Specify character set and validation rules
2. View the database
View database
show databases;
Show create statement
3. Modify the database
alter database db_name;
Modifying the database mainly refers to modifying the character set and verification rules of the database
Example: Change the db1 database character set to gbk
4. Delete database
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.
It is worth mentioning that do not delete the database easily. Deleting the database by mistake is a very serious problem.
5. Database backup
mysqldump -P3306 -u root -p 密码 -B 数据库名 > 数据库备份存储的文件路径
Example:
You can see that this file stores the effective instructions for us to create this database and insert data, etc.
Restore database
source /home/tzc/linux/MySQL/test.sql(带绝对路径+文件名)
What should be done if the backup is not the entire database, but one of the tables?
mysqldump -u root -p 数据库名 表名1 表名2 > D:/mytest.sql
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 data
database, and then use source to restore.
6. Check the connection status
show processlist;
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.
2. Table operations
1.Create table
grammar:
create table table_name(field1 datatype,fileld2 datatype)
charset=字符集 collate=校验规则 engine=存储引擎;
illustrate:
-
field represents column name
-
datatype represents the type of column
-
charset character set, if no character set is specified, the character set of the database shall prevail.
-
collate verification rules. If no verification rules are specified, the verification rules of the database shall prevail.
Example:
Create tables using different storage engines
The table we create is InnoDB by default and is read in the configuration file.
2. View table
desc table_name;
Example:
3. Modify table
In the actual development of the project, the structure of a certain table is often modified, such as field name, field size, field type, table character set type, table storage engine, etc. We still have requirements, add fields, delete fields, etc. At this time we need to modify the table.
ALTER TABLE tablename ADD (column datatype [DEFAULT expr][,column datatype]…);
ALTER TABLE tablename MODIfy(column datatype [DEFAULT expr][,columndatatype]…);
ALTER TABLE tablename DROP (column);
Example:
Insert data into table
insert into 表名 (指定属性字段,如果不指定表示添加所有属性) values(数据);
Add a field to the users table to save the image path
alter table user1 add 属性 数据类型 comment '备注' after birthday(表示在birthday属性后方添加)
Modify attributes: Modify the name length to 60. Modifying attributes is actually overwriting.
Modify attribute name: New fields need to be fully defined
Delete attributes
alter table user1 drop id; //删除id属性
Modify table name
alter table 表名 rename to(可写可不写) 新表名;
4. Delete table
drop table 表名;
Example: