mysql基础操作

数据类型与约束
int 整数 有符号和无符号,长度不受限制
varchar 字符串 中文/数字/字母都是一字字符,受长度限制
decimal 小数 decimal(5,2)总长度5位,整数3位,小数2位
主键 id int unsigned primary key auto_increment
非空 not null
唯一 unique
默认 default
SQL中创建表的操作
创建表
create table 表名(字段名 类型 约束,…)
删除表
drop table 表名
drop table if exists 表名
表数据
添加数据
insert into 表名 values(值1,值2…)给表中所有字段插入数据
insert into 表名(字段2,字段1)values(值2,值1)
修改数据
update 表名 set 字段1=值1,字段2=值2 where 条件
isdelete 逻辑删除
删除数据
delete from 表名 where 条件
查询数据
数据查询
查询表中所有信息
select * from 表名 where 条件
查询表中指定字段
select 字段1,字段2,字段3…from 表名 where 条件
给字段取别名
select 字段1 (as) 别名,字段2 别名 from 表名 where 条件
去重
select distinct 字段 from 表名 where 条件
比较运算符
select * from 表名 where age>20
逻辑运算符
and 满足所有条件
or 满足其中任意一个条件
not 不满足条件
模糊查询
like
% 0到任意多个字符
_表示任意一个字符
范围查询
in 在一个非连续的范围内
between and 在一个连续的范围内 闭区间 小的数据在前
空查询
is null
‘’空字符串
多表查询
排序
select * from 表名 where 条件 order by 列1 (asc)|desc,列2 asc|desc
select * from student where 条件 order by convert(字段 using gbk)asc|desc 纯中文转换后排序
聚合函数
count: 总数 select count(*/字段)from 表名 where 条件
max: 最大值 select max(age) from 表名 where 条件
min: 最小值 select min(age) from 表名 where 条件
avg: 平均值 select avg(grade) from 表名 where 条件
sum: 求和 select sum(grade) from 表名 where 条件
select avg(age)as 平均年龄,max(age)最大年龄,min(age)最小年龄 from 表名 where 条件
分组
select * from 表名 group by 字段,字段2 having 条件
分页
select * from 表名 limit 0,5 从第一行数据开始,显示5行
select * from 表名 limit (n-1)*m,m n代表第几页,m代表每页显示多少条数据
等值连接
方式一 select * from 表1,表2,表3 where 表1.列=表2.列 and 表2.列=表3.列 where 条件
方式二 内连接 取交集 select * from 表1 inner join 表2 on 表1.列=表2.列 inner join 表3 on 表2.列=表3.列 where 条件
左连接
左边的表全显示,右边表能匹配的上的数据连接显示,匹配不上(没有的)以null补充select * from 表1 left join 表2 on 表1.列=表2.列 left join 表3 on 表2.列=表3.列 where 条件
右连接
右边的表全显示,左边表能匹配上的数据连接显示,匹配不上(没有的)以null补充 select * from 表1 right join 表2 on 表1.列=表2.列 right join 表3 on 表2.列=表3.列 where 条件
自关联
select * from 表 别名1 inner join 表 别名2 on 别名1.aid=别名2.pid
子查询
标量子查询
子查询结果输出的是一行一列
select * from student where age>(select avg(age) from student)
列子查询
子查询的结果输出的是一列多行
in: select * from student where age in(select age from student where age=18)
any/some: >:大于最小的; <: 小于最大的 select * from student where age>/< any (select age from student where age between 18 and 22 )
all: >:大于最大的; <: 小于最小的 select * from student where age>/<all (select age from student where age between 18 and 22 )
行子查询
子查询的结果输出的是一行多列
select * from student where(name,sex)=(select name,sex from student where sex=‘男’ order by age desc limit 1)
表子查询
子查询的输出结果是一个表
select * from scores inner join (select cno from courses where cname in(‘数据库’,‘系统测试’) as c on scores.cno=c.cno
数据分表
insert into goods_cate(cate_name) select distinct cate from goods;
create table goods_cate (id int unsigned primary key auto_incremen,cate varchar(10)) select distinct cate from goods;
update goods inner join goods_cate on goods_cate.cate=goods.cate set goods.cate=goods_cate.id

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值