auto_increment#自动增长primarykey#主键CONSTRAINT 外键名 FOREIGNKEY(列名)REFERENCES 要引入的表名(列名)REGEXP#正则表达式distinct#去重join#关联查询 from 表名 别名 join 表名 别名 on 关联条件leftouterjoin#左外连接 以左边为准,右边不满足以null代替rightouterjoin#右外连接 以右边为准,左边不满足以null代替innerjoin#内连接 两边都满足IN#等于列表中的**任意一个**ANY#需要和单行比较操作符一起使用,和子查询返回的**某一个**值比较ALL#需要和单行比较操作符一起使用,和子查询返回的**所有**值比较SOME#实际上是ANY的别名,作用相同,一般常使用ANY## 事务相关begin#事务开始starttransaction#事务开始commit#提交事务rollback#回滚
约束类型
主键约束:非空且唯一
非空约束 NOT NULL
唯一性约束 unique
外键约束
自定义约束 CHECK(条件)
添加约束
在列定义之前
在列定义以后约束
数据类型
数字型
tinyint byte
smaliint 相当于short
mediumint 三个字节
int
bigin 相当于long
float
double
字符型
char 定长字符
varchar(length) 可变长字符
text 文本类型
json
日期型
data
time
datatime
timetamp
枚举
enum
二进制类型
clob
blob
练习
-- 1) 查询数学分数为65,75,85的同学select*from exam
where math in(65,75,85);-- 2) 查询英语成绩,排除完全相同重复数据selectdistinct english
fr om exam;--3) 对学生成绩按照英语进行降序排序,英语相同学员按照数学降序select*from exam
orderby math desc,english desc;-- 4) 统计总分大于220的人数有多少selectcount(*)from exam
where english+chinese+math>220;-- 5) 统计英语成绩大于90的学生有多少个selectcount(*)from exam
where english>90;-- 6) 统计一个班级语文、英语、数学的成绩总和selectsum(chinese) 语文,sum(math) 数学,sum(english) 英语
from exam;-- 7) 求班级最高分和最低分selectmax(chinese+math+english) 最高分,min(chinese+math+english) 最低分
from exam;-- (1)求购买了供应商"宝洁"产品的所有顾客;selectdistinct customer.*from purcase leftjoin product on purcase.productId = product.productId
leftjoin customer on purcase.customerId = customer.customerId
where product.provider ='宝洁';--(2)求购买的商品包含了顾客"Dennis"所购买的所有商品的顾客(姓名)-- ---------------------------------------------------------------------------------------------------select name
from(select customer.name name,count(purcase.productId) num
from purcase leftjoin customer on purcase.customerId = customer.customerId
rightjoin(selectdistinct productId,name
from purcase leftjoin customer on purcase.customerId = customer.customerId
where name ='dennis') cus on purcase.productId = cus.productId
groupby customer.name) a;#(3)求牙膏卖出数量最多的供应商。select id,max(num)from(select purcase.productId id,sum(quantity) num
from purcase leftjoin product on product.productId = purcase.productId
where category='牙膏'groupby purcase.productId
) a;-- DML操作-- (1)将所有的牙膏商品单价增加10%。 update product set unitPrice=unitPrice*1.1where category='牙膏';-- (2)删除从未被购买的商品记录。deletefrom product where productId notin(selectdistinct productId
from purcase
);-- 1) 对订单表中商品归类后,显示每一类商品的总价select product,sum(price) 总价
from orders
groupby product;-- 2) 查询购买了几类商品,并且每类总价大于100的商品select*from(select product,sum(price) 总价
from orders
groupby product
) o
where o.总价>100;---------------------------------------------------------------------------------------------------------------------- 1) 查询平均成绩大于70分的同学的学号和平均成绩select*from(select s.id id ,avg(sc.score) 平均成绩
from student s leftjoin studentcource sc on s.id= sc.student_id
groupby s.id
) a
where a.平均成绩>70;-- 2) 查询所有同学的学号、姓名、选课数、总成绩select s.id 学生ID,s.name 学生姓名,选课数,总成绩
from student s leftjoin(select student_id id,count(*) 选课数,sum(score) 总成绩
from studentcource
groupby student_id
) a on a.id = s.id
orderby s.id;-- 3) 查询没学过关羽老师课的同学的学号、姓名select id,name
from student
where id notin(select student_id id
from studentcource sc rightjoin(select cource.id 课程ID
from cource
where cource.teacher_id =(select id 老师ID
from teacher
where name ='关羽')) a on a.课程ID = sc.cource_id
)orderby id;-- 4) 查询学过赵云老师所教的所有课的同学的学号、姓名select id,name
from student
where id in(select student_id id
from studentcource sc rightjoin(select cource.id 课程ID
from cource
where cource.teacher_id =(select id 老师ID
from teacher
where name ='赵云')) a on a.课程ID = sc.cource_id
)orderby id;-- 5) 查询没有学三门课以上的同学的学号、姓名select ID,学生姓名
from(select s.id ID, s.name 学生姓名,count(*) 选课数
from studentcource sc leftjoin student s on sc.student_id=s.id
groupby s.id
) a
where a.选课数<=3;-- 6) 查询各科成绩最高和最低的分select cource_id,max(sc.score) 最高分,min(sc.score) 最低分
from studentcource sc
groupby sc.cource_id;-- 7) 查询上海和北京学生数量select city 城市,count(*) 数量
from student
where city='上海'or city='北京'groupby city;-- 8) 统计每门课程的学生选修人数(超过两人的进行统计)select c.id ,name ,数量
from cource c leftjoin(select cource_id id,count(*) 数量
from studentcource sc
groupby cource_id
) a on c.id = a.id
where a.数量>2;-- 9) 查询 生物 成绩排名前3的同学select*from student
where id in(select*from(select sc.student_id
from studentcource sc rightjoin(select id
from cource
where name='生物') a on sc.cource_id = a.id
orderby score
limit3offset0) b
);