Class Checkpoint

Making Parkwood Vale · Part 2 of 13

Part 2

Design the database with an ERD, then build the users, loans, copies and books tables in HeidiSQL.

We plan the library's relational database with an entity relationship diagram (ERD), then create it in HeidiSQL with its keys and a first admin user. In this part: 0:13 Why the transactional website needs a relational database 0:42 Drawing the ERD in Visual Paradigm, with primary and foreign keys 2:44 Starting UwAmp and opening phpMyAdmin and HeidiSQL 3:53 Creating the Parkwood Vale database 4:57 The users table, including isAdmin and loanTokens 9:04 The loans table, linked to users by email 12:36 The copies table, and fixing a foreign key error 14:19 The books table 16:58 Adding an admin user and fixing a postcode that was too long 18:46 Checking the tables in phpMyAdmin Software: Visual Paradigm, UwAmp, HeidiSQL, phpMyAdmin, MySQL.
Transcript
  1. In the previous video, we looked at setting up the environment ready to create our
  2. transactional website. Now it's time to look at setting up the relational database
  3. that will run the backend of our transactional website.
  4. We're going to start off by looking at creating an ERD, an entity relationship
  5. diagram, before moving on to actually following that diagram to create our table
  6. within our database using HeidiSQL.
  7. So let's get started.
  8. Okay, so I'm back on my student workstation and I've got online visual paradigm
  9. open. It's a brilliant...
  10. I'm back on my student workstation and I've got online visual paradigm open.
  11. This is a brilliant online ERD creator.
  12. And what I'm going to do is I'm going to start off by showing you a few different
  13. things within here.
  14. Starting off, I have my database laid out just like it would be in the exemplar for
  15. Parkwood Vale, available from the WJEC.
  16. And I start in my books table here, then I've got ISBN and I need to set that to be
  17. the primary key. So I've got to first click to highlight my column.
  18. I'm going to then right click and just create primary key.
  19. Down here we already have our ISBN set as a foreign key.
  20. You can tell it's forward slash, it's slanted.
  21. And I'm just going to click to drag and create a relationship between these two.
  22. The relationship between books and if I go out a second, the copies table is going
  23. to be a one to many. One book can have many copies.
  24. In the same way, I've got one user can have many loans.
  25. The loans to copies table join is going to be a
  26. simple one to one. And that's created my
  27. ERD for me in place. Each of these different elements then I just
  28. simply added by right-clicking and added a column in place and over on the side here
  29. you've got your different styles of templates.
  30. With that then you can either print screen it, you can use a snipping tool, you can
  31. go to file where you can save it or you can go into save as.
  32. This will allow you to download it to your computer, set it as your device and it's
  33. going to ask you how you want to save it to your device.
  34. REID is going to be important because that's going to be the blueprint to our
  35. I'm gonna reinsert the USB so that is just here let's put that back into our
  36. computer and on my device I have
  37. UwAmp I'm going to run UwAmp and get that set
  38. up I
  39. also have that portable HeidiSQL we talked about last time as well I've got these
  40. elements in place I'm not going to be changing any of the settings pre built in to
  41. UwAmp. I'm quite happy with PHP 7, quite happy with MySQL 5.7.
  42. There's no need for any changes there.
  43. I could click onto phpMyAdmin and that'd bring me through to my database editor.
  44. The username and password are both root and that'll allow me access to my database
  45. element here.
  46. Now, personally, I'm not a fan of working with databases directly in phpMyAdmin.
  47. I find it cumbersome. I find it complicated.
  48. I find it doesn't lend itself nicely to production, which is why I said in video
  49. one to install HeidiSQL or get the portable version.
  50. If you want to use
  51. phpMyAdmin, by all means do. I'm going to open up HeidiSQL here though.
  52. I've got my connection to localhost under root and root.
  53. I am going to change the name here.
  54. Just right click and go to rename and call this Parkwood Vale to
  55. make it easy to remember what's going on.
  56. Now when I open this up it's going to be a bit of a nicer interface and it's going
  57. to allow me to do just what I want to do to create my database like I've set out
  58. here.
  59. First thing I want to do is right click and I want to create a new database and it's
  60. going to be called Parkwood Vale.
  61. And click, okay. So there it has created my Parkwood Vale database for me, which
  62. currently has no table sat inside of it.
  63. The way that this works is it connects to my database and provides the SQL
  64. statements for me to create the different elements.
  65. And as you can see here, if I expand this, we've got our create
  66. then show all databases. So everything I am doing, you can see the SQL for it just
  67. underneath if you wanted to see what's going on in depth.
  68. From there, I'm gonna right click, create a new table.
  69. And this is where my visual ERD comes into play because I'm gonna follow what I've
  70. set up here. So the name of my table is gonna be users.
  71. I'm gonna then go through starting off, adding my column.
  72. So I'm gonna add a column. instead of column one I'm going to call it email and
  73. instead of having a data type of the integer I'm going to choose a much more
  74. appropriate data type as a var char.
  75. Now it's currently set to 50 just to make sure if there's no truncation or anything
  76. like that I'm going to set it to 255.
  77. I'm going to leave the rest of the settings as they are.
  78. I'm telling it there is no default element, I'm going to untick actually to say, no,
  79. we can't have null values because this is going to be our primary key.
  80. We can't have a null value primary key.
  81. Next up then, I'm going to add in my password and
  82. simply again, it presets remembering my previous values of Varchar and
  83. 255.
  84. Realistically, we don't want a null value for a password either.
  85. So I'm going to leave that unticked and there are
  86. Next up then I'm gonna have forename and then I'm gonna go surname and it's just as
  87. simple as filling in this table.
  88. All of my settings are already put into 255s the only thing that changes is when I
  89. get to postcodes so I can just carry on working through.
  90. In here then I've got street name and town
  91. and then I'm gonna go into postcode.
  92. Now depending how you are setting your database out you could
  93. and link between the two.
  94. For what I created, I don't think there was a need for it, so I didn't include a
  95. separate table. Each person is going to have one address.
  96. We're not doing an ordering system.
  97. We're not doing an Amazon-style system where they could have possible addresses.
  98. They have one address, which is where they are registered to.
  99. So next up then is admin.
  100. Bit of a strange one, but this is going to be our differentiator.
  101. This is going to tell us if the user logged on should have admin rights.
  102. I'm going to change that from Varchar, just scrolling through all of these options
  103. here, to standard integers. It gives me a readout telling me what the standard
  104. integer would be, and that only needs one byte of space, so I can just put one in
  105. the box there.
  106. I'm going to add a second one then, which has appeared just at the top.
  107. I'm just going to move that downwards, scroll down to get to it, and change it to
  108. loan tokens.
  109. this means that in this loan token section which again should be an integer so let's
  110. just set that to integer and i've got it preset as one they are going to be in place
  111. to say how many books someone can take out at once now when we are
  112. looking at our allow nulls realistically we don't need any information to contain
  113. nulls so it's okay to leave them unticked our default values zero for is
  114. admin
  115. Let's get off. No, they're not admins.
  116. Default loan tokens. I can preset that custom text.
  117. I'm going to say three and click on OK.
  118. Now let's put it in as three as in the text and that should fill that in quite
  119. nicely for me. If I click save, what that does is it creates my table, but I haven't
  120. gone through and set my primary key just yet.
  121. We expand that down and close this off a little bit so we can see more of this
  122. section and we can see all of our details.
  123. In our indexes option, I can choose add a primary key.
  124. Index 1, change the type. Isn't just a key, it is a primary key.
  125. And then I can drag email into place and click save.
  126. And that has created my first table.
  127. I'm going to repeat the process for loans and on with books and
  128. copies as well. It really doesn't matter which order you create your tables in, but
  129. I am going to go around in a U shape.
  130. So create new table. Give the table a name.
  131. This is loans.
  132. And add in the columns. So I'm going to put in again email as
  133. Varchar.
  134. And set it to 255. Now this has to mirror the same thing in
  135. users. because when we create the relationship between the two of them, if they
  136. aren't mirrored, then it won't be happy with how we've set that up.
  137. So allow null, no, no default, perfectly fine.
  138. I'm going to add in another column there.
  139. It's going to be library ID.
  140. I'm going to drop the underscore just to uniform it a little bit more because
  141. nothing else had underscores. So everything is just camel cased instead.
  142. So in here, library ID, again, varchar, this time around, I'm presetting it to 18
  143. bits.
  144. Next up is loan date. So I'm going to add that in.
  145. And this is going to use the pre-built date and time function.
  146. So there it is, date and time.
  147. Date and time will contain, as it states, both the time format and the date format.
  148. Realistically, I could probably get away with just date, given the system I'm using,
  149. but I'm going to put it as date and time.
  150. Same goes then for due date and the same goes
  151. for return date.
  152. Now due date and return date are there.
  153. The loan date will be the date they take the book out.
  154. The due date will be when they are supposed to return it and the return date will be
  155. when they actually returned it.
  156. So you could build in some form of analytics where you look at how many people late
  157. returned a book. The last one is the return flag where you can see whether a loan
  158. has been returned or not. So I can say return and that is going to be
  159. set to an integer.
  160. So let's scroll to the integer section and there we go.
  161. Have that in place. Again, character set, length set, one is perfectly fine.
  162. I'm going to click save on there for a second and that's all set up.
  163. Now what I can do on this point is set up my foreign keys.
  164. The way that I set up my foreign key is I click onto the foreign keys option within
  165. my table. And then I click onto add new key.
  166. I've got FK1 with my key name.
  167. And I'm going to choose the email as my starting column.
  168. The table it's going to be linked to is going to be my users table.
  169. And the key there is also called email.
  170. Now, with those in place, then I can click on save and that will save that foreign
  171. key, FK loans link for me and my two tables are in place.
  172. Next up is to create my next table, which is my loans table.
  173. Let me just move this back over to the side here and make this a bit smaller so I
  174. can see the two at once.
  175. And actually, can I snap that to one side and that to the other side and get rid of
  176. that pain?
  177. Sadly not, but I can still see that enough to be able to create what I need to.
  178. So I'm going to create a new table and that table is going to be called loans.
  179. No, sorry, I've already done the loans table.
  180. Let me move over and I'm going to do the copies table next.
  181. And in my copies table, I'm going to have my ISBN, which is a VAR char
  182. and it is 14 in character length.
  183. That is the one foreign key in here.
  184. My library ID would also be a foreign key.
  185. So let me just mark that as a foreign key there.
  186. And I'm going to add in library ID.
  187. It is a Varchar this time around of 18 key loans.
  188. Neither are going to be allowed nulls.
  189. So I'm going to save those. I want to put in my link between my loans and my
  190. copies. So I'm going to go back to my foreign key section.
  191. from my library ID.
  192. That is going to reference my loans table and reference my library ID.
  193. Now, when I click on save, I get a warning.
  194. It's telling me that it cannot add that foreign key constraint.
  195. I need to go into the options section of my database and change the database
  196. engine.
  197. InnoDB isn't very friendly when it comes to foreign keys.
  198. If I change it to MyISAM, this one here, I can then save that
  199. and it'll happily take my foreign key selection in place.
  200. So if you do get that warning, it's probably because you're using the engine InnoDB.
  201. It is the default engine. It's a bit of an outdated engine.
  202. So if you're using your keys, make sure you change that over.
  203. Now, I'm going to add in my final table, my create new table, and it
  204. is going to be my books table.
  205. And I'm going to add in the details for that.
  206. So ISBN, and it is going to be voucher
  207. 14.
  208. That is going to be my primary key.
  209. I can't have a null ISBN. I can't have an empty ISBN.
  210. There has to be a value for my database to function.
  211. so I'm going to untick null and have no default.
  212. That will form my primary key on this section here.
  213. Now, when I am doing that, I am already going to swap it over to MyISAM because
  214. that's what I want for my foreign key in a second to put that in place.
  215. I'm going to add in my second column, my name column.
  216. It isn't going to be an integer.
  217. It's going to be a VAR chart and it's going to contain two 55 character bits.
  218. Now it might seem strange, but I am going to leave allow null in place.
  219. That will come clear as to why a little bit later on when we get further into these
  220. series and we look at important information.
  221. I'm going to put in publisher and I'm going to add in as well description.
  222. Now as you can see, I've got a var char for my description, but here I have the
  223. option of text. so let me drop that down swap that over to text that allows me to
  224. have a bit more of a fluid
  225. entry i'm going to have rating which just like i've stated there will be a type of
  226. decimal
  227. and i'm going to add in jacket image
  228. and that should as well be a varchar back to that 255 point
  229. i'm going to add in available copies
  230. and set that to be its integer value,
  231. and put a property in place.
  232. Now, I'll save that, go into my foreign keys, and create the link between my two
  233. tables. So this is going from ISBN to my reference table of copies,
  234. to my ISBN.
  235. Click on Save.
  236. and I should now have in place my links between all my different tables.
  237. So I have created my database that follows my design
  238. parameters here.
  239. I will do one last thing for this video, and that is go into my
  240. users table, which is here, and go into the data section across
  241. the top and add myself into it.
  242. so first off i'm going to put in cpd at mrjjones
  243. .co.uk password for now password one two three nice and
  244. secure
  245. now i do need to fill in these details so forename james surname
  246. jones street i'm going to say police
  247. T, Gwyn,
  248. Town,
  249. Ponte Preeze,
  250. Postcode, CF465HotelXray,
  251. is admin, I am admin, so that's a 1.
  252. It's telling me my data is too long for postcode because I have a break in between
  253. them. So let's just remove that break.
  254. And then it's added me to that database.
  255. Let me expand that out. Let's expand that out.
  256. And you can see it's in place.
  257. Now as I said all along, all the messages and prompts I've got as I've been creating
  258. this have come down the bottom here so I can see the SQL that's included as I've
  259. gone through. You can see the insert element here where my passcode is too long and
  260. insert again below where is then the right length.
  261. I can use the simple interface to add rows if I want to, ready for
  262. my designing and testing of my database.
  263. If I go into books, all the details are there.
  264. Copies, loans, everything is in place.
  265. And every time I click onto a different table, a new SQL command is
  266. executed to display that information for me.
  267. So that should allow you to set up the database that will be used in the back end of
  268. your transactional website. I'm going to close down HeidiSQL.
  269. And then I'm going to go back into UwAmp, which I did minimize, and open up
  270. phpMyAdmin.
  271. Log back in with root. Username and password are always root combined.
  272. I've got in there parkwoodvale.
  273. I've got my users.
  274. And that's all set up. I've got my loans table and everything I've created using
  275. HeidiSQL is also in place and checkable using that
  276. my phpMyAdmin. Everything's in place and running.
  277. I'm done for this session so I'm simply going to stop everything and
  278. they're all stopped and I can close it all down.
  279. hopefully that all made sense to you and we can start moving on to setting up our
  280. dream environment and start building our website any problems just reach out and
  281. give me a message thank you