今天小编给大家分享一下MySQL如何创建三张关系表的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。1.创建学生表create&
今天小编给大家分享一下MySQL如何创建三张关系表的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。
create table tbl_stu (id int not null primary key auto_increment,name varchar(45) not null)engine=innodb default charset=utf8;
create table tbl_sub (id int not null primary key auto_increment,subject varchar(45) not null)engine=innodb default charset=utf8;
create table tbl_scores(id int not null primary key auto_increment,stu_id int,sub_id intscore decimal(5,2),constraint sco_stu foreign key(stu_id) references tbl_stu(id),constraint sco_sub foreign key(sub_id) references tbl_sub(id));
insert into tbl_stu values (0,"小王");insert into tbl_stu values (0,"小宋");insert into tbl_stu values (0,"小李");insert into tbl_sub values (0,"语文");insert into tbl_sub values (0,"数学");insert into tbl_sub values (0,"英语");insert into tbl_scores values (0,1,1,90);insert into tbl_scores values (0,1,2,70);insert into tbl_scores values (0,1,3,82);insert into tbl_scores values (0,2,1,95);insert into tbl_scores values (0,2,2,70);insert into tbl_scores values (0,2,3,84);insert into tbl_scores values (0,3,1,85);insert into tbl_scores values (0,3,2,86);
select s3.name,s2.subject,s1.score from tbl_scores as s1inner join tbl_sub as s2 on s1.sub_id = s2.idinner join tbl_stu as s3 on s1.sub_id = s3.id;
select s3.name,avg(s1.score) from tbl_scores as s1inner join tbl_stu as s3 on s1.sub_id = s3.idgroup by s3.name;
select s3.name,sum(s1.score) as s from tbl_scores as s1inner join tbl_stu as s3 on s1.stu_id = s3.idgroup by s3.name order by s desc;
以上就是“Mysql如何创建三张关系表”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注编程网数据库频道。
--结束END--
本文标题: MySQL如何创建三张关系表
本文链接: https://lsjlt.com/news/325630.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-10-23
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
2024-10-22
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0