├── LibraryManagementSystemAndStoredProceduresFINAL.sql └── README.md /LibraryManagementSystemAndStoredProceduresFINAL.sql: -------------------------------------------------------------------------------- 1 | 2 | CREATE PROC dbo.LibraryManagementSystemProcedure 3 | AS 4 | CREATE DATABASE db_LibraryManagement 5 | 6 | GO 7 | /* ======================= TABLES ========================*/ 8 | 9 | 10 | CREATE TABLE tbl_publisher ( 11 | publisher_PublisherName VARCHAR(100) PRIMARY KEY NOT NULL, 12 | publisher_PublisherAddress VARCHAR(200) NOT NULL, 13 | publisher_PublisherPhone VARCHAR(50) NOT NULL, 14 | ); 15 | 16 | CREATE TABLE tbl_book ( 17 | book_BookID INT PRIMARY KEY NOT NULL IDENTITY (1,1), 18 | book_Title VARCHAR(100) NOT NULL, 19 | book_PublisherName VARCHAR(100) NOT NULL CONSTRAINT fk_publisher_name1 FOREIGN KEY REFERENCES tbl_publisher(publisher_PublisherName) ON UPDATE CASCADE ON DELETE CASCADE, 20 | ); 21 | 22 | CREATE TABLE tbl_library_branch ( 23 | library_branch_BranchID INT PRIMARY KEY NOT NULL IDENTITY (1,1), 24 | library_branch_BranchName VARCHAR(100) NOT NULL, 25 | library_branch_BranchAddress VARCHAR(200) NOT NULL, 26 | ); 27 | 28 | SELECT * FROM tbl_library_branch 29 | 30 | CREATE TABLE tbl_borrower ( 31 | borrower_CardNo INT PRIMARY KEY NOT NULL IDENTITY (100,1), 32 | borrower_BorrowerName VARCHAR(100) NOT NULL, 33 | borrower_BorrowerAddress VARCHAR(200) NOT NULL, 34 | borrower_BorrowerPhone VARCHAR(50) NOT NULL, 35 | ); 36 | 37 | SELECT * FROM tbl_borrower 38 | 39 | CREATE TABLE tbl_book_loans ( 40 | book_loans_LoansID INT PRIMARY KEY NOT NULL IDENTITY (1,1), 41 | book_loans_BookID INT NOT NULL CONSTRAINT fk_book_id1 FOREIGN KEY REFERENCES tbl_book(book_BookID) ON UPDATE CASCADE ON DELETE CASCADE, 42 | book_loans_BranchID INT NOT NULL CONSTRAINT fk_branch_id1 FOREIGN KEY REFERENCES tbl_library_branch(library_branch_BranchID) ON UPDATE CASCADE ON DELETE CASCADE, 43 | book_loans_CardNo INT NOT NULL CONSTRAINT fk_cardno FOREIGN KEY REFERENCES tbl_borrower(borrower_CardNo) ON UPDATE CASCADE ON DELETE CASCADE, 44 | book_loans_DateOut VARCHAR(50) NOT NULL, 45 | book_loans_DueDate VARCHAR(50) NOT NULL, 46 | ); 47 | 48 | SELECT * FROM tbl_book_loans 49 | /* Use GETDATE() to retrieve the date values for Date out. Use DATEADD for the DueDate*/ 50 | 51 | CREATE TABLE tbl_book_copies ( 52 | book_copies_CopiesID INT PRIMARY KEY NOT NULL IDENTITY (1,1), 53 | book_copies_BookID INT NOT NULL CONSTRAINT fk_book_id2 FOREIGN KEY REFERENCES tbl_book(book_BookID) ON UPDATE CASCADE ON DELETE CASCADE, 54 | book_copies_BranchID INT NOT NULL CONSTRAINT fk_branch_id2 FOREIGN KEY REFERENCES tbl_library_branch(library_branch_BranchID) ON UPDATE CASCADE ON DELETE CASCADE, 55 | book_copies_No_Of_Copies INT NOT NULL, 56 | ); 57 | 58 | SELECT * FROM tbl_book_copies 59 | 60 | CREATE TABLE tbl_book_authors ( 61 | book_authors_AuthorID INT PRIMARY KEY NOT NULL IDENTITY (1,1), 62 | book_authors_BookID INT NOT NULL CONSTRAINT fk_book_id3 FOREIGN KEY REFERENCES tbl_book(book_BookID) ON UPDATE CASCADE ON DELETE CASCADE, 63 | book_authors_AuthorName VARCHAR(50) NOT NULL, 64 | ); 65 | 66 | SELECT * FROM tbl_book_authors 67 | 68 | /*======================== END TABLES ======================*/ 69 | 70 | 71 | /*==================== POPULATING TABLES ======================*/ 72 | 73 | INSERT INTO tbl_publisher 74 | (publisher_PublisherName, publisher_PublisherAddress, publisher_PublisherPhone) 75 | VALUES 76 | ('DAW Books','375 Hudson Street, New York, NY 10014','212-366-2000'), 77 | ('Viking','375 Hudson Street, New York, NY 10014','212-366-2000'), 78 | ('Signet Books','375 Hudson Street, New York, NY 10014','212-366-2000'), 79 | ('Chilton Books','Not Available','Not Available'), 80 | ('George Allen & Unwin','83 Alexander Ln, Crows Nest NSW 2065, Australia','+61-2-8425-0100'), 81 | ('Alfred A. Knopf','The Knopf Doubleday Group Domestic Rights, 1745 Broadway, New York, NY 10019','212-940-7390'), 82 | ('Bloomsbury','Bloomsbury Publishing Inc., 1385 Broadway, 5th Floor, New York, NY 10018','212-419-5300'), 83 | ('Shinchosa','Oga Bldg. 8, 2-5-4 Sarugaku-cho, Chiyoda-ku, Tokyo 101-0064 Japan','+81-3-5577-6507'), 84 | ('Harper and Row','HarperCollins Publishers, 195 Broadway, New York, NY 10007','212-207-7000'), 85 | ('Pan Books','175 Fifth Avenue, New York, NY 10010','646-307-5745'), 86 | ('Chalto & Windus','375 Hudson Street, New York, NY 10014','212-366-2000'), 87 | ('Harcourt Brace Jovanovich','3 Park Ave, New York, NY 10016','212-420-5800'), 88 | ('W.W. Norton',' W. W. Norton & Company, Inc., 500 Fifth Avenue, New York, New York 10110','212-354-5500'), 89 | ('Scholastic','557 Broadway, New York, NY 10012','800-724-6527'), 90 | ('Bantam','375 Hudson Street, New York, NY 10014','212-366-2000'), 91 | ('Picador USA','175 Fifth Avenue, New York, NY 10010','646-307-5745') 92 | ; 93 | 94 | SELECT * FROM tbl_publisher 95 | 96 | INSERT INTO tbl_book 97 | (book_Title, book_PublisherName) 98 | VALUES 99 | ('The Name of the Wind', 'DAW Books'), 100 | ('It', 'Viking'), 101 | ('The Green Mile', 'Signet Books'), 102 | ('Dune', 'Chilton Books'), 103 | ('The Hobbit', 'George Allen & Unwin'), 104 | ('Eragon', 'Alfred A. Knopf'), 105 | ('A Wise Mans Fear', 'DAW Books'), 106 | ('Harry Potter and the Philosophers Stone', 'Bloomsbury'), 107 | ('Hard Boiled Wonderland and The End of the World', 'Shinchosa'), 108 | ('The Giving Tree', 'Harper and Row'), 109 | ('The Hitchhikers Guide to the Galaxy', 'Pan Books'), 110 | ('Brave New World', 'Chalto & Windus'), 111 | ('The Princess Bride', 'Harcourt Brace Jovanovich'), 112 | ('Fight Club', 'W.W. Norton'), 113 | ('Holes', 'Scholastic'), 114 | ('Harry Potter and the Chamber of Secrets', 'Bloomsbury'), 115 | ('Harry Potter and the Prisoner of Azkaban', 'Bloomsbury'), 116 | ('The Fellowship of the Ring', 'George Allen & Unwin'), 117 | ('A Game of Thrones', 'Bantam'), 118 | ('The Lost Tribe', 'Picador USA'); 119 | 120 | SELECT * FROM tbl_book WHERE book_PublisherName = 'George Allen & Unwin' 121 | 122 | INSERT INTO tbl_library_branch 123 | (library_branch_BranchName, library_branch_BranchAddress) 124 | VALUES 125 | ('Sharpstown','32 Corner Road, New York, NY 10012'), 126 | ('Central','491 3rd Street, New York, NY 10014'), 127 | ('Saline','40 State Street, Saline, MI 48176'), 128 | ('Ann Arbor','101 South University, Ann Arbor, MI 48104'); 129 | 130 | /*UPDATE tbl_library_branch 131 | SET library_branch_BranchName = 'Central' 132 | WHERE library_branch_BranchID = 2;*/ 133 | 134 | SELECT * FROM tbl_library_branch 135 | 136 | INSERT INTO tbl_borrower 137 | (borrower_BorrowerName, borrower_BorrowerAddress, borrower_BorrowerPhone) 138 | VALUES 139 | ('Joe Smith','1321 4th Street, New York, NY 10014','212-312-1234'), 140 | ('Jane Smith','1321 4th Street, New York, NY 10014','212-931-4124'), 141 | ('Tom Li','981 Main Street, Ann Arbor, MI 48104','734-902-7455'), 142 | ('Angela Thompson','2212 Green Avenue, Ann Arbor, MI 48104','313-591-2122'), 143 | ('Harry Emnace','121 Park Drive, Ann Arbor, MI 48104','412-512-5522'), 144 | ('Tom Haverford','23 75th Street, New York, NY 10014','212-631-3418'), 145 | ('Haley Jackson','231 52nd Avenue New York, NY 10014','212-419-9935'), 146 | ('Michael Horford','653 Glen Avenue, Ann Arbor, MI 48104','734-998-1513'); 147 | 148 | SELECT * FROM tbl_borrower 149 | 150 | INSERT INTO tbl_book_loans 151 | (book_loans_BookID, book_loans_BranchID, book_loans_CardNo, book_loans_DateOut, book_loans_DueDate) 152 | VALUES 153 | ('1','1','100','1/1/18','2/2/18'), 154 | ('2','1','100','1/1/18','2/2/18'), 155 | ('3','1','100','1/1/18','2/2/18'), 156 | ('4','1','100','1/1/18','2/2/18'), 157 | ('5','1','102','1/3/18','2/3/18'), 158 | ('6','1','102','1/3/18','2/3/18'), 159 | ('7','1','102','1/3/18','2/3/18'), 160 | ('8','1','102','1/3/18','2/3/18'), 161 | ('9','1','102','1/3/18','2/3/18'), 162 | ('11','1','102','1/3/18','2/3/18'), 163 | ('12','2','105','12/12/17','1/12/18'), 164 | ('10','2','105','12/12/17','1/12/17'), 165 | ('20','2','105','2/3/18','3/3/18'), 166 | ('18','2','105','1/5/18','2/5/18'), 167 | ('19','2','105','1/5/18','2/5/18'), 168 | ('19','2','100','1/3/18','2/3/18'), 169 | ('11','2','106','1/7/18','2/7/18'), 170 | ('1','2','106','1/7/18','2/7/18'), 171 | ('2','2','100','1/7/18','2/7/18'), 172 | ('3','2','100','1/7/18','2/7/18'), 173 | ('5','2','105','12/12/17','1/12/18'), 174 | ('4','3','103','1/9/18','2/9/18'), 175 | ('7','3','102','1/3/18','2/3/18'), 176 | ('17','3','102','1/3/18','2/3/18'), 177 | ('16','3','104','1/3/18','2/3/18'), 178 | ('15','3','104','1/3/18','2/3/18'), 179 | ('15','3','107','1/3/18','2/3/18'), 180 | ('14','3','104','1/3/18','2/3/18'), 181 | ('13','3','107','1/3/18','2/3/18'), 182 | ('13','3','102','1/3/18','2/3/18'), 183 | ('19','3','102','12/12/17','1/12/18'), 184 | ('20','4','103','1/3/18','2/3/18'), 185 | ('1','4','102','1/12/18','2/12/18'), 186 | ('3','4','107','1/3/18','2/3/18'), 187 | ('18','4','107','1/3/18','2/3/18'), 188 | ('12','4','102','1/4/18','2/4/18'), 189 | ('11','4','103','1/15/18','2/15/18'), 190 | ('9','4','103','1/15/18','2/15/18'), 191 | ('7','4','107','1/1/18','2/2/18'), 192 | ('4','4','103','1/1/18','2/2/18'), 193 | ('1','4','103','2/2/17','3/2/18'), 194 | ('20','4','103','1/3/18','2/3/18'), 195 | ('1','4','102','1/12/18','2/12/18'), 196 | ('3','4','107','1/13/18','2/13/18'), 197 | ('18','4','107','1/13/18','2/13/18'), 198 | ('12','4','102','1/14/18','2/14/18'), 199 | ('11','4','103','1/15/18','2/15/18'), 200 | ('9','4','103','1/15/18','2/15/18'), 201 | ('7','4','107','1/19/18','2/19/18'), 202 | ('4','4','103','1/19/18','2/19/18'), 203 | ('1','4','103','1/22/18','2/22/18'); 204 | 205 | 206 | SELECT * FROM tbl_book_loans 207 | 208 | INSERT INTO tbl_book_copies 209 | (book_copies_BookID, book_copies_BranchID, book_copies_No_Of_Copies) 210 | VALUES 211 | ('1','1','5'), 212 | ('2','1','5'), 213 | ('3','1','5'), 214 | ('4','1','5'), 215 | ('5','1','5'), 216 | ('6','1','5'), 217 | ('7','1','5'), 218 | ('8','1','5'), 219 | ('9','1','5'), 220 | ('10','1','5'), 221 | ('11','1','5'), 222 | ('12','1','5'), 223 | ('13','1','5'), 224 | ('14','1','5'), 225 | ('15','1','5'), 226 | ('16','1','5'), 227 | ('17','1','5'), 228 | ('18','1','5'), 229 | ('19','1','5'), 230 | ('20','1','5'), 231 | ('1','2','5'), 232 | ('2','2','5'), 233 | ('3','2','5'), 234 | ('4','2','5'), 235 | ('5','2','5'), 236 | ('6','2','5'), 237 | ('7','2','5'), 238 | ('8','2','5'), 239 | ('9','2','5'), 240 | ('10','2','5'), 241 | ('11','2','5'), 242 | ('12','2','5'), 243 | ('13','2','5'), 244 | ('14','2','5'), 245 | ('15','2','5'), 246 | ('16','2','5'), 247 | ('17','2','5'), 248 | ('18','2','5'), 249 | ('19','2','5'), 250 | ('20','2','5'), 251 | ('1','3','5'), 252 | ('2','3','5'), 253 | ('3','3','5'), 254 | ('4','3','5'), 255 | ('5','3','5'), 256 | ('6','3','5'), 257 | ('7','3','5'), 258 | ('8','3','5'), 259 | ('9','3','5'), 260 | ('10','3','5'), 261 | ('11','3','5'), 262 | ('12','3','5'), 263 | ('13','3','5'), 264 | ('14','3','5'), 265 | ('15','3','5'), 266 | ('16','3','5'), 267 | ('17','3','5'), 268 | ('18','3','5'), 269 | ('19','3','5'), 270 | ('20','3','5'), 271 | ('1','4','5'), 272 | ('2','4','5'), 273 | ('3','4','5'), 274 | ('4','4','5'), 275 | ('5','4','5'), 276 | ('6','4','5'), 277 | ('7','4','5'), 278 | ('8','4','5'), 279 | ('9','4','5'), 280 | ('10','4','5'), 281 | ('11','4','5'), 282 | ('12','4','5'), 283 | ('13','4','5'), 284 | ('14','4','5'), 285 | ('15','4','5'), 286 | ('16','4','5'), 287 | ('17','4','5'), 288 | ('18','4','5'), 289 | ('19','4','5'), 290 | ('20','4','5'); 291 | 292 | SELECT * FROM tbl_book_copies 293 | 294 | 295 | INSERT INTO tbl_book_authors 296 | (book_authors_BookID,book_authors_AuthorName) 297 | VALUES 298 | ('1','Patrick Rothfuss'), 299 | ('2','Stephen King'), 300 | ('3','Stephen King'), 301 | ('4','Frank Herbert'), 302 | ('5','J.R.R. Tolkien'), 303 | ('6','Christopher Paolini'), 304 | ('6','Patrick Rothfuss'), 305 | ('8','J.K. Rowling'), 306 | ('9','Haruki Murakami'), 307 | ('10','Shel Silverstein'), 308 | ('11','Douglas Adams'), 309 | ('12','Aldous Huxley'), 310 | ('13','William Goldman'), 311 | ('14','Chuck Palahniuk'), 312 | ('15','Louis Sachar'), 313 | ('16','J.K. Rowling'), 314 | ('17','J.K. Rowling'), 315 | ('18','J.R.R. Tolkien'), 316 | ('19','George R.R. Martin'), 317 | ('20','Mark Lee'); 318 | 319 | SELECT * FROM tbl_book_authors 320 | END 321 | /*============================== END POPULATING TABLES ==============================*/ 322 | 323 | /* =================== STORED PROCEDURE QUERY QUESTIONS =================================== */ 324 | 325 | /* #1- How many copies of the book titled "The Lost Tribe" are owned by the library branch whose name is "Sharpstown"? */ 326 | 327 | CREATE PROC dbo.bookCopiesAtAllSharpstown 328 | (@bookTitle varchar(70) = 'The Lost Tribe', @branchName varchar(70) = 'Sharpstown') 329 | AS 330 | SELECT copies.book_copies_BranchID AS [Branch ID], branch.library_branch_BranchName AS [Branch Name], 331 | copies.book_copies_No_Of_Copies AS [Number of Copies], 332 | book.book_Title AS [Book Title] 333 | FROM tbl_book_copies AS copies 334 | INNER JOIN tbl_book AS book ON copies.book_copies_BookID = book.book_BookID 335 | INNER JOIN tbl_library_branch AS branch ON book_copies_BranchID = branch.library_branch_BranchID 336 | WHERE book.book_Title = @bookTitle AND branch.library_branch_BranchName = @branchName 337 | GO 338 | EXEC dbo.bookCopiesAtAllSharpstown 339 | 340 | 341 | /* #2- How many copies of the book titled "The Lost Tribe" are owned by each library branch? */ 342 | 343 | CREATE PROC dbo.bookCopiesAtAllBranches 344 | (@bookTitle varchar(70) = 'The Lost Tribe') 345 | AS 346 | SELECT copies.book_copies_BranchID AS [Branch ID], branch.library_branch_BranchName AS [Branch Name], 347 | copies.book_copies_No_Of_Copies AS [Number of Copies], 348 | book.book_Title AS [Book Title] 349 | FROM tbl_book_copies AS copies 350 | INNER JOIN tbl_book AS book ON copies.book_copies_BookID = book.book_BookID 351 | INNER JOIN tbl_library_branch AS branch ON book_copies_BranchID = branch.library_branch_BranchID 352 | WHERE book.book_Title = @bookTitle 353 | GO 354 | EXEC dbo.bookCopiesAtAllBranches 355 | 356 | 357 | /* #3- Retrieve the names of all borrowers who do not have any books checked out. */ 358 | 359 | CREATE PROC dbo.NoLoans 360 | AS 361 | SELECT borrower_BorrowerName FROM tbl_borrower 362 | WHERE NOT EXISTS 363 | (SELECT * FROM tbl_book_loans 364 | WHERE book_loans_CardNo = borrower_CardNo) 365 | GO 366 | EXEC dbo.NoLoans 367 | 368 | /* #4- For each book that is loaned out from the "Sharpstown" branch and whose DueDate is today, retrieve the book title, the borrower's name, and the borrower's address. */ 369 | 370 | CREATE PROC dbo.LoanersInfo 371 | (@DueDate date = NULL, @LibraryBranchName varchar(50) = 'Sharpstown') 372 | AS 373 | SET @DueDate = GETDATE() 374 | SELECT Branch.library_branch_BranchName AS [Branch Name], Book.book_Title [Book Name], 375 | Borrower.borrower_BorrowerName AS [Borrower Name], Borrower.borrower_BorrowerAddress AS [Borrower Address], 376 | Loans.book_loans_DateOut AS [Date Out], Loans.book_loans_DueDate [Due Date] 377 | FROM tbl_book_loans AS Loans 378 | INNER JOIN tbl_book AS Book ON Loans.book_loans_BookID = Book.book_BookID 379 | INNER JOIN tbl_borrower AS Borrower ON Loans.book_loans_CardNo = Borrower.borrower_CardNo 380 | INNER JOIN tbl_library_branch AS Branch ON Loans.book_loans_BranchID = Branch.library_branch_BranchID 381 | WHERE Loans.book_loans_DueDate = @DueDate AND Branch.library_branch_BranchName = @LibraryBranchName 382 | GO 383 | EXEC dbo.LoanersInfo 384 | 385 | /* #5- For each library branch, retrieve the branch name and the total number of books loaned out from that branch. */ 386 | 387 | CREATE PROC dbo.TotalLoansPerBranch 388 | AS 389 | SELECT Branch.library_branch_BranchName AS [Branch Name], COUNT (Loans.book_loans_BranchID) AS [Total Loans] 390 | FROM tbl_book_loans AS Loans 391 | INNER JOIN tbl_library_branch AS Branch ON Loans.book_loans_BranchID = Branch.library_branch_BranchID 392 | GROUP BY library_branch_BranchName 393 | GO 394 | EXEC dbo.TotalLoansPerBranch 395 | 396 | /* #6- Retrieve the names, addresses, and number of books checked out for all borrowers who have more than five books checked out. */ 397 | 398 | CREATE PROC dbo.BooksLoanedOut 399 | (@BooksCheckedOut INT = 5) 400 | AS 401 | SELECT Borrower.borrower_BorrowerName AS [Borrower Name], Borrower.borrower_BorrowerAddress AS [Borrower Address], 402 | COUNT(Borrower.borrower_BorrowerName) AS [Books Checked Out] 403 | FROM tbl_book_loans AS Loans 404 | INNER JOIN tbl_borrower AS Borrower ON Loans.book_loans_CardNo = Borrower.borrower_CardNo 405 | GROUP BY Borrower.borrower_BorrowerName, Borrower.borrower_BorrowerAddress 406 | HAVING COUNT(Borrower.borrower_BorrowerName) >= @BooksCheckedOut 407 | GO 408 | EXEC dbo.BooksLoanedOut 409 | 410 | 411 | 412 | /* #7- For each book authored by "Stephen King", retrieve the title and the number of copies owned by the library branch whose name is "Central".*/ 413 | 414 | CREATE PROC dbo.BookbyAuthorandBranch 415 | (@BranchName varchar(50) = 'Central', @AuthorName varchar(50) = 'Stephen King') 416 | AS 417 | SELECT Branch.library_branch_BranchName AS [Branch Name], Book.book_Title AS [Title], Copies.book_copies_No_Of_Copies AS [Number of Copies] 418 | FROM tbl_book_authors AS Authors 419 | INNER JOIN tbl_book AS Book ON Authors.book_authors_BookID = Book.book_BookID 420 | INNER JOIN tbl_book_copies AS Copies ON Authors.book_authors_BookID = Copies.book_copies_BookID 421 | INNER JOIN tbl_library_branch AS Branch ON Copies.book_copies_BranchID = Branch.library_branch_BranchID 422 | WHERE Branch.library_branch_BranchName = @BranchName AND Authors.book_authors_AuthorName = @AuthorName 423 | GO 424 | EXEC dbo.BookbyAuthorandBranch 425 | 426 | /* ==================================== STORED PROCEDURE QUERY QUESTIONS =================================== */ 427 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Library-Management-System --------------------------------------------------------------------------------