You are on page 1of 6

-- Lab 2 - SQL DDL

-- Replace "nn" after "Student" in the next line with your own user account number
USE Student47
BEGIN TRANSACTION
-- DROP TABLES FIRST
PRINT 'dropping tables - order matters'
DROP TABLE Registration ;
DROP TABLE Qualified ;
DROP TABLE Faculty ;
DROP TABLE Section ;
DROP TABLE Course ;
DROP TABLE Student ;
COMMIT;
BEGIN TRANSACTION
CREATE TABLE STUDENT (
STUDENT_ID INTEGER NOT NULL,
STUDENT_NAME VARCHAR(20),
PRIMARY KEY (STUDENT_ID))
CREATE TABLE FACULTY (
FACULTY_ID INTEGER NOT NULL,
FACULTY_NAME VARCHAR(20),
PRIMARY KEY (FACULTY_ID))
CREATE TABLE COURSE (
COURSE_ID VARCHAR(20) NOT NULL,
COURSE_NAME VARCHAR(20),
PRIMARY KEY (COURSE_ID))
CREATE TABLE QUALIFIED (
FACULTY_ID INTEGER NOT NULL,
COURSE_ID VARCHAR(20) NOT NULL,
DATE_QUALIFIED VARCHAR(10),
PRIMARY KEY (FACULTY_ID, COURSE_ID),
FOREIGN KEY (FACULTY_ID) REFERENCES FACULTY(FACULTY_ID) ON UPDATE CASCADE,
FOREIGN KEY (COURSE_ID) REFERENCES COURSE(COURSE_ID) ON DELETE NO ACTION)
CREATE TABLE SECTION (
SECTION_NO INTEGER NOT NULL,
SEMESTER VARCHAR(10) NOT NULL,
COURSE_ID VARCHAR(20),
PRIMARY KEY (SECTION_NO, SEMESTER),
FOREIGN KEY (COURSE_ID) REFERENCES COURSE(COURSE_ID) ON DELETE SET NULL)
CREATE TABLE REGISTRATION (
STUDENT_ID INTEGER NOT NULL,
SECTION_NO INTEGER NOT NULL,
SEMESTER VARCHAR(10) NOT NULL,
PRIMARY KEY (STUDENT_ID, SECTION_NO, SEMESTER),
FOREIGN KEY (STUDENT_ID) REFERENCES STUDENT(STUDENT_ID) ON DELETE CASCADE,
FOREIGN KEY (SECTION_NO, SEMESTER) REFERENCES SECTION(SECTION_NO, SEMESTER) ON DELETE
CASCADE)

COMMIT;
INSERT INTO STUDENT (STUDENT_ID, STUDENT_NAME)
VALUES (38214, 'Letersky')
INSERT INTO STUDENT (STUDENT_ID, STUDENT_NAME)
VALUES (54907, 'Altvater')
INSERT INTO STUDENT (STUDENT_ID, STUDENT_NAME)
VALUES (66324, 'Aiken')
INSERT INTO STUDENT (STUDENT_ID, STUDENT_NAME)
VALUES (70542, 'Marra')
INSERT INTO FACULTY (FACULTY_ID, FACULTY_NAME)
VALUES (2143, 'Birkin')
INSERT INTO FACULTY (FACULTY_ID, FACULTY_NAME)
VALUES (3467, 'Berndt')
INSERT INTO FACULTY (FACULTY_ID, FACULTY_NAME)
VALUES (4756, 'Collins')
INSERT INTO COURSE (COURSE_ID, COURSE_NAME)
VALUES ('ISM 3113', 'Syst Analysis')
INSERT INTO COURSE (COURSE_ID, COURSE_NAME)
VALUES ('ISM 3112', 'Syst Design')
INSERT INTO COURSE (COURSE_ID, COURSE_NAME)
VALUES ('ISM 4212', 'Database')
INSERT INTO COURSE (COURSE_ID, COURSE_NAME)
VALUES ('ISM 4930', 'Networking')
INSERT INTO QUALIFIED (FACULTY_ID, COURSE_ID, DATE_QUALIFIED)
VALUES (2143, 'ISM 3112', '9/1988')
INSERT INTO QUALIFIED (FACULTY_ID, COURSE_ID, DATE_QUALIFIED)
VALUES (2143, 'ISM 3113', '9/1988')
INSERT INTO QUALIFIED (FACULTY_ID, COURSE_ID, DATE_QUALIFIED)
VALUES (3467, 'ISM 4212', '9/1995')
INSERT INTO QUALIFIED (FACULTY_ID, COURSE_ID, DATE_QUALIFIED)
VALUES (3467, 'ISM 4930', '9/1996')
INSERT INTO QUALIFIED (FACULTY_ID, COURSE_ID, DATE_QUALIFIED)
VALUES (4756, 'ISM 3113', '9/1991')
INSERT INTO QUALIFIED (FACULTY_ID, COURSE_ID, DATE_QUALIFIED)
VALUES (4756, 'ISM 3112', '9/1991')
INSERT INTO SECTION (SECTION_NO, SEMESTER, COURSE_ID)
VALUES (2712, 'I-2006', 'ISM 3113')
INSERT INTO SECTION (SECTION_NO, SEMESTER, COURSE_ID)
VALUES (2713, 'I-2006', 'ISM 3113')
INSERT INTO SECTION (SECTION_NO, SEMESTER, COURSE_ID)

VALUES (2714, 'I-2006', 'ISM 4212')


INSERT INTO SECTION (SECTION_NO, SEMESTER, COURSE_ID)
VALUES (2715, 'I-2006', 'ISM 4930')
INSERT INTO REGISTRATION (STUDENT_ID, SECTION_NO, SEMESTER)
VALUES (38214, 2714, 'I-2006')
INSERT INTO REGISTRATION (STUDENT_ID, SECTION_NO, SEMESTER)
VALUES (54907, 2714, 'I-2006')
INSERT INTO REGISTRATION (STUDENT_ID, SECTION_NO, SEMESTER)
VALUES (54907, 2715, 'I-2006')
INSERT INTO REGISTRATION (STUDENT_ID, SECTION_NO, SEMESTER)
VALUES (66324, 2713, 'I-2006')