Publicidad

Problem 1- Please create the following tables with appropriate primary (1).docx

rtodd884
8 de Feb de 2023
Problem 1- Please create the following tables with appropriate primary (1).docx
Problem 1- Please create the following tables with appropriate primary (1).docx
Problem 1- Please create the following tables with appropriate primary (1).docx
Próximo SlideShare
Problem 1- Please create the following tables with appropriate primary.docxProblem 1- Please create the following tables with appropriate primary.docx
Cargando en ... 3
1 de 3
Publicidad

Más contenido relacionado

Más de rtodd884(20)

Publicidad

Problem 1- Please create the following tables with appropriate primary (1).docx

  1. 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 CREATE TABLE student (
  2. sid int NOT NULL, sname varchar(50) NOT NULL, PRIMARY KEY (sid) ) CREATE TABLE teacher ( tid int NOT NULL, tname varchar(50) NOT NULL, PRIMARY KEY (tid) ) CREATE TABLE class ( cid int NOT NULL, cname varchar(50) NOT NULL, year int NOT NULL, semester varchar(50) NOT NULL, credit int NOT NULL, tid int NOT NULL, PRIMARY KEY (cid), FOREIGN KEY (tid) REFERENCES teacher(tid) ) CREATE TABLE grades ( sid int NOT NULL, cid int NOT NULL, grade char NOT NULL, FOREIGN KEY (sid) REFERENCES student(sid), FOREIGN KEY (cid) REFERENCES class(cid) ) INSERT INTO `student` (`sid`, `sname`) VALUES ('100', 'Mark James'), ('101', 'Nick Lwise'),('102', 'Kevin Morgan'); INSERT INTO `teacher` (`tid`, `tname`) VALUES ('200', 'Dr. Chen'), ('201', 'Mark dowen'),('203', 'Brett lee'); INSERT INTO `grades` (`sid`, `cid`, `grade`) VALUES ('100', '300', 'A'),('102', '301', 'C'),('102', '301', 'A'); SELECT * from student; SELECT * from class where semester='spring' && year='2016';
  3. SELECT cname FROM `class` NATURAL JOIN `teacher` WHERE year='2016'
Publicidad