简而言之:一个方便的备忘录
#sql #database #mysql #cheatsheet

此MySQL备忘单提供了用于管理和操纵数据库的基本命令的全面列表。从创建和修改表到查询和更新数据,本参考指南涵盖了您需要知道的所有关键命令,以有效地与MySQL合作。无论您是初学者还是经验丰富的用户,此作弊表都是快速找到所需命令的宝贵资源。

  1. 连接到mysql:mysql -u [username] -p
  2. 显示所有数据库:SHOW DATABASES;
  3. 创建一个新数据库:CREATE DATABASE [database name];
  4. 选择一个数据库:USE [database name];
  5. 在数据库中显示所有表:SHOW TABLES;
  6. 创建一个新表:CREATE TABLE [table name] ([column name] [data type], [column name] [data type], ...);
  7. 显示表的结构:DESCRIBE [table name];
  8. 将数据插入表:INSERT INTO [table name] ([column name], [column name], ...) VALUES ([value], [value], ...);7
  9. 从表中选择数据:SELECT [column name], [column name], ... FROM [table name];
  10. 表中更新数据:UPDATE [table name] SET [column name] = [new value] WHERE [condition];
  11. 从表中删除数据:DELETE FROM [table name] WHERE [condition];
  12. 选择条件的数据:SELECT [column name], [column name], ... FROM [table name] WHERE [condition];11
  13. 与Join:SELECT [column name], [column name], ... FROM [table name1] JOIN [table name2] ON [table name1.[column name] = [table name2].[column name];选择数据
  14. 列订单数据列:SELECT [column name], [column name], ... FROM [table name] ORDER BY [column name];
  15. 限制返回的行数:SELECT [column name], [column name], ... FROM [table name] LIMIT [number];
  16. 计数行数:SELECT COUNT([column name]) FROM [table name];
  17. 列的总和:SELECT SUM([column name]) FROM [table name];
  18. 列的平均值:SELECT AVG([column name]) FROM [table name];
  19. 组列列:SELECT [column name], [aggregate function]([column name]) FROM [table name] GROUP BY [column name];
  20. 创建一个新用户:CREATE USER 'username'@'host' IDENTIFIED BY 'password';
  21. 授予用户的授予权限:GRANT [permission] ON [database name].[table name] TO 'username'@'host';
  22. 撤销权限:REVOKE [permission] ON [database name].[table name] FROM 'username'@'host';
  23. 显示所有用户:SELECT User, Host FROM mysql.user;
  24. 更改用户的密码:SET PASSWORD FOR 'username'@'host' = PASSWORD('new_password');
  25. 重命名表:ALTER TABLE [table name] RENAME TO [new table name];
  26. 将列添加到表:ALTER TABLE [table name] ADD [column name] [data type];
  27. 在表中修改列:ALTER TABLE [table name] MODIFY [column name] [data type];
  28. 从表中删除一列:ALTER TABLE [table name] DROP COLUMN [column name];
  29. 创建索引:CREATE INDEX [index name] ON [table name] ([column name]);
  30. 删除索引:DROP INDEX [index name] ON [table name];
  31. 创建一个主键:ALTER TABLE [table name] ADD PRIMARY KEY ([column name]);
  32. 创建外键:ALTER TABLE [table name] ADD FOREIGN KEY ([column name]) REFERENCES [referenced table name] ([referenced column name]);
  33. 创建独特的约束:ALTER TABLE [table name] ADD UNIQUE ([column name]);
  34. 创建检查约束:ALTER TABLE [table name] ADD CHECK (condition);
  35. 创建触发器:CREATE TRIGGER [trigger name] [trigger action] [event] ON [table name] FOR EACH ROW [trigger code];
  36. 放下扳机:DROP TRIGGER [trigger name];
  37. 创建一个存储过程:CREATE PROCEDURE [procedure name] ([parameter name] [data type], ...) BEGIN [procedure code]; END;
  38. 调用存储过程:CALL [procedure name]([parameter value], ...);
  39. 删除存储过程:DROP PROCEDURE [procedure name];
  40. 创建视图:CREATE VIEW [view name] AS SELECT [column name], ... FROM [table name];
  41. 删除视图:DROP VIEW [view name];
  42. 创建备份:mysqldump -u [username] -p [database name] > [file name].sql
  43. 还原备份:mysql -u [username] -p [database name] < [file name].sql
  44. 显示当前用户:SELECT CURRENT_USER();
  45. 显示服务器版本:SELECT VERSION();
  46. 显示系统变量:SHOW VARIABLES;
  47. 显示状态:SHOW STATUS;
  48. show processList:SHOW FULL PROCESSLIST;
  49. 杀死一个过程:KILL [process id];
  50. 退出mysql:EXIT;

注意:用实际值替换方形括号的占位符。