Advertisement
vallec

Untitled

Apr 23rd, 2024
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.57 KB | None | 0 0
  1. DROP DATABASE IF EXISTS library;
  2. CREATE DATABASE library;
  3. USE library;
  4.  
  5. CREATE TABLE Authors (
  6. author_id INT AUTO_INCREMENT PRIMARY KEY,
  7. name VARCHAR(255) NOT NULL
  8. );
  9.  
  10. CREATE TABLE Users (
  11. user_id INT AUTO_INCREMENT PRIMARY KEY,
  12. username VARCHAR(255) UNIQUE NOT NULL,
  13. hashed_password VARCHAR(255) NOT NULL
  14. );
  15.  
  16. CREATE TABLE Genres (
  17. genre_id INT AUTO_INCREMENT PRIMARY KEY,
  18. genre_name VARCHAR(100) UNIQUE NOT NULL
  19. );
  20.  
  21. CREATE TABLE Publishers (
  22. publisher_id INT AUTO_INCREMENT PRIMARY KEY,
  23. publisher_name VARCHAR(255) UNIQUE NOT NULL
  24. );
  25.  
  26. CREATE TABLE Books (
  27. book_id INT AUTO_INCREMENT PRIMARY KEY,
  28. title VARCHAR(255) NOT NULL,
  29. page_count INT NOT NULL,
  30. year_published INT NOT NULL,
  31. publisher_id INT NOT NULL,
  32. genre_id INT NOT NULL,
  33. issue_number VARCHAR(50) DEFAULT NULL,
  34. is_available BOOLEAN NOT NULL DEFAULT TRUE,
  35. frequency_status VARCHAR(50) DEFAULT NULL,
  36. FOREIGN KEY (publisher_id) REFERENCES Publishers(publisher_id),
  37. FOREIGN KEY (genre_id) REFERENCES Genres(genre_id)
  38. );
  39.  
  40. CREATE TABLE Ratings (
  41. rating_id INT AUTO_INCREMENT PRIMARY KEY,
  42. book_id INT NOT NULL,
  43. user_id INT NOT NULL,
  44. rating INT NOT NULL,
  45. FOREIGN KEY (book_id) REFERENCES Books(book_id),
  46. FOREIGN KEY (user_id) REFERENCES Users(user_id)
  47. );
  48.  
  49. CREATE TABLE UsersReads (
  50. read_id INT AUTO_INCREMENT PRIMARY KEY,
  51. user_id INT NOT NULL,
  52. book_id INT NOT NULL,
  53. progress INT NOT NULL,
  54. FOREIGN KEY (user_id) REFERENCES Users(user_id),
  55. FOREIGN KEY (book_id) REFERENCES Books(book_id)
  56. );
  57.  
  58. CREATE TABLE Books_Authors (
  59. book_id INT NOT NULL,
  60. author_id INT NOT NULL,
  61. FOREIGN KEY (book_id) REFERENCES Books(book_id),
  62. FOREIGN KEY (author_id) REFERENCES Authors(author_id),
  63. PRIMARY KEY (book_id, author_id)
  64. );
  65.  
  66. CREATE TABLE Comments (
  67. comment_id INT AUTO_INCREMENT PRIMARY KEY,
  68. book_id INT NOT NULL,
  69. user_id INT NOT NULL,
  70. comment TEXT NOT NULL,
  71. comment_date DATETIME NOT NULL,
  72. FOREIGN KEY (book_id) REFERENCES Books(book_id),
  73. FOREIGN KEY (user_id) REFERENCES Users(user_id)
  74. );
  75.  
  76. CREATE TABLE Lends (
  77. lend_id INT AUTO_INCREMENT PRIMARY KEY,
  78. book_id INT NOT NULL,
  79. user_id INT NOT NULL,
  80. checkout_date DATE NOT NULL,
  81. due_date DATE NOT NULL,
  82. return_date DATE,
  83. status ENUM('lend', 'returned', 'overdue') NOT NULL,
  84. FOREIGN KEY (book_id) REFERENCES Books(book_id),
  85. FOREIGN KEY (user_id) REFERENCES Users(user_id)
  86. );
  87.  
  88. /* Add data */
  89.  
  90. /* Hash password before add user */
  91. DELIMITER $$
  92.  
  93. CREATE TRIGGER HashPasswordBeforeInsert BEFORE INSERT ON Users
  94. FOR EACH ROW
  95. BEGIN
  96. SET NEW.hashed_password = SHA2(NEW.hashed_password, 256);
  97. END$$
  98.  
  99. DELIMITER ;
  100.  
  101. INSERT INTO Authors (name) VALUES
  102. ('J.K. Rowling'),
  103. ('George R.R. Martin'),
  104. ('Agatha Christie');
  105.  
  106. INSERT INTO Users (username, hashed_password) VALUES
  107. ('user1', 'pass1'),
  108. ('user2', 'pass2'),
  109. ('user3', 'pass3');
  110.  
  111. INSERT INTO Genres (genre_name) VALUES
  112. ('Fantasy'),
  113. ('Mystery'),
  114. ('Science Fiction'),
  115. ('Fiction'),
  116. ('Thriller'),
  117. ('Crime'),
  118. ('Art'),
  119. ('Novel');
  120.  
  121. INSERT INTO Publishers (publisher_name) VALUES
  122. ('Bloomsbury'),
  123. ('HarperCollins'),
  124. ('Penguin Random House'),
  125. ('Helikon'),
  126. ('Hermes');
  127.  
  128. INSERT INTO Books (title, page_count, year_published, publisher_id, genre_id, issue_number) VALUES
  129. ('The Girl with the Dragon Tattoo', 309, 2005, 1, 1, null),
  130. ('A Game of Thrones', 694, 1996, 2, 1, null),
  131. ('Murder on the Orient Express', 256, 1934, 3, 2, null),
  132. ('Science Today', 120, 2023, 3, 3, 'Part 3'),
  133. ('And Then There Were None', 500, 2017, 4, 6, null);
  134.  
  135. INSERT INTO Ratings (book_id, user_id, rating) VALUES
  136. (1, 1, 5),
  137. (1, 1, 6),
  138. (2, 1, 4),
  139. (3, 2, 5);
  140.  
  141. INSERT INTO UsersReads (user_id, book_id, progress) VALUES
  142. (1, 1, 100),
  143. (2, 2, 150),
  144. (3, 3, 50);
  145.  
  146. INSERT INTO Books_Authors (book_id, author_id) VALUES
  147. (1, 1),
  148. (2, 2),
  149. (3, 3);
  150.  
  151. INSERT INTO Comments (book_id, user_id, comment, comment_date) VALUES
  152. (1, 1, 'Много интересна книга!', '2024-01-01 12:00:00'),
  153. (2, 2, 'Интригуваща инстория. Препоръчвам!', '2024-01-02 13:30:00'),
  154. (3, 3, 'Класическа мистерия.', '2024-01-03 15:45:00');
  155.  
  156. /* 2 */
  157. SELECT *
  158. FROM Books
  159. WHERE year_published > 2000;
  160.  
  161. /* 3 */
  162. SELECT book_id, AVG(rating) AS average_rating
  163. FROM Ratings
  164. GROUP BY book_id;
  165.  
  166. /* 4 */
  167. SELECT Books.title, Authors.name
  168. FROM Books
  169. INNER JOIN Books_Authors ON Books.book_id = Books_Authors.book_id
  170. INNER JOIN Authors ON Books_Authors.author_id = Authors.author_id;
  171.  
  172. /* 5 */
  173. SELECT Books.title, AVG(Ratings.rating) AS average_rating
  174. FROM Books
  175. LEFT OUTER JOIN Ratings ON Books.book_id = Ratings.book_id
  176. GROUP BY Books.title;
  177.  
  178. /* 6 */
  179. SELECT book_id, AVG(rating) AS average_rating
  180. FROM Ratings
  181. GROUP BY book_id
  182. HAVING AVG(rating) > (
  183. SELECT AVG(rating) FROM Ratings
  184. )
  185. ORDER BY average_rating DESC;
  186.  
  187. /* 7 */
  188. SELECT Books.title, Publishers.publisher_name, AVG(Ratings.rating) AS average_rating
  189. FROM Books
  190. JOIN Publishers ON Books.publisher_id = Publishers.publisher_id
  191. LEFT JOIN Ratings ON Books.book_id = Ratings.book_id
  192. GROUP BY Books.title, Publishers.publisher_name
  193. ORDER BY average_rating DESC;
  194.  
  195. /* 8 */
  196. DELIMITER $$
  197.  
  198. CREATE TRIGGER UpdateBookStatusOut BEFORE INSERT ON Lends
  199. FOR EACH ROW
  200. BEGIN
  201. IF NEW.return_date IS NULL THEN
  202. UPDATE Books SET is_available = FALSE WHERE book_id = NEW.book_id;
  203. END IF;
  204. END$$
  205.  
  206. DELIMITER ;
  207.  
  208. DELIMITER $$
  209.  
  210. CREATE TRIGGER UpdateBookStatusReturn BEFORE UPDATE ON Lends
  211. FOR EACH ROW
  212. BEGIN
  213. IF OLD.status = 'lend' AND NEW.status = 'returned' THEN
  214. UPDATE Books SET is_available = TRUE WHERE book_id = NEW.book_id;
  215. END IF;
  216. END$$
  217.  
  218. DELIMITER ;
  219.  
  220. DELIMITER $$
  221.  
  222. CREATE PROCEDURE LendBook(IN bookID INT, IN userID INT, IN dueDate DATE)
  223. BEGIN
  224. DECLARE available BOOLEAN;
  225. SELECT is_available INTO available FROM Books WHERE book_id = bookID;
  226. IF available THEN
  227. INSERT INTO Lends (book_id, user_id, checkout_date, due_date, status)
  228. VALUES (bookID, userID, CURDATE(), dueDate, 'lend');
  229. ELSE
  230. SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'This book is currently unavailable.';
  231. END IF;
  232. END$$
  233.  
  234. DELIMITER ;
  235.  
  236. CALL LendBook(1, 1, '2024-05-01');
  237.  
  238. DELIMITER $$
  239.  
  240. CREATE PROCEDURE ReturnBook(IN lendID INT)
  241. BEGIN
  242. UPDATE Lends
  243. SET return_date = CURDATE(), status = 'returned'
  244. WHERE lend_id = lendID;
  245. END$$
  246.  
  247. DELIMITER ;
  248.  
  249. CALL ReturnBook(1);
  250.  
  251. /* 9 */
  252. DELIMITER $$
  253.  
  254. CREATE PROCEDURE UpdateBookFrequency()
  255. BEGIN
  256. DECLARE v_book_id INT;
  257. DECLARE v_count INT;
  258. DECLARE finished INT DEFAULT 0;
  259.  
  260. DECLARE book_cursor CURSOR FOR
  261. SELECT book_id, COUNT(*) FROM Lends
  262. WHERE checkout_date BETWEEN DATE_SUB(CURDATE(), INTERVAL 1 YEAR) AND CURDATE()
  263. GROUP BY book_id;
  264.  
  265. DECLARE CONTINUE HANDLER FOR NOT FOUND SET finished = 1;
  266.  
  267. OPEN book_cursor;
  268.  
  269. fetch_loop: LOOP
  270. FETCH book_cursor INTO v_book_id, v_count;
  271. IF finished = 1 THEN
  272. LEAVE fetch_loop;
  273. END IF;
  274.  
  275. /* Update status of the book */
  276. IF v_count > 10 THEN
  277. UPDATE Books SET frequency_status = 'често изнасяни' WHERE book_id = v_book_id;
  278. ELSEIF v_count > 5 THEN
  279. UPDATE Books SET frequency_status = 'редовно изнасяни' WHERE book_id = v_book_id;
  280. ELSE
  281. UPDATE Books SET frequency_status = 'рядко изнасяни' WHERE book_id = v_book_id;
  282. END IF;
  283.  
  284. END LOOP;
  285.  
  286. CLOSE book_cursor;
  287. END$$
  288.  
  289. DELIMITER ;
  290.  
  291. CALL UpdateBookFrequency();
  292.  
  293.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement