MySql教程

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

本文主要是介绍MySQL数据库_(学生_教师_课程_成绩)表_练习,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

 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 




 

这篇关于MySQL数据库_(学生_教师_课程_成绩)表_练习的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!