序文
皆さん、こんにちは。MySQL の練習問題をいくつか共有したいと思います。これらはすべて、自己組織化された試験サンプル問題と解答であり、知識ポイントを統合し、試験前に復習するために使用できます。
1.データベースを作成する
1. Mytest というデータベースを作成し、データベースに次の 5 つのテーブルを作成します.テーブル間の関係とフィールドを図に示します:
データベースを作成します。
create database Mytest;
教師テーブルを作成します。
create table teacher(
id smallint(4) not null primary key,
name varchar(20)
);
クラス テーブルを作成します。
create table class(
id smallint(6) not null primary key,
name varchar(50)
);
コース表の作成
create table course(
id smallint(3) not null primary key,
name varchar(100),
teacher_id smallint(6) not null,
foreign key(teacher_id) references teacher(id)
);
学生テーブルを作成する
create table student(
id smallint(6) not null primary key,
name varchar(20),
gender char(2),
class_id smallint(6) not null,
foreign key(class_id) references class(id)
);
スコア表の作成
create table score(
id int(11),
student_id smallint(6) not null,
course_id smallint(3) not null,
mark tinyint(4),
foreign key(student_id) references student(id),
foreign key(course_id) references course(id),
primary key(student_id,course_id)
);
2. 次のデータを挿入します
データを挿入するには 2 つの方法があります (2 つのテーブルが示されています)。
1. 単線挿入
例: クラス テーブル データの挿入
insert into class values(1,"软件工程1班");
insert into class values(2,"计算机科学技术1班");
insert into class values(3,"网络工程1班");
2、。一括挿入
例: 教師テーブル データの挿入
insert into teacher values
(1,"老虎"),
(2,"小马"),
(3,"大牛");
2.運用データベース
1. すべてのコースの名前と対応する教師の名前を照会します。
select course.name,teacher.name
from course inner join teacher
on course.teacher_id = teacher.id;
2. コース「データ構造」の成績がコース「Java 言語」の成績よりも低い学生の学生 ID を照会します。
select shuju.student_id from
(select score.student_id,course.name,score.mark from
score inner join course on
score.course_id=course.id where
course.name="数据结构") as shuju
inner JOIN
(select score.student_id,course.name,score.mark from
score inner join course on
score.course_id=course.id where
course.name="java语言") as java
on shuju.student_id=java.student_id
where shuju .mark<java.mark;
3 平均スコアが 65 を超える学生の ID と平均スコアをクエリします (小数点以下 2 桁を予約します)。
select student_id,round(avg(mark),2) as grade from score
group by student_id having grade>65;
4. 平均成績が 65 を超える学生の名前と平均成績を照会します (小数点以下 2 桁を予約してください)。
select student.name,round(avg(mark),2)
as grade from score
inner joinstudent
onscore.student_id=student.id
group by student_id having grade>65;
5. すべての学生の名前、コース数、合計成績を照会します。
select student.name,count(score.course_id)
as "选课数",sum(score.mark) as "总成绩" from score
inner join student on score.student_id=student.id
group by student_id;
6. 「Daniu」教師のクラスを学習していない生徒の名前を照会します。
select name from student
where id not in(select id from score where course_id in(3,3));
7.「Daniu」先生が教えるすべてのコースを学習した生徒の名前を照会します。
select student.name from
score inner join student
on score.student_id=student.id where score.course_id in
(select course.id from course inner join teacher
on course.teacher_id=teacher.id where teacher.name="大牛")
group by score.student_id;
8. コースの点数が 60 点未満の学生の名前を照会します。
select name from student where id in
(select student_id from score where mark<60
group by student_id);
9. すべてのコースを受講した学生の名前を照会します。
select name from student where id in
(select student_id from score
group by student_id
having count(1)=(select count(1) from course));
10. 「Xiaocao」と同じコースを少なくとも 1 つ持っている学生の名前を照会します。
select name as"姓名" from
student where id in
(select student_id from score where course_id in
(select course_id from score
inner join student
on score.student_id=student.id where student.name="小草")
group by student_id)
and name!="小草";
11.「Xiaocao」が受講したコースとは異なるコースを少なくとも 1 つ持っている学生の名前を照会します。
select name as "姓名" from student where id in
(select student_id from score where course_id in
(select course_id from score
inner join student
on score.student_id!=student.id
where student.name="小草")
group by student_id)
and name!="小草";
12. 各科目の最高点と最低点を照会します。次の形式で表示します。コース ID、最高点、最低点。
select course_id as "课程id",
max(mark) as "最高分",
min(mark) as "最低分"
from score group by course_id;
13. 1 つのコースのみを受講した学生の学生 ID と名前を照会します。
select
student_id as"学生学号",
student.name as"学生姓名" from score
inner join student
on score.student_id=student.id
group by student_id
having count(course_id)=1;
14. 各コースの平均成績を問い合わせると、結果は平均成績の昇順、平均成績が同じ場合はコースIDの降順で並べられます。
select course.name as"课程名称",
avg(mark) as"平均成绩" from score
inner join course
on score.course_id=course.id
group by course_id
order by avg(mark) asc,
course_id desc;
15. 全学生の「データベース主義」、「Java言語」、「C言語」の3科目の成績を平均成績の逆順に表示し、学生ID、データベース原理、Java言語の形式で表示する、C言語、コース数、平均点(上級者は難易度が高い)
- 最初に、単一の学生のデータベースの主なコースのスコアをクエリします
select mark as"分数" from score
left join course
on score.course_id = course.id
where course.name = "数据库原理" and score.student_id=1;
- 上記のクエリの結果を列フィールドとして使用して、最終結果を取得します
select student_id as"学生学号",
(select mark from score
left join course
on score.course_id = course.id
where course.name = "数据库原理"
and score.student_id=sc.student_id)
as "数据库原理",
(select mark from score
left join course
on score.course_id = course.id
where course.name = "java语言"
and score.student_id=sc.student_id)
as "java语言",
(select mark from score
left join course
on score.course_id = course.id
where course.name = "C语言"
and score.student_id=sc.student_id)
as "C语言",
count(course_id) as "课程数",
avg(mark) as "平均分"
from score as sc
group by student_id
order by avg(mark) desc;