Problem 1. Please create the following tables with appropriate primary keys & foreign keys.
The list of tables is:
• Student table with 2 columns: sid (student ID), sname (student name).
• Teacher table with 2 columns: tid (teacher ID), tname (teacher name)
• Class table with 6 columns: cid (class ID), cname (class name), year (year of class), semester (semester of class, e.g., fall, spring), credit (number of credits), tid (teacher ID).
• Grades table with 3 columns: sid (student ID), cid (class ID), grade (grade, 4 is A, 3 is B, 2 is C, 1 is D, 0 is F).
Problem 2. Insert at least three rows of data to each table. Make sure you keep the primary key and foreign key constraints.
Problem 3. Please write ONE SQL statement for each of the following tasks using tables created in Problem 1.
Task 1: Return the total number of students
Task 2: Return the names of classes offered in Spring 2016 (spring is semester, 2016 is year).
Task 3: Return the names of classes taught by Dr. Chen in 2016.
Task 4: Return the IDs of classes with at least 2 students taking the class.
Task 5: Return the names of students who ever took Dr. Chen’s classes.
Task 6: Return the average grade of class IS 633 offered in spring 2016.
Solution
Question: Problem 1:
Table creations are given below: We have used CREATE TABLE syntax for table creation
i) CREATE TABLE Student (sid int NOT NULL, sname varchar(255), PRIMARY KEY (sid))
ii) CREATE TABLE Teacher (tid int NOT NULL, tname varchar(255), PRIMARY KEY (tid))
iii) CREATE TABLE Class (cid int NOT NULL, cname varchar(255), year int, semester varchar(255), credit int, tid int NOT NULL, PRIMARY KEY (cid), FOREIGN KEY (tid) REFERENCES Teacher(tid) )
iv) CREATE TABLE Grades (sid int NOT NULL, cid int NOT NULL, grade int)
.