mysql 跑路_MySQL从删库到跑路(4)SQL查询基础

1.使用约束规定表中的数据规则,其中约束可以使用create table语句在创建表时规定或使用alter table语句对已有表进行更改。

其中:

not null:某列不能存null值

unique: 某列不能存在重复数据

primary key:主键,非空+不重复,确定唯一行。

foreign key:外键, 此列数据指向另一个表中的存在唯一约束的列,即此列内容必须是另一个表中列中存在的内容。

check : 用于约束列中的值的范围(mysql中暂不支持)

default: 规定默认值

已知存在student表如下651e5b465b5971f3c849f5874758f92f.png

设置sid列为主键

alter table student add primary key (sid);

设置 sname列唯一

alter table student add unique(sname);

设置sage默认值为10

alter table student alter sage set default 10;d820de9cd822224b0837d22c9e8a50ad.png

创建一个成绩表score,如下

create table score(

scid int not null,

sid int,

scourse varchar(10) not null,

sscore int,

primary key(scid),

foreign key(sid) references student(sid)

);

其中有scid主键,sid外键,指向student表中sid,科目scourse和成绩sscore列f4d5f4d65a66f17f051cfe62b2f056be.png

向其中依次插入如下数据进行测试

1,3,语文,50

2,3,数学,80

3,4,语文,65

4,4,数学,70

表中存在上面4条数据后再插入如下数据将产生错误

1,3,语文,50

5,8,语文,50

6,3,null,50f1585214aaa30aee5ca3fbe96fccbe29.png

2.使用alter table语句对已存在的表进行添加,修改和删除列的操作。

向student表中添加other列

alter table student add other int;624f5b88c6dadaa9aff954e32a05fe14.png

改变other列的列名和数据类型

alter table student change other newother varchar(10);

删除newother列

alter table student drop column newother;1750a47a25a9e87df1845d96e0450411.png

3.使用union操作符合并两个或多个select语句的结果集,此时select语句中必须有相同数量的列,列中也必须有相似的数据类型,同时列的顺序要相同。

查询score中的scid和student中的sid,此时重复的结果将不会出现

select scid from score union select sid from student;

使用union all显示所有结果集,包含重复

select scid from score union all select sid from student;df3fe9ba70daf54add549f445cc3a0b0.png

4.使用distinct返回唯一不同值

select distinct scourse from score;8022b245d5ced51e8a1708fb9c0f4936.png

5.使用as为表或列取别名(可省略)

查询语文考了50分的学生的名字和id

select b.sid,a.sname from student as a,score b where b.scourse=’语文’ and b.sscore=50 and a.sid=b.sid;bfc620033001d815f539fee3a7cafb17.png

6.使用join把两个表或多个表中的数据连接起来

其中inner join等于join:返回两表中匹配的行,即两表的交集。

left join:返回左表所有记录和右表中满足on条件的行,没有匹配数据即为null

right join:返回右表所有记录和左表中满足on条件的行,没有匹配数据即为null

full join:返回两表所有行,没有匹配数据即为null(mysql中暂不支持)

如下所示19f45f1dd5147a10f10f3a5a46fa514d.png

7.嵌套查询

使用嵌套查询查询语文考了50分的学生的名字和id

select sid,sname from student where sid in (

select sid from score where scourse=’语文’ and sscore=50

);325a2ffb4a570e3f1ff7eac4deb69ea9.png

查询平均成绩大于50的学生的名字

select sname from student where sid in(

select sid from score group by sid having avg(sscore)>50

);

查询平均成绩大于70的学生的名字

select sname from student where sid in(

select sid from score group by sid having avg(sscore)>70

);0ba654e680c4b265a3e6bd1cb7e8a0e1.png

查询平均成绩大于70的学生的名字和平均成绩

select sname,b.score from student a,( select sid,avg(sscore) score from score group by sid having avg(sscore)>70) b where a.sid=b.sid;

查询所有学生的平均成绩

select sname,b.score from student a,( select sid,avg(sscore) score from score group by sid) b where a.sid=b.sid;b0bca07610da5f844e7196ad360c3b48.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值