MySQL 常用命令整理

7 min read
  1. 登录MySQL:mysql -u username -p

  2. 显示所有数据库:show databases;

  3. 创建数据库:create database dbname;

  4. 进入数据库:use dbname;

  5. 显示数据库中所有表:show tables;

  6. 创建表:create table tablename(col1 datatype1, col2 datatype2, ...);

  7. 插入数据:insert into tablename(col1, col2, ...) values(val1, val2, ...);

  8. 查找数据:select * from tablename where condition;

  9. 更新数据:update tablename set col = val where condition;

  10. 删除数据:delete from tablename where condition;

  11. 删除表:drop table tablename;

  12. 删除数据库:drop database dbname;

  13. 修改表名:rename table oldname to newname;

  14. 查看表结构:desc tablename;

  15. 查看服务器版本:select version();

  16. 查看当前时间:select now();

  17. 查看当前用户:select user();

  18. 查看当前用户权限:show grants;

  19. 创建用户:create user username identified by 'password';

  20. 授权用户权限:grant privileges on dbname.tablename to username;

  21. 撤销用户权限:revoke privileges on dbname.tablename from username;

  22. 列出所有用户:select user from mysql.user;

  23. 列出所有权限:show grants for username;

  24. 退出MySQL:exit;