mysql命令

193 阅读4分钟
  1. net start mysql80 启动数据库服务器

  2. net stop mysql 停止数据库服务器

  3. mysql -uroot -p 登录数据库服务器

  4. create database 数据库名; 创建数据库

  5. use 数据库名; 进入数据库

  6. show databases; 显示所有数据库

  7. show tables; 显示所有表

  8. create table 表名(属性字段 数据类型); 创建表

  9. describe 表名;(desc 表名) 查看表结构

  10. insert into 表名 values('属性值'); 向表中添加数据

  11. delete from 表名 where 属性字段=属性值; 删除表中的数据

  12. update 表名 set 属性字段=属性值 where 属性字段=属性值; 修改表中的数据

  13. select 数据字段 from 表名; 查找表中的数据

  14. create table 表名(属性字段 数据类型 primary key); 设置主键约束

  15. create table 表名(字段1 类型, 字段2 类型, primary key(字段1,字段2)); 设置联合主键约束

  16. create table 表名(属性字段 数据类型 primary key auto_increment); 自增约束

  17. alter table 表名 add primary key(属性字段) 设置主键约束

  18. alter table 表名 drop primary key(属性字段) 删除主键约束

  19. alter table 表名 add unique(属性字段) 设置唯一约束

  20. alter table 表名 drop index 属性字段 删除唯一约束

  21. create table 表名(属性字段 数据类型 not null); 设置非空约束

  22. create table 表名(属性字段 数据类型 default xxx); 设置默认约束

  23. create table 表名(属性字段 数据类型 foreign key(主表的数据字段) references 副表表名(副表主键的数据字段)); 设置外键约束

  24. alter table 表格名 add column 数值 类型; 向某一个表格中添加一列

  25. select max(数据字段) from 表名; 最大值

  26. select min(数据字段) from 表名; 最小值

  27. select distinct 数据字段 from 表名; distinct 排重

  28. select 数据字段 from 表名 where 数据字段 in(属性值,属性值); in 或

  29. select 数据字段 from 表名 where order by desc; desc 降序

  30. select 数据字段 from 表名 where order by asc; asc 升序 默认为升序

  31. select 数据字段 from 表名 limit 索引index,条数n; limit 获取表中index开始的n条数据 index起始值为0

  32. select avg(数据字段) from 表名; avg 平均值

  33. select 数据字段 from 表名 group by; group by 分组

  34. select * from 表名 where 数据字段 like '3%'; like 模糊查询 % 任意

  35. select yarn(数据字段) from 表名; yarn 取年份

  36. select yarn(now()) from 表名; yarn(now()) 取当前年份

  37. select 数据字段 from 表名 where 数据字段 not in(属性值,属性值); not 取反

  38. select 数据字段 from 表名 union select 数据字段 from 表名; union 并集

  39. select 数据字段 from 表名 where 数据字段 > any(select 数据字段 from 表名); any 至少

  40. select 数据字段 from 表名 where 数据字段 > all(select 数据字段 from 表名); all 所有

  41. select count(数据字段) from 表名; count 统计

  42. select 数据字段 from 表1 join 表2 where 表1.数据字段 = 表2.数据字段; join on 内连接 两个表的交集

  43. select 数据字段 from 表1 left join 表2 where 表1.数据字段 = 表2.数据字段; left join on 左连接 合并两个表,表2中没有的数据默认为null

  44. select 数据字段 from 表1 right join 表2 where 表1.数据字段 = 表2.数据字段; right join on 右连接 合并两个表,表1中没有的数据默认为null

  45. select 数据字段 from 表1 full join 表2 where 表1.数据字段 = 表2.数据字段; full join on 全连接 合并两个表 不推荐使用

  46. set autocommit = 0; 设置事务的状态

  47. commit; 事务提交

  48. rollback; 事务回滚

  49. begin;(start transaction) 开启局部手动提交事务

  50. select @@global.transaction_isolation; 查看系统级别的事务隔离性

  51. select @@transaction_isolation; 查看回话级别的事务隔离性

  52. set global transaction isolation level read uncommitted; 设置隔离级别

  53. select @@autocommit; 查询事务的状态