Library_database_SQL.xhtml 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2017 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. $xhtml = array(
  20. 'title' => "Library database $a[SQL]",
  21. 'subtitle' => 'Written in <span title="Databases 1">CS 2203</span> of <a href="http://www.uopeople.edu/">University of the People</a>, finalised on 2017-07-19',
  22. 'copyright year' => '2017',
  23. 'body' => <<<END
  24. <h2>$a[SQL] commands</h2>
  25. <blockquote>
  26. <pre><code>create table Borrower (
  27. card_number int generated by default as identity (start with 0, increment by 1) not null primary key,
  28. name varchar(32) not null,
  29. address varchar(32) not null,
  30. postal_code int not null,
  31. telephone_number int,
  32. join_date date default current_date not null,
  33. check (postal_code &lt; 100000)
  34. );
  35. create table Librarian (
  36. id int generated by default as identity (start with 0, increment by 1) not null primary key,
  37. name varchar(32) not null,
  38. extension int not null,
  39. reports_to int,
  40. foreign key (reports_to) references Librarian(id)
  41. );
  42. create table BookMeta (
  43. isbn bigint not null primary key,
  44. title varchar(64) not null,
  45. author varchar(32) not null,
  46. publication_date date not null,
  47. check (isbn &lt; 10000000000000)
  48. );
  49. create table Book(
  50. id int generated by default as identity (start with 0, increment by 1) not null primary key,
  51. isbn bigint not null,
  52. cost decimal(5,2) not null,
  53. foreign key (isbn) references BookMeta(isbn)
  54. );
  55. create table BookLended(
  56. book int not null primary key,
  57. checked_out date default current_date not null,
  58. due_back date not null,
  59. borrower int not null,
  60. librarian int,
  61. check (due_back &gt; checked_out),
  62. foreign key (book) references Book(id),
  63. foreign key (borrower) references Borrower(card_number),
  64. foreign key (librarian) references Librarian(id)
  65. );</code></pre>
  66. </blockquote>
  67. <h2>Screenshot</h2>
  68. <img src="/img/CC_BY-SA_4.0/y.st./coursework/CS2203/Library_database_SQL.png" alt="Database objects" class="weblog-header-image" width="623" height="356" />
  69. <h2>Explanations</h2>
  70. <p>
  71. On the <code>Borrower</code> table, the <code>card_number</code> is set to auto-increment so we don&apos;t have to use an external system to assign card numbers.
  72. The <code>telephone_number</code> is allowed to be null, because some people (such as myself) don&apos;t have telephone service and therefore don&apos;t have a telephone number.
  73. It&apos;s a pain dealing with companies that insist that you need a telephone number, so in my implementation of the library, I gave people like me a break.
  74. If a borrower doesn&apos;t have a telephone number, the database can accommodate that.
  75. The <code>postal_code</code> has a constraint that prevents postal codes with more than five digits from being entered, as postal codes are five digits long.
  76. </p>
  77. <p>
  78. Like <code>Borrower</code> <code>card_number</code>s, <code>Librarian</code> <code>id</code>s are assigned automatically.
  79. The telephone <code>extension</code>, unlike <code>Borrower</code> <code>telephone_number</code>, is not allowed to be null.
  80. This is because all librarians in the building will have an extension using the library&apos;s telephone service.
  81. We can manually add a <code>join_date</code> if we want, but it defaults to the current date.
  82. This is because we&apos;re probably adding them to our database as soon as they register an account with the library.
  83. The unary relationship we defined in class is included.
  84. The assignment instructions said we don&apos;t need it, but they didn&apos;t explicitly say <strong>*not*</strong> to include it, and including it was actually quite easy.
  85. </p>
  86. <p>
  87. The <code>BookMeta</code> table was added to remove redundancy in the <code>Book</code> table.
  88. Each copy of a book needs its own entry in the <code>Book</code> table for a few reasons.
  89. First, differing copies might have been bought for different prices, so the <code>cost</code> needs to be independent for each copy.
  90. Second, each copy needs to be tracked separately in the <code>BookLended</code> relation.
  91. However, all copies of the same book will have the same $a[ISBN], publication date, author, and title.
  92. A check is performed on the <code>isbn</code> field to ensure it&apos;s not longer than thirteen digits, as while $a[ISBN]s have a few different lengths, none are longer than thirteen digits.
  93. </p>
  94. <p>
  95. The <code>Book</code> relation references the <code>BookMeta</code> relation for most of its information, but gives a separate <code>id</code> to each copy of a book and holds the <code>cost</code> of that copy.
  96. <code>id</code>s are mostly meaningless outside the database, so they&apos;re simply assigned automatically.
  97. Once the <code>id</code>s are assigned, barcodes for them can be generated and put on the library&apos;s books.
  98. </p>
  99. <p>
  100. The <code>BookLended</code> table ties the three other main tables together.
  101. The <code>book</code> attribute acts as both a primary key and a foreign key.
  102. As a foreign key, it tells us which book was lent out.
  103. It works as a primary key too though because a book cannot be lent out to two people at once.
  104. When the book is returned, the tuple representing that lending will be removed, and the book can be lent out again.
  105. <code>book</code> and <code>borrower</code> cannot be null, as there can&apos;t be a lending of a book if there is no book lent and/or no one borrowing it.
  106. If the book is removed from the collection, the lending information should be dropped too.
  107. If we want to remove a borrower from the system, we should first have them check the book back in or we should strike any books they haven&apos;t returned from the system.
  108. As long as we&apos;re keeping the books, even if we disallow that patron from borrowing any more, we need to keep that former patron&apos;s information in the database so we know who has our book.
  109. <code>librarian</code> <strong>*can*</strong> be null though.
  110. If we fire a librarian and remove them from the database, the books that they lent out before are still out there, and we need to know which books are missing and who has them.
  111. When a book is checked out, <code>checked_out</code> is automatically set to the current date, unless we specify otherwise.
  112. A book cannot be <code>due_back</code> before it is checked out though, so a constraint ensures this basic logic is followed.
  113. <code>due_back</code> isn&apos;t automatically set though, as the instructions never told us how long a book could be checked out for.
  114. Additionally, there could be multiple factors.
  115. A long-time patron may be allowed to check books out for longer or a popular book may be due back sooner.
  116. By not automatically setting a due date, we allow for flexibility.
  117. </p>
  118. END
  119. );