MySQL Command Encyclopedia

Basic Commands

OperationCommand
Connect to a MySQL databasemysql -u 用户名 -p
View all databasesSHOW DATABASES;
Select a databaseUSE 数据库名;
View all tablesSHOW TABLES;
View table structureDESCRIBE 表名;orSHOW COLUMNS FROM 表名;
Create a new databaseCREATE DATABASE 数据库名;
Drop a databaseDROP DATABASE 数据库名;
Create a new tableCREATE TABLE 表名 (列名1 数据类型 [约束], 列名2 数据类型 [约束], ...);
Drop a tableDROP TABLE 表名;
Insert dataINSERT INTO 表名 (列1, 列2, ...) VALUES (值1, 值2, ...);
Query dataSELECT 列1, 列2, ... FROM 表名 WHERE 条件;
Update dataUPDATE 表名 SET 列1 = 值1, 列2 = 值2, ... WHERE 条件;
Delete dataDELETE FROM 表名 WHERE 条件;
Create userCREATE USER '用户名'@'主机' IDENTIFIED BY '密码';
Grant privileges to userGRANT 权限 ON 数据库名.* TO '用户名'@'主机';
Flush privilegesFLUSH PRIVILEGES;
View current userSELECT USER();
Exit MySQLEXIT;

Database Related Commands

The following are commands related to MySQL database operations, including creating, dropping, and modifying databases:

OperationCommand
Create databaseCREATE DATABASE 数据库名;
Drop databaseDROP DATABASE 数据库名;
Modify database character set and collationALTER DATABASE 数据库名 DEFAULT CHARACTER SET 编码格式 DEFAULT COLLATE 排序规则;
View all databasesSHOW DATABASES;
View database detailsSHOW CREATE DATABASE 数据库名;
Select databaseUSE 数据库名;
View database status informationSHOW STATUS;
View database error informationSHOW ERRORS;
View database warning informationSHOW WARNINGS;
View tables in the databaseSHOW TABLES;
View table structureDESC 表名;
DESCRIBE 表名;
SHOW COLUMNS FROM 表名;
EXPLAIN 表名;
Create tableCREATE TABLE 表名 (列名1 数据类型 [约束], 列名2 数据类型 [约束], ...);
Drop tableDROP TABLE 表名;
Modify table structureALTER TABLE 表名 ADD 列名 数据类型 [约束];
ALTER TABLE 表名 DROP 列名;
ALTER TABLE 表名 MODIFY 列名 数据类型 [约束];
View the CREATE SQL of the tableSHOW CREATE TABLE 表名;

Data Table Related Commands

The following are common commands related to MySQL data tables, including creating, modifying, and dropping tables, as well as viewing table structure and data:

OperationCommand
Create tableCREATE TABLE 表名 (列名1 数据类型 [约束], 列名2 数据类型 [约束], ...);
Drop tableDROP TABLE 表名;
Modify table structureAdd column:ALTER TABLE 表名 ADD 列名 数据类型 [约束];
Drop column:ALTER TABLE 表名 DROP 列名;
Modify column:ALTER TABLE 表名 MODIFY 列名 数据类型 [约束];
Rename column:ALTER TABLE 表名 CHANGE 旧列名 新列名 数据类型 [约束];
View table structureDESC 表名;
DESCRIBE 表名;
SHOW COLUMNS FROM 表名;
EXPLAIN 表名;
View the CREATE SQL of the tableSHOW CREATE TABLE 表名;
View all data in the tableSELECT * FROM 表名;
Insert dataINSERT INTO 表名 (列1, 列2, ...) VALUES (值1, 值2, ...);
Update dataUPDATE 表名 SET 列1 = 值1, 列2 = 值2, ... WHERE 条件;
Delete dataDELETE FROM 表名 WHERE 条件;
View table indexesSHOW INDEX FROM 表名;
Create indexCREATE INDEX 索引名 ON 表名 (列名);
Drop indexDROP INDEX 索引名 ON 表名;
View table constraintsSHOW CREATE TABLE 表名;(Constraint information will be included in the CREATE TABLE SQL)
View table statisticsSHOW TABLE STATUS LIKE '表名';

MySQL Transaction Related Commands

The following are common commands related to MySQL transactions:

OperationCommand
Start transactionSTART TRANSACTION;orBEGIN;
Commit transactionCOMMIT;
Rollback transactionROLLBACK;
View current transaction statusSHOW ENGINE INNODB STATUS;(You can view the transaction status of the InnoDB storage engine)
Lock tables for transaction operationsLOCK TABLES 表名 WRITE;orLOCK TABLES 表名 READ;
Unlock tablesUNLOCK TABLES;
Set transaction isolation levelSET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
Other Extensions