MYSQL常用语法大全(一)

一、常用数据库操作命令

1.show databases; 查看所有的数据库
2.create database test; 创建一个叫test的数据库
3.show create database test;查看test库的详细信息
4.drop database test; 删除一个叫test的数据库
5.use test; 选中库 ,在建表之前必须要选择数据库
6.show tables; 在选中的数据库之中查看所有的表
7.create table 表名 (字段1 类型, 字段2 类型);
8.show create table 表名; 查看创建表的详细信息
9.desc 表名;查看所在的表的字段
10.drop table 表名; 删除表

二、修改表的命令

1.修改字段类型:alter table 表名 modify 字段 字段类型;
2.添加新的字段:alter table 表名 add 字段 字段类型
3.添加字段并指定位置:alter table 表名 add 字段 字段类型 after 字段;
4.删除表字段:alter table 表名 drop 字段名;
5.修改指定的字段:alter table 表名 change 原字段名字 新字段名字 字段类型

三、对数据的操作

1.增加数据(insert)3种方式
1.1 insert into 表名 values(值1,值2,…)(很少用)
1.2 insert into 表名(字段1,字段2…) values(值1,值2,…);(较常用)
1.3 insert into 表名(字段1,字段2…) values(值1,值2,…),(值1,值2,…),(值1,值2,…);
2.删除数据(delete)
delete from 表名 where 条件 注意:where 条件必须加,否则数据会被全部删除
3.更新数据(update)
update 表名 set 字段1 = 值1, 字段2 = 值2 where 条件
4.查询数据(select)
4.1 查询表中所有的数据 select * from 表名
4.2 根据条件查询数据 select 字段 from 表名 where 条件 (最常用的)
where 条件后面跟的条件
关系:>,<,>=,<=,!=
逻辑:or或, and并且
区间:age between 4 and 6 相当与 age>4 and age<=6;闭区间,包含边界
4.3 模糊查询(like)
select * from 表名 where 字段 like ‘%条件%’
5.排序(order by)
select 字段 from 表名 order by 排序字段(desc | asc)
排序关键词 desc 降序 ,asc 升序(默认)
5.1 多字段排序
select 字段 from 表 order by 字段1 desc |asc,…字段n desc| asc;
6.常用的聚合函数(sum,avg,count,max,min)
select count(*) from 表名
7.分组(group by)
select * from 表名 group by 字段
7.1聚合函数多半和分组,组合使用较多
例:查询学生表student每个班级2022年入学年龄最小的学生
select class,min(age) as mage from student where data > ‘2020-01-01’ group by class
8.limit用法
select * from 表名 limit M,N (是从第M+1条数据开始查询,查询出N条数据)
select * from 表明 limit 0,2 (从第一条开始,该页数据有2条)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值