LAB 4 – Simple SQL statements Question 1 Write SQL statements to create a database containing the following tables Note need to add appropriate primary and foreign keys 1 Table Departments Name Type S[.]
LAB – Simple SQL statements Question 1: Write SQL statements to create a database containing the following tables Note: need to add appropriate primary and foreign keys 1.Table: Departments Name DeptID Name NoOfStudents Type varchar Nvarchar int Size Type varchar Nvarchar Nvarchar varchar Date Nvarchar Varchar float Numeric(4,2) Size Type varchar Nvarchar tinyint Size Type varchar varchar int int float varchar Size 50 Constraint: Name - not null 2.Table: Students Name StudentID LastName FirstName Sex DateOfBirth PlaceOfBirth DeptID Scholarship AverageScore Constraint: Sex should be ‘F’ or ‘M’ 30 10 30 3.Table: Courses Name CourseID Name Credits 35 4.Table: Results Name StudentID CourseID Year Semester Mark Grade Write SQL statements to insert data to database as follow: Table Departments: DeptID Name IS Information Systems NC Network and Communication SE Software Engineering CE Computer Engineering CS Computer Science Table Students: NoOfStudent s 4 StudentID LastName FirstName Sex DateOfBirth PlaceOfBirth DeptID Scholarship S001 Lê Kim Lan F 23/02/1990 Hà nội IS 130000 S002 Trần Minh Chánh M 24/12/1992 Bình Định NC 150000 S003 Lê An Tuyết F 21/02/1991 Hải phòng IS 170000 S004 Trần Anh Tuấn M 20/12/1993 TpHCM NC 80000 S005 Trần Thị Mai F 12/08/1991 TpHCM SE S006 Lê Thị Thu Thủy F 02/01/1991 An Giang IS S007 Nguyễn Kim Thư F 02/02/1990 Hà Nội SE 180000 S008 Lê Văn Long M 08/12/1992 TpHCM IS 190000 Table Courses: CourseID Name DS01 Database Systems AI01 Artificial Intelligence CN01 Computer Network CN01 Computer Network DSA1 Data Structures and Algorithms Table Results: StudentID S001 S001 S001 S001 S001 S002 S002 S002 S002 S003 S003 S003 S003 S004 S004 S005 S005 S005 S006 S006 CourseID DS01 DS01 AI01 AI01 CN01 DS01 DS01 CN01 DSA1 DS01 DS01 CN01 CN01 DS01 DSA1 DS01 CN01 CN01 AI01 CN01 Year 2017 2017 2017 2017 2017 2016 2017 2016 2016 2017 2017 2017 2017 2017 2018 2017 2017 2018 2018 2018 Credits 3 3 Semester 2 1 3 3 2 1 Mark 4.5 4.5 10 2.5 4.5 10 2.5 10 Grade Write SQL statements to bellow task Create Table Departments( DeptID varchar(4) primary key, name Nvarchar(50) Not null, NoOfStudents int, ) INSERT INTO Departments(DeptID,name,NoOfStudents) VALUES('IS','Information Systems',NULL) insert into Departments(DeptID,name,NoOfStudents) AverageScore values('NC','Network and Communication',NULL) insert into Departments(DeptID,name,NoOfStudents) values('SE','Software Engineering',NULL) insert into Departments(DeptID,name,NoOfStudents) values('CE','Computer Engineering',NULL) insert into Departments(DeptID,name,NoOfStudents) values('CS','Computer Science',NULL) Create Table Students( StudentID varchar(4) primary key, LastName Nvarchar(30) , FirstName Nvarchar(20) , Sex varchar(1) NOT NULL CHECK (Sex IN ('F', 'M')), DateOfBirth Date, PlaceOfBirth Nvarchar(30) , DeptID varchar(4), Scholarship float, AverageScore Numeric(4,2) ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S001','Lê','Kim Lan','F','1990-02-23','Hà nội' ,'IS',130000,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S002' ,'Trầ n' ,'Minh Chánh' ,'M' , '1992-12-24' , 'Bình Định' , 'NC' , 150000 ,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S003' ,'Lê', 'An Tuyế t' ,'F' ,'1991-02-21' ,'Hải phòng' ,'IS' ,170000 ,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S004' ,'Trầ n' ,'Anh Tuấ n', 'M ','1993-12-20','TpHCM','NC' ,80000 ,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S005' ,'Trầ n','Thị Mai' ,'F' ,'1991-08-12' ,'TpHCM' ,'SE' , ,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S006' ,'Lê' ,'Thị Thu Thủy' ,'F' ,'1991-01-02' ,'An Giang' ,'IS', ,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S007' ,'Nguyễ n', 'Kim Thư' ,'F' ,'1990-02-02' ,'Hà Nội', 'SE', 180000 ,Null ) INSERT INTO Student s(StudentID,LastName,FirstName,Sex,DateOfBirth,PlaceOfBirth,DeptID,Scholarship,AverageScore) values('S008' ,'Lê','Văn Long' ,'M' ,'1992-12-08','TpHCM' ,'IS' ,190000 ,Null ) Create Table Courses( CourseID varchar(4)primary key, Name Nvarchar(35), Credits tinyint, ) INSERT INTO Courses(CourseID,Name,Credits) values('DS01' ,'Database Systems' , ) INSERT INTO Courses(CourseID,Name,Credits) values('AI01' , 'Artificial Intelligence' , ) INSERT INTO Courses(CourseID,Name,Credits) values('CN01', 'Computer Network' , ) INSERT INTO Courses(CourseID,Name,Credits) values('CN01' ,'Computer Network' , ) INSERT INTO Courses(CourseID,Name,Credits) values('DSA1' , 'Data Structures and Algorithms' , ) Create Table Results( StudentID varchar(4), CourseID varchar(4), Year int, Semester int, Mark float, Grade varchar(6) ) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S001' , 'DS01' , 2017 ,1 ,3 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S001' , 'DS01' ,2017, ,6 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S001' , 'AI01' , 2017 , ,4.5 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S001' ,'AI01' ,2017 ,2 ,6 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S001 ' ,'CN01' ,2017 , 3, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S002' ,'DS01', 2016 , 1, 4.5 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S002' , 'DS01', '2017', ,7 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S002', 'CN01' ,'2016' , ,10 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S002' , 'DSA1' , '2016' , 3, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S003' , 'DS01' ,'2017' ,3 ,5 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S003' , 'DS01' ,'2017' , 1, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S003' , 'CN01' ,2017 , ,2.5 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S003' , 'CN01', 2017 , 3, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S004' , 'DS01' ,2017 , ,4.5 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S004' ,'DSA1', 2018 , ,10 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S005' , 'DS01', 2017 ,2, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S005' , 'CN01' ,2017 , 2, 2.5 ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S005' , 'CN01', 2018 , 1, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S006' ,'AI01', 2018 , 1, ,Null) INSERT INTO Results(StudentID,CourseID,Year,Semester,Mark,Grade) values('S006' ,'CN01' ,2018 , ,10 ,Null) Question Update NoOfStudents of each department in Departments table where NoOfStudents is the total number of students of each departments Note that for department that has no student, the NoOfStudents should be UPDATE Departments SET NoOfStudents = ( SELECT COUNT(*) FROM Students WHERE Departments.DeptID=Students.DeptID) Question Update AverageScore for each student so that for each course, we take only his/her highest Mark and the AverageScore of the student is calculated as the average mark of all the courses that the student joins UPDATE Students SET AverageScore = ( SELECT Sum(Mark)/COUNT(StudentID) FROM Results WHERE Students.StudentID=Results.StudentID) Question Update Grade in table Results so that: Grade = ‘Passed’ if 5