MySQL "Multi-table join operation 2"

drop database if exists 学生管理数据库;
create database 学生管理数据库;
use 学生管理数据库;
create table 学生表
(学号 int primary key,
姓名 char(10),
性别 char(2),
年龄 int,
所在院系 char(20),
班级 char(10),
入学日期 date)charset utf8;

create table 课程表
(课程号 char(4) primary key,
课程名 char(20),
先修课 char(4),
学分 int)charset utf8;

create table 成绩表
(学号 int ,
课程号 char(4),
成绩 int,
学分 int,
primary key(学号,课程号))charset utf8;

create table 授课表
(教师名 char(10),
课程号 char(4),
学时数 int,
班级名 char(10),
primary key(课程号,班级名))charset utf8;

insert into 学生表
values(20009001,'葛文卿','女',22,'国际贸易','国贸2班','2000-8-29'),
(20014019,'郑秀丽','女',21,'会计学','会计1班','2001-9-2'),
(20023001,'刘成铠','男',18,'计算机','软件2班','2002-8-27'),
(20026002,'李涛','女',19,'电子学','电子1班','2002-8-27'),
(20023002,'沈香娜','女',18,'计算机','软件2班','2002-8-27'),
(20026003,'李涛','男',19,'计算机','软件1班','2002-8-27'),
(20023003,'肖一竹','女',19,'计算机','软件2班','2002-8-27');

insert into 课程表
values('C801','高等数学',NULL,4),
('C802','C++语言','C807',3),
('C803','数据结构','C802',4),
('C804','数据库原理','C803',4),
('C805','操作系统','C807',4),
('C806','编译原理','C803',4),
('C807','离散数学',NULL,4);

insert into 成绩表
values(20023001,'C801',98,4),
(20023001,'C802',92,3),
(20023001,'C803',NULL,4),
(20023002,'C802',NULL,3),
(20023002,'C804',70,4),
(20026001,'C801',85,4),
(20026002,'C803',95,4);

insert into 授课表
values('苏亚步','C801',72,'软件2班'),
('王文山','C802',64,'软件2班'),
('张珊','C803',72,'软件2班'),
('王文山','C804',64,'软件2班'),
('苏亚步','C801','72','软件1班');

1. Add the foreign key constraint FK_Cno to the teaching table, so that the "course number" in the teaching table refers to the "course number" in the course table (at the same time, when the course number information of the course table is updated, the course information in the teaching table is also updated accordingly ).

ALTER TABLE ADD CONSTRAINT FK_Cno FOREIGN KEY (course number) REFERENCES curriculum (course number) ON UPDATE CASCADE;

2. Delete the foreign key constraint of the teaching table.

ALTER TABLE DROP FOREIGN KEY FK_Cno;

3. Add an attribute to the student table: nationality, the data type is varchar(10).

ALTER TABLE student table ADD COLUMN ethnic varchar(10); ALTER TABLE student table ADD ethnic varchar(10);

4. Delete the newly added attribute ethnic group in the student table.

ALTER TABLE student table DROP COLUMN ethnic; ALTER TABLE student table DROP ethnic;

5. Make a list of students enrolled before New Year's Day in 2002.

SELECT name FROM student table WHERE enrollment date< '2002-01-01';

6. Count the "number" and "average age" of male and female students.

SELECT gender, COUNT(*) number of people, AVG (age) average age FROM student table GROUP BY gender;

7. Find out the name, gender and age of the students in Software Class 1 and Software Class 2.

select name, gender, age from student table
where class='software class 1'or class='software class 2';

8. Query the student ID and name of all students in the class of "Chen Xiangna" (if using student table self-join, use S1 and S2 for table aliases).

select s1. Student ID, s1. Name
from student table s1 join student table s2
on s1. Class=s2. Class and s2. Name='Agarwood';

9. Find out the "number of students" in Xiao Yizhu's class.

select count(*) Number of students
from student table s1 join student table s2
on s1. Class=s2. Class and s2. Name='Xiao Yizhu';

10. List the course name and class name brought by teacher Wang Wenshan.

select c. course name, tc. class name
from teaching table tc join course table c
on tc. course number = c. course number and tc. teacher name ='Wang Wenshan';

11. List the student numbers of students who have taken more than one course and their "number of courses".

select student number, count( ) number of electives
from transcript group by student number having
count(
)>1;

12. Find out all the course numbers that have not been taken by the student (DISTINCT if you can use it, don’t use it).

select course number from course table
where course number not in (select course number from grade table);

13. Find out the "number of students" who have taken the elective course "Advanced Mathematics" (only the field of "number of students" is displayed).

select count(*) the number of students
from the transcript sc join the curriculum c
on sc. course number = c. course number and c. course name ='Advanced Mathematics';

14. Find out the names of students whose grades in the School of Computer Science are blank.

select s. name
from student table s join score table sc
on s. student number = sc. student number and s. department ='computer' and sc, score is null;

15. Count the number of "students" in elective courses of each age (DISTINCT is not used if it can be used).

select s. age, count (distinct s. student number) number of students
from transcript sc join student table s
on sc. student number = s. student number group by s. age;

16. Retrieve the name and class of the student with the highest average grade.

SELECT S. name, S. class, AVG (SC. results) FROM student table S JOIN results table SC ON S. student number = SC. student number
GROUP BY SC. student number HAVING AVG (score) = (

SELECT MAX(SNO_AVG) FROM (SELECT 学号,AVG(成绩) SNO_AVG FROM 成绩表 GROUP BY 学号) SA

);

Guess you like

Origin blog.csdn.net/ziyue13/article/details/112071665