You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

286 lines
9.6 KiB

  1. % Created 2023-02-28 Tue 23:48
  2. % Intended LaTeX compiler: pdflatex
  3. \documentclass[11pt]{article}
  4. \usepackage[utf8]{inputenc}
  5. \usepackage[T1]{fontenc}
  6. \usepackage{graphicx}
  7. \usepackage{longtable}
  8. \usepackage{wrapfig}
  9. \usepackage{rotating}
  10. \usepackage[normalem]{ulem}
  11. \usepackage{amsmath}
  12. \usepackage{amssymb}
  13. \usepackage{capt-of}
  14. \usepackage{hyperref}
  15. \author{Amirlan Sharipov (BS-CS21-01)}
  16. \date{\today}
  17. \title{Lab 6 Amirlan Sharipov (BS-CS21-01)}
  18. \hypersetup{
  19. pdfauthor={Amirlan Sharipov (BS-CS21-01)},
  20. pdftitle={Lab 6 Amirlan Sharipov (BS-CS21-01)},
  21. pdfkeywords={},
  22. pdfsubject={},
  23. pdfcreator={Emacs 28.2 (Org mode 9.6)},
  24. pdflang={English}}
  25. \begin{document}
  26. \maketitle
  27. \tableofcontents
  28. \section{Disclaimer}
  29. \label{sec:orgd9c7526}
  30. Please, use the lab6.sql file to read/copy the source code. Also, the html version of this document looks better than the pdf one.
  31. \section{Exercise 1}
  32. \label{sec:orgbb19d8e}
  33. \subsection{Table creation and insertion}
  34. \label{sec:orge2ed6dc}
  35. I used the schema provided in the slides. And then manually inserted data into the tables.
  36. \begin{verbatim}
  37. CREATE TABLE customers (
  38. customerId INT,
  39. customerName VARCHAR(50),
  40. city VARCHAR(50),
  41. PRIMARY KEY (customerId)
  42. );
  43. CREATE TABLE items (
  44. itemId INT,
  45. itemName VARCHAR(50),
  46. price FLOAT,
  47. PRIMARY KEY (itemId)
  48. );
  49. CREATE TABLE orders (
  50. orderId INT,
  51. customerId INT,
  52. date DATE,
  53. FOREIGN KEY (customerId) REFERENCES customers(customerId),
  54. PRIMARY KEY (orderId)
  55. );
  56. CREATE TABLE order_details (
  57. orderId INT,
  58. itemId INT,
  59. quantity INT,
  60. FOREIGN KEY (itemId) REFERENCES items(itemId),
  61. PRIMARY KEY (orderId, itemId)
  62. );
  63. INSERT INTO customers VALUES('101', 'Martin', 'Prague');
  64. INSERT INTO customers VALUES('107', 'Herman', 'Madrid');
  65. INSERT INTO customers VALUES('110', 'Pedro', 'Moscow');
  66. INSERT INTO items VALUES('3786', 'Net', 35.0);
  67. INSERT INTO items VALUES('4011', 'Racket', 65.0);
  68. INSERT INTO items VALUES('9132', 'Pack-3', 4.75);
  69. INSERT INTO items VALUES('5794', 'Pack-6', 5.0);
  70. INSERT INTO items VALUES('3141', 'Cover', 10.0);
  71. INSERT INTO orders VALUES('2301', '101', '2011-02-23');
  72. INSERT INTO orders VALUES('2302', '107', '2011-02-25');
  73. INSERT INTO orders VALUES('2303', '110', '2011-02-27');
  74. INSERT INTO order_details VALUES ('2301', '3786', 3);
  75. INSERT INTO order_details VALUES ('2301', '4011', 6);
  76. INSERT INTO order_details VALUES ('2301', '9132', 8);
  77. INSERT INTO order_details VALUES ('2302', '5794', 4);
  78. INSERT INTO order_details VALUES ('2303', '4011', 2);
  79. INSERT INTO order_details VALUES ('2303', '3141', 2);
  80. \end{verbatim}
  81. \subsection{Queries}
  82. \label{sec:org4f468b3}
  83. First query takes the sum of all prices * quantities groupped by the orders and sorts them.
  84. The second query does the same thing, groups by customers, sorts by sum of quantities (descending order) and takes the first result.
  85. \begin{verbatim}
  86. SELECT order_details.orderId, SUM(items.price * order_details.quantity)
  87. FROM order_details
  88. INNER JOIN items on items.itemId=order_details.itemId
  89. group by order_details.orderId ORDER BY sum ASC;
  90. SELECT customers.customerName, customers.city FROM customers
  91. INNER JOIN orders ON customers.customerId=orders.customerId
  92. INNER JOIN order_details ON order_details.orderId=orders.orderId
  93. GROUP BY customers.customerId
  94. ORDER BY SUM(order_details.quantity) DESC
  95. LIMIT 1;
  96. \end{verbatim}
  97. \section{Exercise 2}
  98. \label{sec:org99a84d3}
  99. \subsection{Normalization}
  100. \label{sec:org77cd4b4}
  101. I have several assumptions:
  102. \begin{itemize}
  103. \item Any teacher can work at several schools at once (or change the school)
  104. \item Room numbers don't depend on schools: the first number of the room name is not enough to assume the opposite
  105. \item Teachers may teach several courses: it's an elementary school, usually teachers can teach anything in elementary schools
  106. \end{itemize}
  107. \subsubsection{1NF}
  108. \label{sec:org4242f0d}
  109. It's almost in 1NF state. Each cell is already atomic, values of the same domain, etc. The only thing that's not there is the primary key. Let's say for now that the primary key is a tuple of school, teacher, course, room, grade, and book. This way there are no conflicts. It still looks like a mess, so I will normalize it further.
  110. \subsubsection{2NF}
  111. \label{sec:orge350be8}
  112. Make new tables with relations for partial functional dependencies of non-prime attributes on candidate keys:
  113. \begin{itemize}
  114. \item lessons (lessonId, schoolName, teacherName, courseName, roomName, gradeName)
  115. \item books (bookId, bookName, publisherName)
  116. \item loans (loanId, lessonId, bookId, loanDate)
  117. \end{itemize}
  118. \subsubsection{3NF}
  119. \label{sec:org627722d}
  120. Make new tables (with appropriate IDs) with relations for transitive functional dependencies of non-prime attribute on candidate key:
  121. \begin{itemize}
  122. \item schools (schoolId, schoolName)
  123. \item teachers (teacherId, teacherName)
  124. \item courses (courseId, courseName)
  125. \item rooms (roomId, roomName)
  126. \item grades (gradeId, gradeName)
  127. \item publishers (publisherId, publisherName)
  128. \item lessons (lessonId, schoolId, teacherId, courseId, roomId, gradeId)
  129. \item books (bookId, bookName, publisherId)
  130. \item loans (loanId, lessonId, bookId, loanDate)
  131. \end{itemize}
  132. \subsubsection{BCNF and 4NF}
  133. \label{sec:org00e5b2a}
  134. Already satisfies.
  135. \subsubsection{Code}
  136. \label{sec:org3de05cf}
  137. \begin{verbatim}
  138. CREATE TABLE schools (
  139. schoolId SERIAL,
  140. schoolName VARCHAR(50),
  141. PRIMARY KEY (schoolId)
  142. );
  143. CREATE TABLE teachers (
  144. teacherId SERIAL,
  145. teacherName VARCHAR(30),
  146. PRIMARY KEY (teacherId)
  147. );
  148. CREATE TABLE courses (
  149. courseId SERIAL,
  150. courseName VARCHAR(40),
  151. PRIMARY KEY (courseId)
  152. );
  153. CREATE TABLE rooms (
  154. roomId SERIAL,
  155. roomName VARCHAR(40),
  156. PRIMARY KEY (roomId)
  157. );
  158. CREATE TABLE grades (
  159. gradeId SERIAL,
  160. gradeName VARCHAR(15),
  161. PRIMARY KEY (gradeId)
  162. );
  163. CREATE TABLE publishers (
  164. publisherId SERIAL,
  165. publisherName VARCHAR(30),
  166. PRIMARY KEY (publisherId)
  167. );
  168. CREATE TABLE books (
  169. bookId SERIAL,
  170. bookName VARCHAR(60),
  171. publisherId INT,
  172. FOREIGN KEY (publisherId) REFERENCES publishers(publisherId),
  173. PRIMARY KEY (bookId)
  174. );
  175. CREATE TABLE lessons (
  176. lessonId SERIAL,
  177. schoolId INT,
  178. teacherId INT,
  179. courseId INT,
  180. roomId INT,
  181. gradeId INT,
  182. FOREIGN KEY (teacherId) REFERENCES teachers(teacherId),
  183. FOREIGN KEY (courseId) REFERENCES courses(courseId),
  184. FOREIGN KEY (roomId) REFERENCES rooms(roomId),
  185. FOREIGN KEY (gradeId) REFERENCES grades(gradeId),
  186. PRIMARY KEY (lessonId)
  187. );
  188. CREATE TABLE loans (
  189. loanId SERIAL,
  190. lessonId INT,
  191. bookId INT,
  192. loanDate DATE,
  193. FOREIGN KEY (lessonId) REFERENCES lessons(lessonId),
  194. FOREIGN KEY (bookId) REFERENCES books(bookId),
  195. PRIMARY KEY (loanId)
  196. );
  197. INSERT INTO schools (schoolName)
  198. SELECT DISTINCT school FROM loan_books;
  199. INSERT INTO teachers (teacherName)
  200. SELECT DISTINCT teacher FROM loan_books;
  201. -- Inserted Numerical thinking 2 times because of case sensitivity. Not gonna change anything.
  202. INSERT INTO courses (courseName)
  203. SELECT DISTINCT course FROM loan_books;
  204. INSERT INTO rooms (roomName)
  205. SELECT DISTINCT room FROM loan_books;
  206. INSERT INTO grades (gradeName)
  207. SELECT DISTINCT grade FROM loan_books;
  208. INSERT INTO publishers (publisherName)
  209. SELECT DISTINCT publisher FROM loan_books;
  210. INSERT INTO books (bookName, publisherId)
  211. SELECT DISTINCT loan_books.book,
  212. publishers.publisherId FROM loan_books
  213. INNER JOIN publishers ON
  214. publishers.publisherName=loan_books.publisher;
  215. INSERT INTO lessons (schoolId, teacherId, courseId, roomId, gradeId)
  216. SELECT DISTINCT schools.schoolId, teachers.teacherId,
  217. courses.courseId, rooms.roomId, grades.gradeId FROM loan_books
  218. INNER JOIN schools ON schools.schoolName=loan_books.school
  219. INNER JOIN teachers ON teachers.teacherName=loan_books.teacher
  220. INNER JOIN courses ON courses.courseName=loan_books.course
  221. INNER JOIN rooms ON rooms.roomName=loan_books.room
  222. INNER JOIN grades ON grades.gradeName=loan_books.grade;
  223. INSERT INTO loans (lessonId, bookId, loanDate)
  224. SELECT DISTINCT lessons.lessonId, books.bookId,
  225. loan_books.loanDate FROM loan_books
  226. INNER JOIN schools ON schools.schoolName=loan_books.school
  227. INNER JOIN teachers ON teachers.teacherName=loan_books.teacher
  228. INNER JOIN courses ON courses.courseName=loan_books.course
  229. INNER JOIN rooms ON rooms.roomName=loan_books.room
  230. INNER JOIN grades ON grades.gradeName=loan_books.grade
  231. INNER JOIN books ON books.bookName=loan_books.book
  232. INNER JOIN lessons ON lessons.gradeId=grades.gradeId
  233. and lessons.roomId=rooms.roomId
  234. and lessons.courseId=courses.courseId
  235. and lessons.teacherId=teachers.teacherId;
  236. \end{verbatim}
  237. \subsection{Queries}
  238. \label{sec:org917674e}
  239. The first query list all the schools that borrowed the books of every publisher using DISTINCT keyword.
  240. The second query orders the results from each school, and takes only 1 loan that has the highest loanDate for each school.
  241. \begin{verbatim}
  242. SELECT DISTINCT publishers.publisherName, books.bookName,
  243. schools.schoolName FROM books
  244. INNER JOIN loans ON loans.bookId=books.bookId
  245. INNER JOIN lessons ON lessons.lessonId=loans.lessonId
  246. INNER JOIN schools ON schools.schoolId=lessons.schoolId
  247. INNER JOIN publishers ON publishers.publisherId=books.publisherId
  248. ORDER BY publishers.publisherName;
  249. SELECT DISTINCT ON (schools.schoolName) schools.schoolName,
  250. publishers.publisherName, books.bookName FROM loans
  251. INNER JOIN lessons ON lessons.lessonId=loans.lessonId
  252. INNER JOIN schools ON schools.schoolId=lessons.schoolId
  253. INNER JOIN books ON books.bookId=loans.bookId
  254. INNER JOIN publishers ON publishers.publisherId=books.publisherId
  255. ORDER BY schools.schoolName, loans.loanDate DESC, 1;
  256. \end{verbatim}
  257. \end{document}