Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Week1: Database #3

Open
wants to merge 1 commit into
base: database-assignment-1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions bai2.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
-- Bạn hãy viết một script để tạo các bản cho hệ thống với cấu trúc ở dưới
USE ep_week1;

create table if not exists Professor(
prof_id int primary key auto_increment,
prof_lname varchar(50),
prof_fname varchar(50)
);

create table if not exists Student(
stud_id int primary key auto_increment,
stud_lname varchar(50),
stud_fname varchar(50),
stud_street varchar(255),
stud_city varchar(50),
stud_zip varchar(10)
);

create table if not exists Room
(
room_id int primary key auto_increment,
room_loc varchar(50),
room_cap varchar(50),
class_id int
);

create table if not exists Course
(
course_id int primary key auto_increment,
course_name varchar(255)
);

create table if not exists Class
(
class_id int primary key auto_increment,
class_name varchar(255),
prof_id int,
course_id int,
room_id int,
FOREIGN KEY (prof_id) REFERENCES Professor (prof_id),
FOREIGN KEY (course_id) REFERENCES Course (course_id),
FOREIGN KEY (room_id) REFERENCES Room (room_id)
);

alter table Room
add FOREIGN KEY (class_id) REFERENCES Class (class_id);

create table if not exists Enroll
(
stud_id int,
class_id int,
grade varchar(3),
primary key (stud_id, class_id),
FOREIGN KEY (stud_id) REFERENCES Student (stud_id),
FOREIGN KEY (class_id) REFERENCES Class (class_id)
);



74 changes: 74 additions & 0 deletions bai3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
use ep_week1;

-- những cặp student-professor có dạy học nhau và số lớp mà họ có liên quan
SELECT Student.stud_fname"Studen_Name", Professor.prof_fname as "Professor_Name", Class.class_id as "Class"
FROM Student
INNER JOIN Enroll ON Student.stud_id = Enroll.stud_id
INNER JOIN Class ON Class.class_id = Enroll.class_id
INNER JOIN Professor ON Professor.prof_id = Class.prof_id;

-- những course (distinct) mà 1 professor cụ thể đang dạy

SELECT DISTINCT CONCAT(Professor.prof_lname, Professor.prof_fname),Course.course_name
From Course
INNER JOIN Class ON Course.course_id = Class.course_id
INNER JOIN Professor ON Class.prof_id = Professor.prof_id;

-- những course (distinct) mà 1 student cụ thể đang học
select distinct concat(Student.stud_fname, " ", Student.stud_lname), Course.course_name
from Student
inner join Enroll on Enroll.stud_id = Student.stud_id
inner join Class on Enroll.class_id = Class.class_id
inner join Course on Course.course_id = Class.course_id;

-- điểm số là A, B, C, D, E, F tương đương với 10, 8, 6, 4, 2, 0
select * from Enroll;
create view Student_Grade as
select Enroll.stud_id, Enroll.class_id,
case Enroll.grade
when "A" then 10
when "B" then 8
when "C" then 6
when "D" then 4
when "E" then 2
when "F" then 0
end as "gpa"
from Enroll;

select * from Student_Grade;

-- điểm số trung bình của 1 học sinh cụ thể (quy ra lại theo chữ cái, và xếp loại học lực (weak nếu avg < 5, average nếu >=5 < 8, good nếu >=8 )
select Student.stud_id,
Student.stud_fname,
AVG(Student_Grade.gpa) as 'average_score',
case
when AVG(Student_Grade.gpa) < 5 then 'week'
when AVG(Student_Grade.gpa) >= 8 then 'good'
else 'average'
end as "assess"
from Student
inner join Student_Grade on Student_Grade.stud_id = Student.stud_id
group by Student.stud_id;

-- điểm số trung bình của các class (quy ra lại theo chữ cái)
select Class.class_id, Class.class_name, AVG(Student_Grade.gpa) as 'average_score',
case
when AVG(Student_Grade.gpa) < 5 then 'week'
when AVG(Student_Grade.gpa) >= 8 then 'good'
else 'average'
end as 'assess'
from Class
inner join Student_Grade on Student_Grade.class_id = Class.class_id
group by Class.class_id;

-- điểm số trung bình của các course (quy ra lại theo chữ cái)
select Course.course_id, Course.course_name, AVG(Student_Grade.gpa) as 'average_score',
case
when AVG(Student_Grade.gpa) < 5 then 'week'
when AVG(Student_Grade.gpa) >= 8 then 'good'
else 'average'
end as 'assess'
from Course
inner join Class on Class.course_id = Course.course_id
inner join Student_Grade on Student_Grade.class_id = Class.class_id
group by Course.course_id