ICode9

精准搜索请尝试: 精确搜索
首页 > 数据库> 文章详细

MySQL数据库_(学生_教师_课程_成绩)表_练习

2021-06-02 18:33:50  阅读:178  来源: 互联网

标签:varchar students sno 数据库 课程 MySQL table where select


 SQL的执行顺序

(8)    SELECT (9) DISTINCT column,...            选择字段、去重
(6)    AGG_FUNC(column or expression),...        聚合
(1)    FROM [left_table]                         选择表
(3)   <join_type> JOIN <right_table>            链接
(2)    ON <join_condition>                       链接条件
(4)    WHERE <where_condition>                   条件过滤
(5)    GROUP BY <group_by_list>                  分组
(7)    HAVING <having_condition>                     分组过滤
(9) ORDER BY <order_by_list>                  排序
(10)   LIMIT count OFFSET count;                     分页

 

/*==============================================================*/
/* Table: teachers                                              */
/*==============================================================*/
drop table if exists teachers;
create table teachers(
   tno                  varchar(3) not null,
   tname                varchar(4),
   tsex                 varchar(2),
   tbirthday            datetime,
   prof                 varchar(6),
   depart               varchar(10),
   primary key (tno)
);
/*==============================================================*/
/* Table: students                                              */
/*==============================================================*/
drop table if exists students;
create table students
(
   sno                  varchar(3) not null,
   sname                varchar(4) not null,
   ssex                 varchar(2) not null,
   sbirthday            datetime,
   class                varchar(5),
   primary key (sno)
);
/*==============================================================*/
/* Table: courses                                               */
/*==============================================================*/
drop table if exists courses;
create table courses
(
   cno                  varchar(5) not null,
   cname                varchar(10) not null,
   tno                  varchar(3) not null,
   primary key (cno)
);
alter table courses add constraint FK_Reference_3 foreign key(tno) 
  references teachers(tno) on delete restrict on update restrict;
/*==============================================================*/
/* Table: scores                                                */
/*==============================================================*/
drop table if exists scores;
create table scores
(
   sno                  varchar(3) not null,
   cno                  varchar(5) not null,
   degree               numeric(10,1) not null,
   primary key (sno, cno)
);

alter table scores add constraint FK_Reference_1 foreign key(sno) 
  references students(sno) on delete restrict on update restrict;

alter table scores add constraint FK_Reference_2 foreign key(cno) 
  references courses(cno) on delete restrict on update restrict;
# 考试题型

# 查看一张表中的所有记录(studnets)
select * from students;

# 查询一张表中的指定列(students(学号,名字))
select sno,sname from students;

# 查询指定的记录(students(学号=101))
select * from students where sno=101;

# 模糊查询--查询1976年出生的学生
select * from students where sbirthday like '1976-%'

# 模糊查询--查询70后的学生
select * from students where sbirthday like '197%'

# 查询某个范围之间的所有记录
select * from students as s where s.sno between 101 and 107;

# 查询满足多个条件的记录  and or in
select *  from students as s where s.ssex = '男' and s.class ='95033';
select *  from students as s where s.sno = 101 or s.sno=103;
select *  from students as s where s.class in (95031,95033);  

# 去重 distinct 
select distinct s.class from students as s

# 按学号升降 查询记录
select * from students as s order by s.sno asc;
select * from students as s order by s.sno desc;

# 查询每个班的人数
select class,count(class) from students group by class;
select stu.class,count(stu.class) from students as stu group by class

# 查询学生中最大和最小出生日期与姓名
select stu.sname,stu.sbirthday from students as stu
	where 
		stu.sbirthday = (
			select max(sbirthday) from students 
		)
		OR
		stu.sbirthday = (
			select min(sbirthday) from students 
		)
# 查询每个课程的评价成绩
select cno,avg(degree) from scores
	group by cno
	

# 查询男教师及其所上的课程
select * from teachers as a inner join courses as b 
	on a.tno = b.tno and a.tsex='男'


# 查询每个老师教的课程
select * from teachers as t
	left join courses as c
		on t.tno = c.tno

# 查询女老师的信息
select *
from teachers as t
left join courses as c 
	on t.tno = c.tno
	where t.tsex = '女'

# 查询得分前3名的学员信息
select * 
from students as s 
	where s.sno in (
		select a.sno from
		(select sc.sno
		from scores as sc
		order by sc.degree
		limit 3 )as a
	)

#  查询课程是“计算机导论”的,得分前3名的学员信息
select *
from scores 
where cno = (
	select cno from courses where cname='计算机导论'
)
order by degree desc limit 3

select * from scores  where cno='3-105' order by degree limit 3 




 

标签:varchar,students,sno,数据库,课程,MySQL,table,where,select
来源: https://blog.csdn.net/lyh1023812/article/details/117444350

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有