此MySQL备忘单提供了用于管理和操纵数据库的基本命令的全面列表。从创建和修改表到查询和更新数据,本参考指南涵盖了您需要知道的所有关键命令,以有效地与MySQL合作。无论您是初学者还是经验丰富的用户,此作弊表都是快速找到所需命令的宝贵资源。
- 连接到mysql:
mysql -u [username] -p
- 显示所有数据库:
SHOW DATABASES;
- 创建一个新数据库:
CREATE DATABASE [database name];
- 选择一个数据库:
USE [database name];
- 在数据库中显示所有表:
SHOW TABLES;
- 创建一个新表:
CREATE TABLE [table name] ([column name] [data type], [column name] [data type], ...);
- 显示表的结构:
DESCRIBE [table name];
- 将数据插入表:
INSERT INTO [table name] ([column name], [column name], ...) VALUES ([value], [value], ...);
7 - 从表中选择数据:
SELECT [column name], [column name], ... FROM [table name];
- 表中更新数据:
UPDATE [table name] SET [column name] = [new value] WHERE [condition];
- 从表中删除数据:
DELETE FROM [table name] WHERE [condition];
- 选择条件的数据:
SELECT [column name], [column name], ... FROM [table name] WHERE [condition];
11 - 与Join:
SELECT [column name], [column name], ... FROM [table name1] JOIN [table name2] ON [table name1.[column name] = [table name2].[column name];
选择数据 - 列订单数据列:
SELECT [column name], [column name], ... FROM [table name] ORDER BY [column name];
- 限制返回的行数:
SELECT [column name], [column name], ... FROM [table name] LIMIT [number];
- 计数行数:
SELECT COUNT([column name]) FROM [table name];
- 列的总和:
SELECT SUM([column name]) FROM [table name];
- 列的平均值:
SELECT AVG([column name]) FROM [table name];
- 组列列:
SELECT [column name], [aggregate function]([column name]) FROM [table name] GROUP BY [column name];
- 创建一个新用户:
CREATE USER 'username'@'host' IDENTIFIED BY 'password';
- 授予用户的授予权限:
GRANT [permission] ON [database name].[table name] TO 'username'@'host';
- 撤销权限:
REVOKE [permission] ON [database name].[table name] FROM 'username'@'host';
- 显示所有用户:
SELECT User, Host FROM mysql.user;
- 更改用户的密码:
SET PASSWORD FOR 'username'@'host' = PASSWORD('new_password');
- 重命名表:
ALTER TABLE [table name] RENAME TO [new table name];
- 将列添加到表:
ALTER TABLE [table name] ADD [column name] [data type];
- 在表中修改列:
ALTER TABLE [table name] MODIFY [column name] [data type];
- 从表中删除一列:
ALTER TABLE [table name] DROP COLUMN [column name];
- 创建索引:
CREATE INDEX [index name] ON [table name] ([column name]);
- 删除索引:
DROP INDEX [index name] ON [table name];
- 创建一个主键:
ALTER TABLE [table name] ADD PRIMARY KEY ([column name]);
- 创建外键:
ALTER TABLE [table name] ADD FOREIGN KEY ([column name]) REFERENCES [referenced table name] ([referenced column name]);
- 创建独特的约束:
ALTER TABLE [table name] ADD UNIQUE ([column name]);
- 创建检查约束:
ALTER TABLE [table name] ADD CHECK (condition);
- 创建触发器:
CREATE TRIGGER [trigger name] [trigger action] [event] ON [table name] FOR EACH ROW [trigger code];
- 放下扳机:
DROP TRIGGER [trigger name];
- 创建一个存储过程:
CREATE PROCEDURE [procedure name] ([parameter name] [data type], ...) BEGIN [procedure code]; END;
- 调用存储过程:
CALL [procedure name]([parameter value], ...);
- 删除存储过程:
DROP PROCEDURE [procedure name];
- 创建视图:
CREATE VIEW [view name] AS SELECT [column name], ... FROM [table name];
- 删除视图:
DROP VIEW [view name];
- 创建备份:
mysqldump -u [username] -p [database name] > [file name].sql
- 还原备份:
mysql -u [username] -p [database name] < [file name].sql
- 显示当前用户:
SELECT CURRENT_USER();
- 显示服务器版本:
SELECT VERSION();
- 显示系统变量:
SHOW VARIABLES;
- 显示状态:
SHOW STATUS;
- show processList:
SHOW FULL PROCESSLIST;
- 杀死一个过程:
KILL [process id];
- 退出mysql:
EXIT;
注意:用实际值替换方形括号的占位符。