Advertisement
Guest User

Setup

a guest
Dec 18th, 2014
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 14.68 KB | None | 0 0
  1. BEGIN
  2. FOR c IN (SELECT TABLE_NAME FROM user_tables) loop
  3. EXECUTE immediate ('drop table '||c.TABLE_NAME||' cascade constraints');
  4. END loop;
  5. END;
  6. /
  7. BEGIN
  8. FOR c IN (SELECT * FROM user_objects) loop
  9. EXECUTE immediate ('drop '||c.object_type||' '||c.object_name);
  10. END loop;
  11. END;
  12. /
  13.  
  14. -- Tables
  15.  
  16. CREATE TABLE StudyProgramme (
  17. programmeName VARCHAR(30),
  18. PRIMARY KEY (programmeName)
  19. );
  20.  
  21. CREATE TABLE Branches (
  22. branchName VARCHAR(30),
  23. programmeName VARCHAR(30),
  24. FOREIGN KEY (programmeName) REFERENCES StudyProgramme(programmeName),
  25. PRIMARY KEY (branchName, programmeName)
  26. );
  27.  
  28. CREATE TABLE Students (
  29. idd INT,
  30. studentName VARCHAR(30),
  31. branchName VARCHAR(30),
  32. programmeName VARCHAR (30),
  33. FOREIGN KEY (branchName, programmeName) REFERENCES Branches(branchName, programmeName),
  34. PRIMARY KEY (idd)
  35. );
  36.  
  37. CREATE TABLE Courses (
  38. code INT,
  39. credits INT,
  40. courseName VARCHAR(30),
  41. CONSTRAINT NonNegativeCredits CHECK (credits > 0 ),
  42. PRIMARY KEY (code)
  43. );
  44.  
  45. CREATE TABLE Studying (
  46. idd INT REFERENCES Students(idd),
  47. code INT REFERENCES Courses(code),
  48. PRIMARY KEY (idd, code)
  49. );
  50.  
  51. CREATE TABLE Finished (
  52. grade VARCHAR(1),
  53. idd INT REFERENCES Students(idd),
  54. code INT REFERENCES Courses(code),
  55. CONSTRAINT GradeSpan CHECK (grade IN ('U',3,4,5)),
  56. PRIMARY KEY (idd, code)
  57. );
  58.  
  59. CREATE TABLE RestrictedCourse (
  60. spots INT,
  61. code INT REFERENCES Courses(code),
  62. CONSTRAINT NonNegativeSpots CHECK (spots > 0),
  63. PRIMARY KEY (code)
  64. );
  65.  
  66. CREATE TABLE Departments (
  67. departmentName VARCHAR(20),
  68. PRIMARY KEY (departmentName)
  69. );
  70.  
  71. CREATE TABLE Categorized (
  72. typ VARCHAR(15),
  73. PRIMARY KEY (typ)
  74. );
  75.  
  76. CREATE TABLE IsCategorized (
  77. code INT REFERENCES Courses(code),
  78. typ VARCHAR(15) REFERENCES Categorized(typ),
  79. PRIMARY KEY (code, typ)
  80. );
  81.  
  82. CREATE TABLE HostedBy (
  83. programmeName VARCHAR(30) REFERENCES StudyProgramme(programmeName),
  84. departmentName VARCHAR(20) REFERENCES Departments(departmentName),
  85. PRIMARY KEY (programmeName, departmentName)
  86. );
  87.  
  88. CREATE TABLE isMandatorySP (
  89. programmeName VARCHAR(30) REFERENCES StudyProgramme(programmeName),
  90. code INT REFERENCES Courses(code),
  91. PRIMARY KEY (code, programmeName)
  92. );
  93.  
  94. CREATE TABLE isRecommended (
  95. code INT REFERENCES Courses(code),
  96. branchName VARCHAR(30),
  97. programmeName VARCHAR (30),
  98. FOREIGN KEY (branchName, programmeName) REFERENCES Branches(branchName, programmeName),
  99. PRIMARY KEY (code, branchName, programmeName)
  100. );
  101.  
  102. CREATE TABLE isMandatoryB (
  103. code INT REFERENCES Courses(code),
  104. branchName VARCHAR(30),
  105. programmeName VARCHAR (30),
  106. FOREIGN KEY (branchName, programmeName) REFERENCES Branches(branchName, programmeName),
  107. PRIMARY KEY (code, branchName, programmeName)
  108. );
  109.  
  110. CREATE TABLE Waiting (
  111. queueNumber INT,
  112. idd INT REFERENCES Students(idd),
  113. code INT REFERENCES Courses(code),
  114. CONSTRAINT NonNegativeQueue CHECK (queuenumber > 0),
  115. PRIMARY KEY (idd, code)
  116. );
  117.  
  118. CREATE TABLE PrerequisiteCourse (
  119. course_code INT REFERENCES Courses(code),
  120. required_course INT REFERENCES Courses(code),
  121. PRIMARY KEY (course_code, required_course)
  122. );
  123.  
  124. -- Insert
  125.  
  126. /* StudyProgramme */
  127.  
  128. INSERT INTO StudyProgramme
  129.   VALUES ('I');
  130.  
  131. INSERT INTO StudyProgramme
  132.   VALUES ('V');
  133.  
  134. INSERT INTO StudyProgramme
  135.   VALUES ('E');
  136.  
  137. INSERT INTO StudyProgramme
  138.   VALUES ('M');
  139.  
  140. /* Branches */
  141.  
  142. INSERT INTO Branches
  143.   VALUES ('Management','I');
  144.  
  145. INSERT INTO Branches
  146.   VALUES ('Mathematics','I');
  147.  
  148. INSERT INTO Branches
  149.   VALUES ('Management','V');
  150.  
  151. INSERT INTO Branches
  152.   VALUES ('Mathematics', 'E');
  153.  
  154. INSERT INTO Branches
  155.   VALUES ('Data', 'E');
  156.  
  157. INSERT INTO Branches
  158.   VALUES ('Mathematics', 'M');
  159.  
  160. INSERT INTO Branches
  161.   VALUES ('Construction', 'M');
  162.  
  163. /* Students */
  164.  
  165. INSERT INTO Students
  166.   VALUES ('1', 'Erik', 'Management', 'I');
  167.  
  168. INSERT INTO Students
  169.   VALUES ('2', 'Johan', 'Mathematics', 'I');
  170.  
  171. INSERT INTO Students
  172.   VALUES ('3', 'Frida', 'Construction', 'M');
  173.  
  174. INSERT INTO Students
  175.   VALUES ('4', 'Stina', 'Management', 'V');
  176.  
  177. INSERT INTO Students
  178.   VALUES ('5', 'Emma', 'Data', 'E');
  179.  
  180.  
  181. /* Courses */
  182.  
  183. INSERT INTO Courses
  184.   VALUES ('146', '7', 'Avslutande matte');
  185.  
  186. INSERT INTO Courses
  187.   VALUES ('100', '7', 'Olinjär algebra');
  188.  
  189. INSERT INTO Courses
  190.   VALUES ('200', '15', 'Ek 4an');
  191.  
  192. INSERT INTO Courses
  193.   VALUES ('243', '30', 'Programmera mera');
  194.  
  195. INSERT INTO Courses
  196.   VALUES ('23', '15', 'Matematikens underbara värld');
  197.  
  198. INSERT INTO Courses
  199.   VALUES ('50', '7', 'Alkemi');
  200.  
  201. INSERT INTO Courses
  202.   VALUES ('10', '7', 'Palmestri');
  203.  
  204. /* Categorized */
  205.  
  206. INSERT INTO Categorized
  207.   VALUES ('Mathematical');
  208.  
  209. INSERT INTO Categorized
  210.   VALUES ('Seminar');
  211.  
  212. INSERT INTO Categorized
  213.   VALUES ('Research');
  214.  
  215. /* Departments */
  216.  
  217. INSERT INTO Departments
  218.   VALUES ('Dep för CS');
  219.  
  220. INSERT INTO Departments
  221.   VALUES ('Dep för Logistik');
  222.  
  223. INSERT INTO Departments
  224.   VALUES ('Dep för Management');
  225.  
  226. INSERT INTO Departments
  227.   VALUES ('Dep för Matematik');
  228.  
  229. INSERT INTO Departments
  230.   VALUES ('Dep för Kvantfysik');
  231.  
  232. /* HostedBy */
  233.  
  234. INSERT INTO HostedBy
  235.   VALUES ('I', 'Dep för Management');
  236.  
  237. INSERT INTO HostedBy
  238.   VALUES ('I', 'Dep för Matematik');
  239.  
  240. INSERT INTO HostedBy
  241.   VALUES ('V', 'Dep för Logistik');
  242.  
  243. INSERT INTO HostedBy
  244.   VALUES ('E', 'Dep för Kvantfysik');
  245.  
  246. INSERT INTO HostedBy
  247.   VALUES ('M', 'Dep för CS');
  248.  
  249. /* IsCategorized */
  250.  
  251. INSERT INTO IsCategorized
  252.   VALUES ('200', 'Seminar');
  253.  
  254. INSERT INTO IsCategorized
  255.   VALUES ('100', 'Mathematical');
  256.  
  257. INSERT INTO IsCategorized
  258.   VALUES ('23', 'Research');
  259.  
  260. INSERT INTO IsCategorized
  261.   VALUES ('146', 'Mathematical');
  262.  
  263. INSERT INTO IsCategorized
  264.   VALUES ('243', 'Mathematical');
  265.  
  266. /* IsMandatoryB */
  267.  
  268. INSERT INTO IsMandatoryB
  269.   VALUES ('100', 'Management', 'V');
  270.  
  271. INSERT INTO IsMandatoryB
  272.   VALUES ('200', 'Construction', 'M');
  273.  
  274. /* IsMandatorySP */
  275.  
  276. INSERT INTO IsMandatorySP
  277.   VALUES ('M', '243');
  278.  
  279. /* IsRecommended */
  280.  
  281. INSERT INTO IsRecommended
  282.   VALUES ('100', 'Management', 'I');
  283.  
  284. INSERT INTO IsRecommended
  285.   VALUES ('23', 'Data', 'E');
  286.  
  287. /* Finished */
  288.  
  289. INSERT INTO Finished
  290.   VALUES ('U', '4', '100');
  291.  
  292. INSERT INTO Finished
  293.   VALUES ('5', '5', '200');
  294.  
  295. INSERT INTO Finished
  296.   VALUES ('5', '3', '100');
  297.  
  298. INSERT INTO Finished
  299.   VALUES ('3', '5', '243');
  300.  
  301. INSERT INTO Finished
  302.   VALUES ('5', '2', '200');
  303.  
  304. INSERT INTO Finished
  305.   VALUES ('4', '2', '100');
  306.  
  307. INSERT INTO Finished
  308.   VALUES ('3', '1', '23');
  309.  
  310. INSERT INTO Finished
  311.   VALUES ('3', '5', '23');  
  312.  
  313. /* RestrictedCourse */
  314.  
  315. INSERT INTO RestrictedCourse
  316.   VALUES ('100', '100');
  317.  
  318. INSERT INTO RestrictedCourse
  319.   VALUES ('50', '23');
  320.  
  321. INSERT INTO RestrictedCourse
  322.   VALUES ('1', '50');
  323.  
  324. INSERT INTO RestrictedCourse
  325.   VALUES ('1', '10');
  326.  
  327. /* Studying */
  328.  
  329. INSERT INTO Studying
  330.   VALUES ('1', '100');
  331.  
  332. INSERT INTO Studying
  333.   VALUES ('1', '200');
  334.  
  335. INSERT INTO Studying
  336.   VALUES ('2', '100');
  337.  
  338. INSERT INTO Studying
  339.   VALUES ('1', '50');
  340.  
  341. INSERT INTO Studying
  342.   VALUES ('1', '10');
  343.  
  344. INSERT INTO Studying
  345.   VALUES ('2', '10');
  346.  
  347. INSERT INTO Studying
  348.   VALUES ('3', '10');
  349.  
  350. /* Waiting */
  351.  
  352. INSERT INTO Waiting
  353.   VALUES ('1', '2', '146');
  354.  
  355. INSERT INTO Waiting
  356.   VALUES ('1', '3', '243');
  357.  
  358. INSERT INTO Waiting
  359.   VALUES ('1', '3', '50');
  360.  
  361. INSERT INTO Waiting
  362.   VALUES ('2', '4', '50');
  363.  
  364. /* Prerequisite */
  365.  
  366. INSERT INTO PrerequisiteCourse
  367.   VALUES ('146', '100');
  368.  
  369. -- Views
  370.  
  371. /* For all students, their names, and the programme and branch they are following.*/
  372.  
  373. CREATE OR REPLACE VIEW StudentsFollowing AS
  374. SELECT studentname, idd, branchname, programmename
  375. FROM Students;
  376.  
  377. CREATE OR REPLACE VIEW AllCourses AS
  378. SELECT coursename, code, credits
  379. FROM Courses;
  380.  
  381. /* For all students, all finished courses, along with their grades (grade 'U', '3', '4' or '5').*/
  382.  
  383. CREATE OR REPLACE VIEW FinishedCourses AS
  384. SELECT DISTINCT Students.STUDENTNAME, coursename, grade
  385. FROM Courses, Students, Finished
  386. WHERE Courses.CODE = Finished.CODE AND Finished.idd = students.idd;
  387.  
  388. /* All registered and waiting students for all courses, along with their waiting status ('registered' or 'waiting').*/
  389.  
  390. CREATE OR REPLACE VIEW Registrations AS
  391. (SELECT studentname, students.idd, coursename, Courses.code, 'waiting' AS STATUS
  392. FROM Students, Waiting, Courses
  393. WHERE Students.idd = waiting.idd AND waiting.code = courses.code)
  394. UNION
  395. (SELECT studentname, students.idd, coursename, Courses.code, 'registered' AS STATUS
  396. FROM Students, Studying, Courses
  397. WHERE Students.idd = studying.idd AND studying.code = courses.code);
  398.  
  399. /* For all students, all passed courses, i.e. courses finished with a grade other than 'U'.
  400. This view is intended as a helper view towards the PathToGraduation view, and will not be directly used by your application.*/
  401.  
  402. CREATE OR REPLACE VIEW PassedCourses AS
  403.   SELECT studentname, Students.idd, coursename, finished.code, grade, credits
  404.   FROM Courses, Finished, Students
  405.   WHERE Courses.code = finished.code AND Finished.idd = Students.idd
  406.   AND grade != 'U';
  407.  
  408. /* For all students, the mandatory courses (branch and programme) they have not yet passed.*/
  409.  
  410. CREATE OR REPLACE VIEW UnreadMandatory AS
  411.   (SELECT studentName, idd, code
  412.   FROM Students NATURAL JOIN isMandatorySP)
  413.   UNION
  414.   (SELECT studentNAme, idd, code
  415.   FROM Students NATURAL JOIN isMandatoryB)
  416.   MINUS
  417.   (SELECT studentName, idd, code
  418.   FROM Students NATURAL JOIN PassedCourses);
  419.  
  420. /* PathToGraduation
  421. For all students, their path to graduation, i.e. a view with columns for:
  422. the number of credits they have passed.
  423. the number of branch-specific mandatory and recommended credits they have passed.
  424. the number of mandatory courses they have yet to pass (branch or programme).
  425. the number of credits they have passed in courses that are classified as math courses.
  426. the number of credits they have passed in courses that are classified as research courses.
  427. the number of seminar courses they have passed.
  428. whether or not they qualify for graduation.*/
  429.  
  430.  
  431. /* the number of credits they have passed.*/
  432.  
  433. CREATE OR REPLACE VIEW PTGpassedCredits AS
  434. SELECT DISTINCT students.studentname,students.idd, SUM(DISTINCT credits) AS passedCredits
  435. FROM Students, Finished, Courses
  436. WHERE Students.idd = Finished.IDD AND Finished.CODE = Courses.CODE AND grade != 'U'
  437. GROUP BY students.studentname, students.idd;
  438.  
  439.  
  440. /* the number of branch-specific mandatory and recommended credits they have passed.*/
  441.  
  442. CREATE OR REPLACE VIEW PTGMandatoryBSCourses AS
  443. SELECT studentname, Students.idd, SUM(DISTINCT credits) AS mandatoryBScoursesCredits
  444. FROM Students, Finished, Courses, isMandatoryB, isRecommended
  445. WHERE Students.idd = Finished.idd AND Finished.code = Courses.code AND grade != 'U'
  446. AND (Courses.code = isMandatoryB.CODE OR Courses.code = isRecommended.code)
  447. GROUP BY studentname, Students.idd;
  448.  
  449.  
  450.  
  451. /* the number of mandatory courses they have yet to pass (branch or programme).*/
  452.  
  453. CREATE OR REPLACE VIEW PTGCoursesYetToPass AS
  454. SELECT studentname, idd, code, COUNT(DISTINCT code) AS coursesYetToPass
  455. FROM UnreadMandatory
  456. GROUP BY studentname, idd, code
  457. MINUS
  458. SELECT studentname, idd, code, COUNT(DISTINCT code) AS coursesYetToPass
  459. FROM PassedCourses
  460. GROUP BY studentname, idd, code;
  461.  
  462.  
  463. /*the number of credits they have passed in courses that are classified as math courses.*/
  464.  
  465.  
  466. CREATE OR REPLACE VIEW PTGMathCoursesPassed AS
  467.   SELECT studentname, idd, SUM(DISTINCT credits) AS passedMathCredits
  468.   FROM PassedCourses NATURAL JOIN IsCategorized
  469.   WHERE typ = 'Mathematical'
  470.   GROUP BY studentname, idd;
  471.  
  472.  
  473.   /*the number of credits they have passed in courses that are classified as research courses.*/
  474.  
  475. CREATE OR REPLACE VIEW PTGResearchCoursesPassed AS
  476.   SELECT studentname, idd, SUM(DISTINCT credits) AS passedResearchCredits
  477.   FROM PassedCourses NATURAL JOIN IsCategorized
  478.   WHERE typ = 'Research'
  479.   GROUP BY studentname, idd;
  480.  
  481.  
  482. /*the number of seminar courses they have passed.*/
  483.  
  484. CREATE OR REPLACE VIEW PTGSeminarCoursesPassed AS
  485.   SELECT studentname, idd, COUNT(DISTINCT code) AS passedSeminarCourses
  486.   FROM PassedCourses NATURAL JOIN IsCategorized
  487.   WHERE typ = 'Seminar'
  488.   GROUP BY studentname, idd;
  489.  
  490.  
  491.   /*whether or not they qualify for graduation.*/
  492.  
  493. CREATE OR REPLACE VIEW PTGGraduationYes AS
  494.   SELECT studentname, idd, 'Yes' AS QualifiesForGraduation
  495.   FROM PassedCourses
  496.   NATURAL JOIN PTGMandatoryBSCourses
  497.   NATURAL JOIN PTGMathCoursesPassed
  498.   NATURAL JOIN PTGSeminarCoursesPassed
  499.   NATURAL JOIN PTGResearchCoursesPassed
  500.   WHERE MandatoryBScoursesCredits >= 10 AND passedMathCredits >= 20 AND passedSeminarCourses > 0 AND passedResearchCredits >= 10
  501.   AND studentname NOT IN (SELECT Studentname FROM PTGCoursesYetToPass)
  502.   GROUP BY studentname, idd, 'Yes';
  503.  
  504.   CREATE OR REPLACE VIEW PTGGraduationNo AS
  505.   SELECT students.Studentname, students.idd, 'No' AS QualifiesForGraduation
  506.   FROM Students
  507.   WHERE students.Studentname NOT IN (SELECT Studentname FROM PTGGraduationYes);
  508.  
  509. CREATE OR REPLACE VIEW PTGGRaduationTotal AS
  510.   (SELECT PTGGRaduationNo.Studentname, PTGGRaduationNo.idd, 'No' AS QualifiesForGraduation
  511.   FROM PTGGraduationNo)
  512.   UNION
  513.   (SELECT PTGGRaduationYes.Studentname, PTGGRaduationYes.idd, 'Yes' AS QualifiesForGraduation
  514.   FROM PTGGraduationYes);
  515.  
  516.   /* PathToGraduation
  517. For all students, their path to graduation, i.e. a view with columns for:
  518. the number of credits they have passed.
  519. the number of branch-specific mandatory and recommended credits they have passed.
  520. the number of mandatory courses they have yet to pass (branch or programme).
  521. the number of credits they have passed in courses that are classified as math courses.
  522. the number of credits they have passed in courses that are classified as research courses.
  523. the number of seminar courses they have passed.
  524. whether or not they qualify for graduation.*/
  525.  
  526. CREATE OR REPLACE VIEW PathToGraduation AS
  527.   SELECT DISTINCT PTGGraduationtotal.idd, PTGGraduationtotal.studentname, PTGpassedcredits.passedcredits,
  528.   PTGMandatoryBSCourses.mandatorybscoursescredits, PTGCoursesYetToPass.CoursesYetToPass, PTGMathCoursesPassed.passedmathcredits,
  529.   PTGResearchCoursesPassed.passedresearchcredits, PTGSeminarCoursesPassed.passedseminarcourses, qualifiesforgraduation
  530.  
  531.   FROM PTGGraduationTotal
  532.   FULL OUTER JOIN PTGPassedCredits ON PTGGraduationTotal.idd = PTGpassedcredits.idd
  533.   FULL OUTER JOIN PTGMandatoryBSCourses ON PTGGraduationTotal.idd = PTGMandatoryBSCourses.idd
  534.   FULL OUTER JOIN PTGCoursesYetToPass ON PTGGraduationTotal.idd = PTGCoursesYetToPass.idd
  535.   FULL OUTER JOIN PTGMathCoursesPassed ON PTGGraduationTotal.idd = PTGMathCoursesPassed.idd
  536.   FULL OUTER JOIN PTGResearchCoursesPassed ON PTGGraduationTotal.idd = PTGResearchCoursesPassed.idd
  537.   FULL OUTER JOIN PTGSeminarCoursesPassed ON PTGGraduationTotal.idd = PTGSeminarCoursesPassed.idd
  538.  
  539.   ORDER BY PTGGraduationTotal.IDD;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement