记录 MySQL 命令行常用命令行详解:
1. 登录 MySQL。-u 和 -p 后面紧跟用户名和密码,没有空格。
mysql -uroot -p'密码'

2. 查看所有的数据库。
show databases;

3. 创建名为 test 的数据库
create database test;

4. 进入 test 数据库
use test;
![]()
5. 查看数据库里有多少张表
show tables;

6. 创建名为 t1 表,并创建两个字段,id、name,varchar 表示设置数据长度,用字符来字符来定义长度单位,其中 1 汉字 = 2 字符 = 2 字节
create table t1(id varchar(20),name varchar(20));

7. 向表中插入数据
- insert into t1 values("1","Tom");
- insert into t1 values("2","Jerry");

8. 查看 t1 表数据内容
select * from t1;

9. id、name 多个条件查询
select * from t1 where id = 1 and name = 'Tom';

10. 查看 t1 表数据内容
desc t1;

11. 修改 name 字段的长度
alter table t1 modify column name varchar(30);

12. 修改 name 字段的内容
update t1 set name = 'Tommy' where id = 1;

13. 刷新权限
flush privileges;

14. 查看数据库字符集
show variables like '%char%';

15. 查看 MySQL 存储引擎
show engines;

16. 查看 MySQL 默认的存储引擎
show variables like '%storage_engine%';

17. 查看单个表的存储引擎
show create table t1;

show table status from test where name='t1' \G

18. 修改 MySQL t1 表存储引擎
alter table t1 engine=MyISAM;

19. 查询整个MySQL实例里面存储引擎为MyISAM的表
如果要查询某个库或所有实例里面表使用的存储引擎,那么可以使用information_schema.tables来查询。
select table_catalog, table_schema, table_name, engine from information_schema.tables where engine='MyISAM';

20. 查询 test 数据库里面存储引擎为 MyISAM 的表
select table_catalog, table_schema, table_name, engine from information_schema.tables where table_schema='test' and engine='MyISAM';

21. 清空表内容
delete from t2;

22. 删除表
drop table t2;

23. 删除 test_1 数据库
drop database test_1;

24. 退出 MySQL
exit
