forked from Christian.Pape/gsd-questions
Christian Pape
7 years ago
commit
1b1fc65eff
9 changed files with 520 additions and 0 deletions
-
12.gitignore
-
11Makefile
-
229gsd-questions.tex
-
BINimages/task3-chen.graffle
-
BINimages/task3-chen.pdf
-
22queries/task1-sql-statements.sql
-
142queries/task2-constraints-and-integrity.sql
-
81queries/task3-ddl-example.sql
-
23queries/task3-ddl.sql
@ -0,0 +1,12 @@ |
|||||
|
*.aux |
||||
|
*.log |
||||
|
.DS_Store |
||||
|
*.synctex.gz |
||||
|
*.bbl |
||||
|
*.blg |
||||
|
*.nav |
||||
|
*.toc |
||||
|
*.out |
||||
|
images/*eps-converted-to.pdf |
||||
|
gsd-questions-solution.pdf |
||||
|
gsd-questions.pdf |
@ -0,0 +1,11 @@ |
|||||
|
all: solution exercise |
||||
|
|
||||
|
solution: |
||||
|
- cp gsd-questions.tex gsd-questions-solution.tex && pdflatex '\def\loesung{} \input{gsd-questions-solution}' && rm gsd-questions-solution.tex |
||||
|
|
||||
|
exercise: |
||||
|
- pdflatex gsd-questions |
||||
|
|
||||
|
clean: |
||||
|
rm -rf gsd-questions.aux gsd-questions.log gsd-questions.out gsd-questions.pdf |
||||
|
rm -rf gsd-questions-solution.aux gsd-questions-solution.log gsd-questions-solution.out gsd-questions-solution.pdf |
@ -0,0 +1,229 @@ |
|||||
|
\documentclass[a4paper,11pt]{article} |
||||
|
|
||||
|
\usepackage{amsmath} |
||||
|
\usepackage{color} |
||||
|
\usepackage{colortbl} |
||||
|
\usepackage{anysize} |
||||
|
\usepackage{enumerate} |
||||
|
|
||||
|
\usepackage[utf8]{inputenc} |
||||
|
\usepackage{amssymb} |
||||
|
\usepackage{amsmath} |
||||
|
\usepackage{tikz} |
||||
|
\usepackage{listings} |
||||
|
|
||||
|
\pagestyle{empty} |
||||
|
\setlength{\parindent}{0mm} |
||||
|
|
||||
|
\begin{document} |
||||
|
%\def\loesung{} |
||||
|
\textsc{Hochschule Fulda}{\small\hfill GSD Database \& SQL questionnaire}\\ |
||||
|
{\small Fachbereich Angewandte Informatik{\small\hfill SS 18}\\ |
||||
|
Dipl.-Inf. Christian Pape}\\ |
||||
|
|
||||
|
%------------------------------------------------------------------------------ |
||||
|
\textbf{Task 1 (SQL statements):}\\ |
||||
|
%------------------------------------------------------------------------------ |
||||
|
Write down the results of the following SQL statements on table T. |
||||
|
\begin{quote} |
||||
|
\begin{tabular}[t]{l|l|l|l} |
||||
|
T & A & B & C \\ |
||||
|
\hline |
||||
|
& 1 & blue & 10 \\ |
||||
|
& 2 & blue & 40 \\ |
||||
|
& 3 & pink & 30 \\ |
||||
|
& 4 & orange & 10 \\ |
||||
|
& 5 & orange & 20 \\ |
||||
|
& 6 & orange & 50 \\ |
||||
|
& 7 & orange & 50 \\ |
||||
|
& 8 & black & 50 \\ |
||||
|
& 9 & black & 40 \\ |
||||
|
& 10 & violet & 10 \\ |
||||
|
& 11 & violet & 20 \\ |
||||
|
& 12 & violet & 10 \\ |
||||
|
\end{tabular} |
||||
|
\end{quote} |
||||
|
|
||||
|
\begin{description} |
||||
|
\item[a.)] {\tt SELECT B, COUNT(*) FROM T GROUP BY B HAVING SUM(C)<=40} |
||||
|
\begin{quote} |
||||
|
\begin{tabular}[t]{|p{2.5cm}|p{2.5cm}|} |
||||
|
\hline |
||||
|
B & COUNT(*) \\ |
||||
|
\hline |
||||
|
\hline |
||||
|
\ifdefined\loesung |
||||
|
\textcolor{red}{pink} & \textcolor{red}{1} \\[0.3cm] |
||||
|
\hline |
||||
|
\textcolor{red}{violet} & \textcolor{red}{3} \\[0.3cm] |
||||
|
\hline |
||||
|
\textcolor{red}{} & \textcolor{red}{} \\[0.3cm] |
||||
|
\hline |
||||
|
\textcolor{red}{} & \textcolor{red}{} \\[0.3cm] |
||||
|
\else |
||||
|
& \\[0.3cm] |
||||
|
\hline |
||||
|
& \\[0.3cm] |
||||
|
\hline |
||||
|
& \\[0.3cm] |
||||
|
\hline |
||||
|
& \\[0.3cm] |
||||
|
\fi |
||||
|
\hline |
||||
|
\end{tabular} |
||||
|
\end{quote} |
||||
|
|
||||
|
\item[b.)] {\tt SELECT B, COUNT(*) FROM T WHERE C>35 \\GROUP BY B HAVING COUNT(*)>=2} |
||||
|
\begin{quote} |
||||
|
\begin{tabular}[t]{|p{2.5cm}|p{2.5cm}|} |
||||
|
\hline |
||||
|
B & COUNT(*) \\ |
||||
|
\hline |
||||
|
\hline |
||||
|
\ifdefined\loesung |
||||
|
\textcolor{red}{orange} & \textcolor{red}{2} \\[0.3cm] |
||||
|
\hline |
||||
|
\textcolor{red}{black} & \textcolor{red}{2} \\[0.3cm] |
||||
|
\hline |
||||
|
\textcolor{red}{} & \textcolor{red}{} \\[0.3cm] |
||||
|
\hline |
||||
|
\textcolor{red}{} & \textcolor{red}{} \\[0.3cm] |
||||
|
\else |
||||
|
& \\[0.3cm] |
||||
|
\hline |
||||
|
& \\[0.3cm] |
||||
|
\hline |
||||
|
& \\[0.3cm] |
||||
|
\hline |
||||
|
& \\[0.3cm] |
||||
|
\fi |
||||
|
\hline |
||||
|
\end{tabular} |
||||
|
\end{quote} |
||||
|
\end{description} |
||||
|
|
||||
|
\pagebreak |
||||
|
\textsc{Hochschule Fulda}{\small\hfill GSD Database \& SQL questionnaire}\\ |
||||
|
{\small Fachbereich Angewandte Informatik{\small\hfill SS 18}\\ |
||||
|
Dipl.-Inf. Christian Pape}\\ |
||||
|
|
||||
|
%------------------------------------------------------------------------------ |
||||
|
\textbf{Task 2 (constraints \& integrity):}\\ |
||||
|
%------------------------------------------------------------------------------ |
||||
|
The following table definition with integrity constraints are given. |
||||
|
\begin{quote} |
||||
|
{\tt |
||||
|
\begin{tabbing} |
||||
|
CREATE TABLE T1 (\=A INT, B INT, C INT,\\ |
||||
|
\>CONSTRAINT T1\_PS PRIMARY KEY (A,B),\\ |
||||
|
\>CONSTRAINT T1\_SK UNIQUE (C));\\ |
||||
|
\\ |
||||
|
CREATE TABLE T2 (A INT, B INT, C INT, D INT, E INT,\\ |
||||
|
\>CONSTRAINT T2\_PS PRIMARY KEY (A),\\ |
||||
|
\>CONSTRAINT T2\_FS1 FOREIGN KEY (B,C) REFERENCES T1(A,B),\\ |
||||
|
\>CONSTRAINT T2\_FS2 FOREIGN KEY (D) REFERENCES T1(C),\\ |
||||
|
\>CONSTRAINT T2\_E\_NN CHECK (E IS NOT NULL),\\ |
||||
|
\>CONSTRAINT T2\_E\_13 CHECK (E BETWEEN 1 AND 3)); \\ |
||||
|
\end{tabbing}} |
||||
|
\end{quote} |
||||
|
The table T1 and T2 contain the following tuples. NULL values are indicated by a hyphen (-). |
||||
|
\begin{quote} |
||||
|
|
||||
|
\begin{tabular}[t]{l|l|l|l} |
||||
|
T1 & A & B & C \\ |
||||
|
\hline |
||||
|
& 1 & 1 & 5 \\ |
||||
|
& 2 & 2 & 10 \\ |
||||
|
& 3 & 3 & 15 \\ |
||||
|
& 4 & 4 & 20 \\ |
||||
|
& 5 & 5 & 25 \\ |
||||
|
\end{tabular} |
||||
|
\hspace{2cm} |
||||
|
\begin{tabular}[t]{l|l|l|l|l|l} |
||||
|
T2 & A & B & C & D & E\\ |
||||
|
\hline |
||||
|
& 100 & - & - & 15 & 1 \\ |
||||
|
& 101 & 2 & 2 & 25 & 2 \\ |
||||
|
& 102 & 2 & 2 & - & 3 \\ |
||||
|
& 103 & 3 & 3 & 10 & 3 \\ |
||||
|
& 104 & 3 & 3 & 10 & 3 \\ |
||||
|
\end{tabular} |
||||
|
|
||||
|
\end{quote} |
||||
|
|
||||
|
Which of the following INSERT-statements are violating/not violating the defined integrity constraints? Only one integrity constraint will be violated for each statement. Enter the name of the violated integrity constraint or ''none'' if all conditions are met. |
||||
|
|
||||
|
\begin{tabular}[t]{|l|l|l|} |
||||
|
\hline |
||||
|
Nr. & INSERT-statement & Violated constraint \\[0.3cm] |
||||
|
\hline |
||||
|
1 & {\tt INSERT INTO T1 VALUES (1, 2, 5)} & \ifdefined\loesung \textcolor{red}{T1\_SK} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
2 & {\tt INSERT INTO T1 VALUES (1, 2, 30)} & \ifdefined\loesung \textcolor{red}{none} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
3 & {\tt INSERT INTO T1 VALUES (1, 1, 50)} & \ifdefined\loesung \textcolor{red}{T1\_PS} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
4 & {\tt INSERT INTO T1 VALUES (1, NULL, NULL)} & \ifdefined\loesung \textcolor{red}{T1\_PS} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
5 & {\tt INSERT INTO T2 VALUES (117, 2, 2, 5, 5)} & \ifdefined\loesung \textcolor{red}{T2\_E\_13} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
6 & {\tt INSERT INTO T2 VALUES (109, 3, 1, 10, 3)} & \ifdefined\loesung \textcolor{red}{T2\_FS1} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
7 & {\tt INSERT INTO T2 VALUES (103, 1, 1, 20, 2)} & \ifdefined\loesung \textcolor{red}{T2\_PS} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
8 & {\tt INSERT INTO T2 VALUES (110, 4, 4, 35, 1)} & \ifdefined\loesung \textcolor{red}{T2\_FS2} \fi \\[0.3cm] |
||||
|
\hline |
||||
|
\end{tabular} |
||||
|
|
||||
|
\pagebreak |
||||
|
\textsc{Hochschule Fulda}{\small\hfill GSD Database \& SQL questionnaire}\\ |
||||
|
{\small Fachbereich Angewandte Informatik{\small\hfill SS 18}\\ |
||||
|
Dipl.-Inf. Christian Pape}\\ |
||||
|
|
||||
|
%------------------------------------------------------------------------------ |
||||
|
\textbf{Task 3 (relations \& DDL-statements):}\\ |
||||
|
%------------------------------------------------------------------------------ |
||||
|
The following tables and DDL-statements are given. Please note: an exam with a grade other than 5.0 is passed. |
||||
|
\begin{figure}[htb] |
||||
|
\begin{minipage}{0.30\linewidth} |
||||
|
\includegraphics[width=38mm]{images/task3-chen.pdf} |
||||
|
\end{minipage} |
||||
|
%\hfill |
||||
|
\begin{minipage}{0.55\linewidth} |
||||
|
\scriptsize\lstinputlisting[numbers=none]{queries/task3-ddl.sql} |
||||
|
\end{minipage} |
||||
|
\end{figure} |
||||
|
|
||||
|
|
||||
|
|
||||
|
\begin{description} |
||||
|
\item[a.)] What is defined in the table {\tt STUDENT}? |
||||
|
\ifdefined\loesung \textcolor{red}{ |
||||
|
\begin{itemize} |
||||
|
\item the student's Id is the primary key |
||||
|
\item a student's Id have to be between 100000 and 999999 |
||||
|
\item the GENDER column is restricted to 'm' for male and 'f' for female |
||||
|
\end{itemize}}\else \vfill \fi |
||||
|
\item[b.)] What is defined in the table {\tt EXAM}? |
||||
|
\ifdefined\loesung \textcolor{red}{ |
||||
|
\begin{itemize} |
||||
|
\item the primary key is composed of the student's Id, the attempt and the course name |
||||
|
\item an exam can be taken three times |
||||
|
\item the possible grades for an exam are 1.0, 1.3, 1.7, 2.0, 2.3, 2.7, 3.0, 3.3, 3.7, 4.0, 5.0 |
||||
|
\item the referenced student Id must exist in the {\tt STUDENT} table ({\tt FOREIGN KEY}) |
||||
|
\item the deletion of a student will also delete all associated exams ({\tt ON DELETE CASCADE}) |
||||
|
\end{itemize}}\else \vfill \fi |
||||
|
|
||||
|
\item[c.)] Define a SQL query to find students (student's Id, firstname, lastname and course) who failed in the third attempt? |
||||
|
\begin{quote} |
||||
|
\ifdefined\loesung \textcolor{red}{\tt SELECT S.STUDENT\_ID, S.LASTNAME, E.COURSE FROM STUDENT S, EXAM E \\ WHERE S.STUDENT\_ID=E.STUDENT\_ID AND ATTEMPT=3 AND GRADE=5}\else \vfill \fi |
||||
|
\end{quote} |
||||
|
|
||||
|
\item[d.)] Define a SQL query to report the average attempts for male and female students? |
||||
|
\begin{quote} |
||||
|
\ifdefined\loesung \textcolor{red}{\tt SELECT S.GENDER, AVG(ATTEMPT) FROM STUDENT S, EXAM E \\ WHERE S.STUDENT\_ID=E.STUDENT\_ID AND NOT GRADE=5 GROUP BY S.GENDER}\else \vfill \fi |
||||
|
\end{quote} |
||||
|
\end{description} |
||||
|
%------------------------------------------------------------------------------ |
||||
|
\end{document} |
||||
|
%------------------------------------------------------------------------------ |
@ -0,0 +1,22 @@ |
|||||
|
SET FEEDBACK OFF |
||||
|
DROP TABLE T; |
||||
|
|
||||
|
CREATE TABLE T (A INT, B VARCHAR(16), C INT); |
||||
|
|
||||
|
INSERT INTO T VALUES (1, 'blue', 10); |
||||
|
INSERT INTO T VALUES (2, 'blue', 40); |
||||
|
INSERT INTO T VALUES (3, 'pink', 30); |
||||
|
INSERT INTO T VALUES (4, 'orange', 10); |
||||
|
INSERT INTO T VALUES (5, 'orange', 20); |
||||
|
INSERT INTO T VALUES (6, 'orange', 50); |
||||
|
INSERT INTO T VALUES (7, 'orange', 50); |
||||
|
INSERT INTO T VALUES (8, 'black', 50); |
||||
|
INSERT INTO T VALUES (9, 'black', 40); |
||||
|
INSERT INTO T VALUES (10, 'violet', 10); |
||||
|
INSERT INTO T VALUES (11, 'violet', 20); |
||||
|
INSERT INTO T VALUES (12, 'violet', 10); |
||||
|
|
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
SELECT B, COUNT(*) FROM T GROUP BY B HAVING SUM(C)<=40; |
||||
|
SELECT B, COUNT(*) FROM T WHERE C>35 GROUP BY B HAVING COUNT(*)>=2; |
@ -0,0 +1,142 @@ |
|||||
|
SET FEEDBACK OFF |
||||
|
DROP TABLE T1 CASCADE CONSTRAINTS; |
||||
|
DROP TABLE T2 CASCADE CONSTRAINTS; |
||||
|
CREATE TABLE T1 (A INT, B INT, C INT, CONSTRAINT T1_PS PRIMARY KEY (A,B), CONSTRAINT T1_SK UNIQUE (C)); |
||||
|
CREATE TABLE T2 (A INT, B INT, C INT, D INT, E INT, CONSTRAINT T2_PS PRIMARY KEY (A), CONSTRAINT T2_FS1 FOREIGN KEY (B,C) REFERENCES T1(A,B), CONSTRAINT T2_FS2 FOREIGN KEY (D) REFERENCES T1(C), CONSTRAINT T2_E_NN CHECK (E IS NOT NULL), CONSTRAINT T2_E_13 CHECK (E BETWEEN 1 AND 3)); |
||||
|
SET FEEDBACK OFF |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T1 VALUES (1, 2, 5); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T1 VALUES (1, 2, 30); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T1 VALUES (1, 1, 50); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T1 VALUES (1, NULL, NULL); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T2 VALUES (117, 2, 2, 5, 5); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T2 VALUES (109, 3, 1, 10, 3); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T2 VALUES (103, 1, 1, 20, 2); |
||||
|
|
||||
|
SET FEEDBACK OFF |
||||
|
DELETE FROM T2; |
||||
|
DELETE FROM T1; |
||||
|
INSERT INTO T1 VALUES (1, 1, 5); |
||||
|
INSERT INTO T1 VALUES (2, 2, 10); |
||||
|
INSERT INTO T1 VALUES (3, 3, 15); |
||||
|
INSERT INTO T1 VALUES (4, 4, 20); |
||||
|
INSERT INTO T1 VALUES (5, 5, 25); |
||||
|
INSERT INTO T2 VALUES (100, NULL, NULL, 15, 1); |
||||
|
INSERT INTO T2 VALUES (101, 2, 2, 25, 2); |
||||
|
INSERT INTO T2 VALUES (102, 2, 2, NULL, 3); |
||||
|
INSERT INTO T2 VALUES (103, 3, 3, 10, 3); |
||||
|
INSERT INTO T2 VALUES (104, 3, 3, 10, 3); |
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
INSERT INTO T2 VALUES (110, 4, 4, 35, 1); |
@ -0,0 +1,81 @@ |
|||||
|
SET FEEDBACK OFF |
||||
|
|
||||
|
DROP TABLE STUDENT CASCADE CONSTRAINTS; |
||||
|
DROP TABLE EXAM CASCADE CONSTRAINTS; |
||||
|
|
||||
|
CREATE TABLE STUDENT ( |
||||
|
STUDENT_ID INT PRIMARY KEY, |
||||
|
LASTNAME VARCHAR(40) NOT NULL, |
||||
|
FIRSTNAME VARCHAR(40) NOT NULL, |
||||
|
GENDER VARCHAR(1) NOT NULL, |
||||
|
CONSTRAINT CHK_STUDENT_ID |
||||
|
CHECK (STUDENT_ID>=100000 AND STUDENT_ID<=999999), |
||||
|
CONSTRAINT CHK_GENDER |
||||
|
CHECK (GENDER='m' OR GENDER='f') |
||||
|
); |
||||
|
|
||||
|
CREATE TABLE EXAM ( |
||||
|
STUDENT_ID INT, |
||||
|
COURSE VARCHAR(40), |
||||
|
GRADE DECIMAL(2,1), |
||||
|
ATTEMPT INT, |
||||
|
CONSTRAINT PK PRIMARY KEY(STUDENT_ID, ATTEMPT, COURSE), |
||||
|
CONSTRAINT CHK_ATTEMPT CHECK (ATTEMPT IN (1,2,3)), |
||||
|
CONSTRAINT CHK_GRADE CHECK ( |
||||
|
GRADE IN (1.0,1.3,1.7,2.0,2.3,2.7,3.0,3.3,3.7,4.0,5.0) |
||||
|
), |
||||
|
CONSTRAINT FK_STUDENT_ID FOREIGN KEY (STUDENT_ID) |
||||
|
REFERENCES STUDENT(STUDENT_ID) ON DELETE CASCADE |
||||
|
); |
||||
|
|
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100000, 'Joffrey', 'Baratheon', 'm'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100001, 'Jorah', 'Mormont', 'm'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100002, 'Daenerys', 'Targaryen', 'f'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100003, 'Theon', 'Graufreud', 'm'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100004, 'Tyrion', 'Lennister', 'm'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100005, 'Jon', 'Snow', 'm'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100006, 'Arya', 'Stark', 'f'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100007, 'Jaime', 'Lennister', 'm'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100008, 'Brienne', 'of Tarth', 'f'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100009, 'Sansa', 'Stark', 'f'); |
||||
|
INSERT INTO STUDENT (STUDENT_ID, FIRSTNAME, LASTNAME, GENDER) VALUES (100010, 'Petyr', 'Baelish', 'm'); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100000, 'Database Management Systems', 1, 5.0); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100001, 'Database Management Systems', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100001, 'Database Management Systems', 2, 3.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100001, 'Programming I', 1, 2.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100002, 'Database Management Systems', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100002, 'Database Management Systems', 2, 1.7); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100002, 'Programming I', 1, 4); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100003, 'Database Management Systems', 1, 2.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100003, 'Programming I', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100003, 'Programming I', 2, 3.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100004, 'Database Management Systems', 1, 1.0); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100004, 'Programming I', 1, 1.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100005, 'Database Management Systems', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100005, 'Database Management Systems', 2, 1.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100005, 'Programming I', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100005, 'Programming I', 2, 2); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100006, 'Database Management Systems', 1, 1); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100006, 'Programming I', 1, 1); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100007, 'Database Management Systems', 1, 2.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100007, 'Programming I', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100007, 'Programming I', 2, 1.7); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100008, 'Database Management Systems', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100008, 'Database Management Systems', 2, 1.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100008, 'Programming I', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100008, 'Programming I', 2, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100008, 'Programming I', 3, 3.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100009, 'Database Management Systems', 1, 2.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100009, 'Programming I', 1, 1.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100010, 'Database Management Systems', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100010, 'Database Management Systems', 2, 2.3); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100010, 'Programming I', 1, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100010, 'Programming I', 2, 5); |
||||
|
INSERT INTO EXAM (STUDENT_ID, COURSE, ATTEMPT, GRADE) VALUES (100010, 'Programming I', 3, 5); |
||||
|
|
||||
|
SET FEEDBACK ON |
||||
|
|
||||
|
-- c.) |
||||
|
SELECT S.STUDENT_ID, S.LASTNAME, E.COURSE FROM STUDENT S, EXAM E WHERE S.STUDENT_ID=E.STUDENT_ID AND ATTEMPT=3 AND GRADE=5; |
||||
|
-- d.) |
||||
|
SELECT S.GENDER, AVG(ATTEMPT) FROM STUDENT S, EXAM E WHERE S.STUDENT_ID=E.STUDENT_ID AND NOT GRADE=5 GROUP BY S.GENDER; |
@ -0,0 +1,23 @@ |
|||||
|
CREATE TABLE STUDENT ( |
||||
|
STUDENT_ID INT PRIMARY KEY, |
||||
|
LASTNAME VARCHAR(40) NOT NULL, |
||||
|
FIRSTNAME VARCHAR(40) NOT NULL, |
||||
|
GENDER VARCHAR(1) NOT NULL, |
||||
|
CONSTRAINT CHK_STUDENT_ID |
||||
|
CHECK (STUDENT_ID>=100000 AND STUDENT_ID<=999999), |
||||
|
CONSTRAINT CHK_GENDER |
||||
|
CHECK (GENDER='m' OR GENDER='f') |
||||
|
); |
||||
|
CREATE TABLE EXAM ( |
||||
|
STUDENT_ID INT, |
||||
|
COURSE VARCHAR(40), |
||||
|
GRADE DECIMAL(2,1), |
||||
|
ATTEMPT INT, |
||||
|
CONSTRAINT PK PRIMARY KEY(STUDENT_ID, ATTEMPT, COURSE), |
||||
|
CONSTRAINT CHK_ATTEMPT CHECK (ATTEMPT IN (1,2,3)), |
||||
|
CONSTRAINT CHK_GRADE CHECK ( |
||||
|
GRADE IN (1.0,1.3,1.7,2.0,2.3,2.7,3.0,3.3,3.7,4.0,5.0) |
||||
|
), |
||||
|
CONSTRAINT FK_STUDENT_ID FOREIGN KEY (STUDENT_ID) |
||||
|
REFERENCES STUDENT(STUDENT_ID) ON DELETE CASCADE |
||||
|
); |
Write
Preview
Loading…
Cancel
Save
Reference in new issue