MySQL Command Reference
Basic Commands
| Operation | Command |
|---|---|
| Connect to the MySQL database | mysql -u 用户名 -p |
| View all databases | SHOW DATABASES; |
| Select a database | USE 数据库名; |
| View all tables | SHOW TABLES; |
| View table structure | DESCRIBE 表名; or SHOW COLUMNS FROM 表名; |
| Create a new database | CREATE DATABASE 数据库名; |
| Delete a database | DROP DATABASE 数据库名; |
| Create a new table | CREATE TABLE 表名 (列名1 数据类型 [约束], 列名2 数据类型 [约束], ...); |
| Delete a table | DROP TABLE 表名; |
| Insert data | INSERT INTO 表名 (列1, 列2, ...) VALUES (值1, 值2, ...); |
| Query data | SELECT 列1, 列2, ... FROM 表名 WHERE 条件; |
| Update data | UPDATE 表名 SET 列1 = 值1, 列2 = 值2, ... WHERE 条件; |
| Delete data | DELETE FROM 表名 WHERE 条件; |
| Create users | CREATE USER '用户名'@'主机' IDENTIFIED BY '密码'; |
| Grant a user | GRANT 权限 ON 数据库名.* TO '用户名'@'主机'; |
| Flush Privileges | FLUSH PRIVILEGES; |
| View the current user | SELECT USER(); |
| Exit MySQL | EXIT; |
Database-Related Commands
The following are commands related to MySQL database operations, including creating, deleting, and modifying databases:
| Operation | Command |
|---|---|
| Create the database | CREATE DATABASE 数据库名; |
| Drop the database | DROP DATABASE 数据库名; |
| Modify database encoding and collation | ALTER DATABASE 数据库名 DEFAULT CHARACTER SET 编码格式 DEFAULT COLLATE 排序规则; |
| View all databases | SHOW DATABASES; |
| View database details | SHOW CREATE DATABASE 数据库名; |
| Select a database | USE 数据库名; |
| View database status information | SHOW STATUS; |
| View database error information | SHOW ERRORS; |
| View database warning information | SHOW WARNINGS; |
| View database tables | SHOW TABLES; |
| View table structure | DESC 表名; |
`DESCRIBE 表名;`
`SHOW COLUMNS FROM 表名;`
`EXPLAIN 表名;` |
| Create a table | CREATE TABLE 表名 (列名1 数据类型 [约束], 列名2 数据类型 [约束], ...); | | Delete table | DROP TABLE 表名; | | Modify table structure | ALTER TABLE 表名 ADD 列名 数据类型 [约束];
ALTER TABLE 表名 DROP 列名;
ALTER TABLE 表名 MODIFY 列名 数据类型 [约束]; | | View the table's CREATE SQL | SHOW CREATE TABLE 表名; |
Data Table-Related Commands
The following are common commands related to MySQL data tables, including creating, modifying, deleting tables and viewing table structure and data:
| Operation | Command |
|---|---|
| Create a table | CREATE TABLE 表名 (列名1 数据类型 [约束], 列名2 数据类型 [约束], ...); |
| Delete table | DROP TABLE 表名; |
| Modify table structure | Add a column: ALTER TABLE 表名 ADD 列名 数据类型 [约束]; |
删除列: `ALTER TABLE 表名 DROP 列名;`
修改列: `ALTER TABLE 表名 MODIFY 列名 数据类型 [约束];`
重命名列: `ALTER TABLE 表名 CHANGE 旧列名 新列名 数据类型 [约束];` |
| View table structure | DESC 表名;
DESCRIBE 表名;
SHOW COLUMNS FROM 表名;
EXPLAIN 表名; | | View the table's CREATE SQL | SHOW CREATE TABLE 表名; | | View all data in a table | SELECT * FROM 表名; | | Insert data | INSERT INTO 表名 (列1, 列2, ...) VALUES (值1, 值2, ...); | | Update data | UPDATE 表名 SET 列1 = 值1, 列2 = 值2, ... WHERE 条件; | | Delete data | DELETE FROM 表名 WHERE 条件; | | View a table's indexes | SHOW INDEX FROM 表名; | | Creating indexes | CREATE INDEX 索引名 ON 表名 (列名); | | Delete indexes | DROP INDEX 索引名 ON 表名; | | View a table's constraints | SHOW CREATE TABLE 表名; (Constraint information is included in the table's CREATE SQL) | | View a table's statistics | SHOW TABLE STATUS LIKE '表名'; |
MySQL Transaction-Related Commands
The following are common commands related to MySQL transactions:
| Operation | Command |
|---|---|
| Start a transaction | START TRANSACTION; or BEGIN; |
| Commit a transaction | COMMIT; |
| Roll back a transaction | ROLLBACK; |
| View the current transaction status | SHOW ENGINE INNODB STATUS; (You can view the transaction status of the InnoDB storage engine) |
| Lock tables for transaction operations | LOCK TABLES 表名 WRITE; or LOCK TABLES 表名 READ; |
| Release locked tables | UNLOCK TABLES; |
| Set the transaction isolation level | SET TRANSACTION ISOLATION LEVEL READ COMMITTED; |
`SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;`
`SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;`
`SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;` |