From 1b1fc65efffb1a08a6adaff3765f71dd919857e7 Mon Sep 17 00:00:00 2001 From: Christian Pape Date: Mon, 4 Jun 2018 10:06:02 +0200 Subject: [PATCH] first commit --- .gitignore | 12 + Makefile | 11 + gsd-questions.tex | 229 ++++++++++++++++++++ images/task3-chen.graffle | Bin 0 -> 7237 bytes images/task3-chen.pdf | Bin 0 -> 12172 bytes queries/task1-sql-statements.sql | 22 ++ queries/task2-constraints-and-integrity.sql | 142 ++++++++++++ queries/task3-ddl-example.sql | 81 +++++++ queries/task3-ddl.sql | 23 ++ 9 files changed, 520 insertions(+) create mode 100755 .gitignore create mode 100644 Makefile create mode 100755 gsd-questions.tex create mode 100644 images/task3-chen.graffle create mode 100644 images/task3-chen.pdf create mode 100644 queries/task1-sql-statements.sql create mode 100644 queries/task2-constraints-and-integrity.sql create mode 100644 queries/task3-ddl-example.sql create mode 100644 queries/task3-ddl.sql diff --git a/.gitignore b/.gitignore new file mode 100755 index 0000000..9263c23 --- /dev/null +++ b/.gitignore @@ -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 diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..426caaf --- /dev/null +++ b/Makefile @@ -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 diff --git a/gsd-questions.tex b/gsd-questions.tex new file mode 100755 index 0000000..612be3f --- /dev/null +++ b/gsd-questions.tex @@ -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} +%------------------------------------------------------------------------------ diff --git a/images/task3-chen.graffle b/images/task3-chen.graffle new file mode 100644 index 0000000000000000000000000000000000000000..39aa21b76b0b20aee0798aa913b9593e0d4dd092 GIT binary patch literal 7237 zcmV-L9J=EliwFP!000030PS7rvZ6?v{@r~QXZq)y>JkuKda8ROS;YlKHZdwYk?n`>uRkd=`*Z83|NQO8FaJglDOn#eFRQ>$(wC7!_Ic{%r=M#zuv!I9twziA z%c##w@(V1ZR%3*pUVie@boEQEmSx#zBS(FIdQu0!@_=s0-x>F{s~r7 zmVTxVJZt+_`rD5`d|5dA-}35w%ShpMwS;}C<>%pLBaV%H=Z7yz8bkemgVlYGmZ9&) z(Ps5|2=}oE4Z^fl>5jS{}QF?ur$7Q!UrVD^yaZs5i~!igSKe${aC^vWzK* zZl?KO(S9<6$BXGv&m=!b@ph;N1qR8>pg4OO1a`jLF6v#F9W^!b-I{xBpJHmNXF&`J zih36|r_PPM2D+$sUyRO8kb(2#On?gls<5-_`7d#_u%?at5A#6A|4hp+tgEs>(VPSJOx4$rF2w|?L z10UaA*hwnn(88y>V`YF1k{*2wPe=M|*e>|WI|5QZSjfIz(1C0#~{-$8* zu$k^* z9AU)5n2ZbinHcUG8vaoX+_3|R0zm;-M!R6BfJO$B+DHel8AAfmB59G4!FWODa-EC` zfRvB}m%)LIlVgz`0F@^-k#&J)k*3R{V3Lt;#`VFJCzmeg74;blG7&ifSPZ!rJ7Zuk zNL=i!z>FtfU7iMRMzMgmfv;2jjK{%(p>jDX>nb2XsA<;SfiR*x&>Mo4OGR!k0kKYH zV!sbkg~CBU23rMY@Ik+*Z>Q6|I7q?4rNvAb0W_kguCNDnoc7(J1{ypayTb-(Su~Q3 zT-ewmj$TEPgTBDZ5!u(79gt;m$UrN|uels4B5VgJ zF)2r^B&s^8S*)FD968n5m8+qorLd?t3CNkwk*=Da(2fy$CT?KIkgCQ`Ts4Dw{ST{nHsz0m@}3I|w|d@A>U0t-+8qX+kz#i|w9}aYdpWMt9Vc^Gk0|QvEF&i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VMzJI+ARdDV;Fu_aZ$!MDw}4}%70qs()Dlg-#q4UDBNWj zI(f`I2!L^veGAV6gg;I4P>{jMetL`I0mP=`r^zh%pZ~VJONTF{mC#>roYNOK9xA4O zOKzyxvDC+Sy zIC{ZuQ@O&qLMNH&?fQ5Vyc4Qj0bnng#z&Vm-#&@x_wYa)*Nv(~{!2E@r#hkXnX%)Z zaZtT?#;2E`%U771NF#rriD=*Gh@5RkHG<8FtDX`zosK@wM9R(Q&{wBUX`}8?JXz6l z6+1~X%oFzFBq%O?%ZgU}oW{?w3+}(<0WzmVbR?*0;EZVYKG4~%A{a~%eu}wii4(lI zF#`l|F{RsellLMdF8gms6Yj{T>)e$`qY*Pp#IKTFZh(s|d5JoM{LrB4MdBeevnd(ce T_|J-d`|*DPK9skuLumj2r!YPJ literal 0 HcmV?d00001 diff --git a/images/task3-chen.pdf b/images/task3-chen.pdf new file mode 100644 index 0000000000000000000000000000000000000000..6c10a661286295e913c0435cd2eaab573d6ec502 GIT binary patch literal 12172 zcmbt)by!s07cM18gVG`lAT2P&(B0kLFbv(@AuSEkB?2NKf`W86(p>`5-Q9Q4&oA$N zet+D7XXc!Duf6x#`>eB`cb#`pDvF9TFf+2EP`0lgt!@__za47tL}3E}0d~fgD7?G? zCRvEBnX@?n1cj6VOyU;S&JaiFueFggL=*zHGl2m3`B9vl9U(?GDDJQ}333s$K&+7c zD_kd8jA3aCt@xNa3r5sBHqHFt5fyV_M0r{NedvJsifB7k`-JAc_r`D^KJg+d+U>`E z7!{=ZyH~^`<7+WO&W0*`G?xr?qc~ zcO9EQRQBrSeBpN7h~d$S(A|ObJtnQOz#BBg5WeT_cwjT5P{vmrLYa*Ij7l#eBnMHe z+Z{U6ih@ImQ;Hlb#VfU>^x=ueBin)TR2nPI8_yL#C8M#Ak;Coi4$X@cfYCZNyYbz> zP19*-8nLdBE@13?pkBgbq%s-{CDO|&SNhY5uOftKB2#KBo%Yexy!udROVu)tj2zmk zFBt}+{an9E^rW2fg(U5Z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;X3 H5J&kxXON3z literal 0 HcmV?d00001 diff --git a/queries/task1-sql-statements.sql b/queries/task1-sql-statements.sql new file mode 100644 index 0000000..f97e4ef --- /dev/null +++ b/queries/task1-sql-statements.sql @@ -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; \ No newline at end of file diff --git a/queries/task2-constraints-and-integrity.sql b/queries/task2-constraints-and-integrity.sql new file mode 100644 index 0000000..50c67e0 --- /dev/null +++ b/queries/task2-constraints-and-integrity.sql @@ -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); diff --git a/queries/task3-ddl-example.sql b/queries/task3-ddl-example.sql new file mode 100644 index 0000000..b4d93f4 --- /dev/null +++ b/queries/task3-ddl-example.sql @@ -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; diff --git a/queries/task3-ddl.sql b/queries/task3-ddl.sql new file mode 100644 index 0000000..a8f0569 --- /dev/null +++ b/queries/task3-ddl.sql @@ -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 +); \ No newline at end of file