YouTube2Text

Database Design Full Course - How to Design a Relational Database (1.5 Hours!) — Transcript

by Caleb Curry · 14,232 words · 2,152 segments · language en · Watch on YouTube

Full transcript

  1. 0:00Welcome to your database design course.
  2. 0:02This video is designed to cover
  3. 0:03everything we talk about in my eight
  4. 0:04hour database design video without
  5. 0:06having to spend all day studying the
  6. 0:08topic. But there's no real information
  7. 0:10lost because the way I designed this
  8. 0:12video is to take you from the complete
  9. 0:14beginning to being able to design
  10. 0:16databases confidently. And best of all,
  11. 0:18you'll be able to look at existing
  12. 0:19database designs and be able to identify
  13. 0:21certain issues that could cause problems
  14. 0:23later on down the road. Database design
  15. 0:25is one of those things where a little
  16. 0:26bit of effort goes a really long way and
  17. 0:28if you mess things up, you might end up
  18. 0:30causing issues with your apps down the
  19. 0:31road, having to come back and spending
  20. 0:33hours or days fixing your database
  21. 0:35design and wasting a lot of time
  22. 0:37figuring out how to enforce data
  23. 0:38integrity. So if you start with the
  24. 0:40database design from the beginning,
  25. 0:41you're going to save yourself so much
  26. 0:43time in the future. So we'll start off
  27. 0:44with some foundational stuff. If you've
  28. 0:46done anything with databases or
  29. 0:47application development before, a lot of
  30. 0:49this might be a review, but let's just
  31. 0:50go through it to make sure everybody's
  32. 0:51on the same page for the upcoming
  33. 0:53content. You can, however, use the notes
  34. 0:55and the timestamps if you need to jump
  35. 0:56to any section. So when I'm talking
  36. 0:58about databases, I'll often draw it
  37. 1:00something like this, which is like the
  38. 1:02universal database icon. And what do we
  39. 1:05actually store in a database? Well, put
  40. 1:08simply, we store data. Wow. That's
  41. 1:11incredible, Caleb. Never thought of
  42. 1:13that. So what exactly would be
  43. 1:14classified as data? Well, really
  44. 1:16anything you want to store to be able to
  45. 1:18access that information later. You can
  46. 1:20think of it just as info that we need to
  47. 1:22persist for later access. So a common
  48. 1:24example of this is if you have a website
  49. 1:27or an app, it could be a desktop app or
  50. 1:30a mobile app, and there might be an
  51. 1:32option to log in. Well, in order for
  52. 1:34that system to know if you're logging in
  53. 1:36correctly, it needs to check data in the
  54. 1:38database. So that's one example, storing
  55. 1:41user information. You can also store
  56. 1:43events, when the person logged in, their
  57. 1:45activity. You can store things like user
  58. 1:47settings, what are their preferences.
  59. 1:49Any of the data that they access on your
  60. 1:51website needs to be stored somewhere as
  61. 1:52well. Now, this is like a visual diagram
  62. 1:55of what a database looks like, but what
  63. 1:56is a database? Well, it's just a
  64. 1:59software going to be installed on a
  65. 2:01server.
  66. 2:05So it might be obvious, might not be if
  67. 2:07you've never really seen servers, but
  68. 2:09there's not an actual system that looks
  69. 2:11like this in person, right? It's just a
  70. 2:13regular computer. So there will be
  71. 2:15server racks or even just computers like
  72. 2:17your own personal computer, and you will
  73. 2:19install a relational database management
  74. 2:23system.
  75. 2:24So relational here, that refers to
  76. 2:26tables. So everything is tabular data.
  77. 2:29So when you look at it and you visualize
  78. 2:30that data, it's going to look almost
  79. 2:32like a spreadsheet. So we'll talk a
  80. 2:34little bit about that structure, but
  81. 2:35first I want you to understand that the
  82. 2:38database is just the software on a
  83. 2:40computer. That computer could be
  84. 2:41dedicated to that database or it could
  85. 2:44also be running your backend or other
  86. 2:46services as well. So it really depends
  87. 2:48on what scale you are at, how much
  88. 2:50computing you dedicate to this database.
  89. 2:52But fortunately, all the database design
  90. 2:55stuff is going to be pretty much the
  91. 2:57same. The only time it's going to make a
  92. 2:59difference is if you get to things like
  93. 3:01sharding and splitting up data across
  94. 3:04regions. That gets into some more
  95. 3:06complex system design topics. I do have
  96. 3:08dedicated lessons on that. For most
  97. 3:10applications and what you're probably
  98. 3:12going to be using is just a centralized
  99. 3:14database in one location.
  100. 3:16So that means somewhere we have this
  101. 3:18database stored and then all the
  102. 3:20requests get funnelled to this one
  103. 3:22location. So that's a little bit about
  104. 3:23how it works behind the scenes, but for
  105. 3:24the rest of the video, we'll pretty much
  106. 3:26ignore the computer hardware and just
  107. 3:28talk about how things work for the
  108. 3:30actual database software. Okay, so we
  109. 3:33talked about this being built as tables.
  110. 3:37And when you hear the term relation,
  111. 3:40fundamentally this means tables, but
  112. 3:42you'll also hear relationships.
  113. 3:47These are fundamentally different if
  114. 3:49you're studying database design
  115. 3:51formally, but many people do call
  116. 3:53relationships relations and that gets a
  117. 3:55little confusing. But that's what it
  118. 3:56means when we're talking about a
  119. 3:58relational database management system.
  120. 4:00We're building things structured as
  121. 4:02tables. So if we can visualize the data
  122. 4:04as tables, we're basically going to have
  123. 4:06columns at the top and then we'll have
  124. 4:09different rows of data.
  125. 4:11And this looks a lot like a spreadsheet.
  126. 4:14So, when would you want to use a
  127. 4:16database over just a simple spreadsheet
  128. 4:18if they structure data the same way?
  129. 4:20Well, this really comes down to the use
  130. 4:21case and the scale at which you need to
  131. 4:23run. Generally, with a spreadsheet, I
  132. 4:25think of it as something that a human
  133. 4:27will go in there, look at the data,
  134. 4:28enter in numbers, run calculations,
  135. 4:30whatever they need to do, and typically
  136. 4:32that user is connected to that
  137. 4:33spreadsheet directly.
  138. 4:35With databases, when you're starting
  139. 4:36out, that might be the case, or if
  140. 4:38you're a database administrator, that
  141. 4:39might be the case. You might be working
  142. 4:41with the database directly. But in most
  143. 4:43cases, it's going to be an application
  144. 4:45that connects to the database. So, it
  145. 4:47looks something like this, where
  146. 4:48somebody tries to log in, we have a
  147. 4:50backend application that processes that
  148. 4:52request and accesses anything it might
  149. 4:54need to access from the database. For
  150. 4:56good software engineering principles, we
  151. 4:58want to avoid direct connection to that
  152. 5:01database when we can. By that, I mean we
  153. 5:04want to avoid having people go in here
  154. 5:06and mess with the data in the database.
  155. 5:09This is risky because, well, people make
  156. 5:12mistakes, but also it can put the data
  157. 5:15itself in jeopardy. So, what we do is we
  158. 5:17program the exact things we want to do
  159. 5:19in some software, and then we can
  160. 5:21interact with the database through that
  161. 5:23software. Beyond that, there's other
  162. 5:25benefits as well. Databases are designed
  163. 5:27with indexing for fast querying. They
  164. 5:30have formal relationships so you can
  165. 5:32connect data across tables. They also
  166. 5:34have procedural programming languages,
  167. 5:36and often they can support many
  168. 5:38connections at one time. Some of those
  169. 5:40things can exist with spreadsheets, but
  170. 5:42it's really something for a small scale
  171. 5:45and not something you would want to use
  172. 5:47for the backend of an application. I
  173. 5:49have seen people use basic spreadsheets
  174. 5:51for the backends of applications, but
  175. 5:53that's going to limit them if they want
  176. 5:54to get to a certain level. So, really
  177. 5:56that's just for like personal
  178. 5:58applications. And if I was to start
  179. 5:59something just for fun, I would skip the
  180. 6:01spreadsheet altogether and just go with
  181. 6:03a small database instance. It's easy to
  182. 6:05get started with SQLite or Postgres.
  183. 6:08You can often just use it within a
  184. 6:10container. You don't even have to have a
  185. 6:11tight integration with your system. So
  186. 6:13the general structure for software
  187. 6:15development is going to be some front
  188. 6:18end or client that connects to a back
  189. 6:21end, and the back end is what accesses
  190. 6:23the database. We've already mentioned
  191. 6:25this, but I just want to draw it out
  192. 6:27because I wanted to specifically mention
  193. 6:29that we should not go from the client
  194. 6:31directly. So for [clears throat]
  195. 6:33example, if you are building a website
  196. 6:35or a mobile app or a desktop app, you do
  197. 6:38not want to put that connection string
  198. 6:42in the client.
  199. 6:44Anything on the client you should assume
  200. 6:46is insecure. So if you have some
  201. 6:48connection string to a shared database,
  202. 6:50that client can manipulate that database
  203. 6:52in ways you wouldn't want them to. So
  204. 6:54this is the general structure for a
  205. 6:56centralized
  206. 6:59database. Something you would get with
  207. 7:00Postgres, MySQL, Oracle, SQL Server, or
  208. 7:04any of the other major database systems
  209. 7:06out there. But this brings up the
  210. 7:07question, what if you have some software
  211. 7:10that needs to store information
  212. 7:13and it's not in this client server
  213. 7:15structure? Let's just say it's some
  214. 7:17local video editing software and we need
  215. 7:19to store what edits exist or what files
  216. 7:22exist or whatever it might be. Well,
  217. 7:25there's often going to be a dedicated
  218. 7:28database just for that instance. So in
  219. 7:32this situation, if someone else
  220. 7:33downloaded the software,
  221. 7:37they would have the database copied as
  222. 7:39well. So this is a fundamentally
  223. 7:42different structure than what we have
  224. 7:44here. Because in this situation, if we
  225. 7:47have multiple connections, let's
  226. 7:50say three clients are connected. I'll
  227. 7:53just draw them over here so you can
  228. 7:54visualize them.
  229. 7:58All three of these are going to connect
  230. 8:00to that same backend service. In this
  231. 8:03instance, you're going to have two
  232. 8:05separate clients with two separate
  233. 8:08database instances. They're not shared,
  234. 8:10so you don't have that centralized
  235. 8:12connection.
  236. 8:13This idea is often called an embedded
  237. 8:15database
  238. 8:18because the database is embedded in
  239. 8:21whatever system is running that
  240. 8:23software. It goes with the software. So
  241. 8:25that's what you're going to get for
  242. 8:26things like refrigerators that need to
  243. 8:29store any information, any smart
  244. 8:31technology or offline databases, they're
  245. 8:34likely going to be installed on that
  246. 8:36system. So this is really getting into
  247. 8:37the architecture of databases and a
  248. 8:40little bit less on actually designing
  249. 8:42tables, but this is very important
  250. 8:44because it can influence the way you
  251. 8:46design tables. If your database has to
  252. 8:49support many users, you might have a
  253. 8:52different design than you would if you
  254. 8:54were in a distributed environment. So
  255. 8:56starting off, let's just assume we're in
  256. 8:58this environment here. And in that
  257. 9:00situation, we might have some separation
  258. 9:02where this user cannot access everything
  259. 9:05from this user and vice versa. And those
  260. 9:08complexities is why the software layer
  261. 9:11is so important. So if you're building
  262. 9:13on top of a database, let's
  263. 9:18say this is your app,
  264. 9:21you can code the rules
  265. 9:28for access. You control everything that
  266. 9:31gets through to the database.
  267. 9:34So you could tie this to auth, for
  268. 9:36example, and make sure the person is
  269. 9:37logged in.
  270. 9:39You could tie it to region,
  271. 9:42only giving the information for a
  272. 9:45certain location.
  273. 9:47You can also validate data.
  274. 9:54So that software layer is super
  275. 9:55important.
  276. 9:57Some stuff will be done in the database
  277. 9:59and then other stuff will be done at the
  278. 10:01application layer. So what I want to do
  279. 10:02now is I want to zoom in on this section
  280. 10:05now that we got the architecture and the
  281. 10:07fundamentals out of the way, and we can
  282. 10:09talk what the actual database structure
  283. 10:12will look like. So, let's just get some
  284. 10:13definitions out of the way. These are
  285. 10:17pretty formal, so you might not see
  286. 10:19these so much in interviews, but if
  287. 10:21you're in school, you probably will.
  288. 10:25So, entity type, and then we have
  289. 10:31attributes. So, when you're working with
  290. 10:33a database, you're not just going to
  291. 10:34store one thing, you're going to store
  292. 10:36multiple of those things. So, for
  293. 10:38example, if you have a table for
  294. 10:40comments,
  295. 10:43you're going to have multiple rows, each
  296. 10:45describing a comment. The entity type is
  297. 10:48the thing you're trying to describe, the
  298. 10:50type of data. The attributes are what
  299. 10:52things you care about with this comment.
  300. 10:55So, for example,
  301. 10:57the posting timestamp,
  302. 11:00the person who posted it, the poster,
  303. 11:03what they commented on, and you can put
  304. 11:06any other data that you care about
  305. 11:07related to that comment. These are the
  306. 11:11attributes. So, one specific comment
  307. 11:14would be an entity, and then the
  308. 11:16attributes become the columns in the
  309. 11:18table. So, if we wanted to design this
  310. 11:21out as a table,
  311. 11:23it would look something like this. And
  312. 11:25I'm going to give it an ID column, the
  313. 11:28poster ID, the timestamp, I'll just
  314. 11:31write TS to keep it quick. And let's say
  315. 11:33these are videos, so we could say the
  316. 11:35video ID, what video we're commenting
  317. 11:38on. These will be the columns, and then
  318. 11:41we could give specific data, which would
  319. 11:44be the entities.
  320. 11:47So, ID 7,
  321. 11:50poster ID 32, timestamp,
  322. 11:53some really long number, video ID 72.
  323. 11:57And this would represent one comment. We
  324. 11:59could then add another one, and this
  325. 12:01person commented on the same video. So,
  326. 12:03in this situation, I'm just using
  327. 12:05numbers, and you might be thinking,
  328. 12:06well, where's the actual data? Well,
  329. 12:09that's one of the benefits of
  330. 12:10relationships. We can tie this to
  331. 12:14another table for videos.
  332. 12:19And then we'll have a video in there
  333. 12:20with the ID of 72.
  334. 12:23So that's how we know that this comment
  335. 12:25is associated with that video, and these
  336. 12:28are connected in the database using
  337. 12:30foreign keys. So we'll get into all of
  338. 12:32that. Then every single row is going to
  339. 12:35have an ID.
  340. 12:38That's how you can uniquely identify
  341. 12:40that row. So these do not reference
  342. 12:42another table. These describe that
  343. 12:44specific comment. So if you wanted to
  344. 12:47refer to a specific comment, you could
  345. 12:49say comment with the ID of nine, and
  346. 12:52that will give you that exact row. So to
  347. 12:54continue with some of the vocabulary, we
  348. 12:56say row, but you might also hear tuple,
  349. 12:59pretty common in more formal database
  350. 13:02design classes and stuff like that, or
  351. 13:04you might hear record.
  352. 13:05And this is basically just an entry in
  353. 13:07the database. For the columns, we'll
  354. 13:10have attributes
  355. 13:13or you'll often hear fields. Again,
  356. 13:16you're probably not going to get quizzed
  357. 13:17on these things, but you will run into
  358. 13:19these as you're studying database
  359. 13:20design, so it's good to be familiar with
  360. 13:22them. So why do we need to make these
  361. 13:23things random numbers that connect to
  362. 13:25other tables? And why do we have to
  363. 13:27split up the data across other tables?
  364. 13:30Well, it's all part of a system to build
  365. 13:32and protect data integrity.
  366. 13:35So data integrity is basically saying
  367. 13:38that the data is trustworthy, the
  368. 13:40existing data that's already there, and
  369. 13:42that the structure of the database will
  370. 13:44protect us from bad data or data that
  371. 13:47ruins our data integrity. So there is a
  372. 13:50few different classifications.
  373. 13:53So I'll just write the different options
  374. 13:55up here. These are the different angles
  375. 13:57that you want to think about protecting
  376. 13:59your data. So entity integrity is all
  377. 14:02about looking at a single row, one
  378. 14:04entity, and trusting that data, making
  379. 14:07sure that there's no duplicates, so that
  380. 14:09comment is not in that database
  381. 14:11mistakenly twice. Additionally, that row
  382. 14:13is dedicated to just one individual
  383. 14:15comment. We're not trying to cram
  384. 14:17multiple comments into a single row.
  385. 14:19This one's relatively easy to achieve
  386. 14:21with a couple of design decisions, which
  387. 14:23we'll talk about with relationships and
  388. 14:25normalization. Basically, this answers
  389. 14:27the question, can I look at a row and
  390. 14:29trust that data? Referential integrity,
  391. 14:31this goes into the relationships across
  392. 14:33tables with foreign keys. What we don't
  393. 14:36want is a pointer that points to
  394. 14:37nothing. So if 72 doesn't exist, that's
  395. 14:40a problem. There are certain situations
  396. 14:43where you do want that, but by default,
  397. 14:45you don't unless you explicitly do.
  398. 14:47Hopefully, that's not like super
  399. 14:49confusing. Basically, in general, we are
  400. 14:52going to want to always make sure that
  401. 14:56this is a strong connection
  402. 14:59and that we can't go in here and make
  403. 15:00any bogus data. So if we go in here and
  404. 15:02change this to 77 and 77 doesn't exist,
  405. 15:06well, the database should say, yo, that
  406. 15:08doesn't work. You need to actually pick
  407. 15:10a value that exists. Domain integrity,
  408. 15:13this is specific to the data that is
  409. 15:15stored in the database. Does it make
  410. 15:18sense for what you're trying to
  411. 15:19represent? This can be achieved a few
  412. 15:22different ways.
  413. 15:24Data types will get us the basics.
  414. 15:27Lookup tables,
  415. 15:29this allows us to choose from different
  416. 15:30options. You can also use things like
  417. 15:33enums.
  418. 15:34Then many databases will have check
  419. 15:36constraints so you can actually check
  420. 15:38the value, making sure it's not too big
  421. 15:41or too small. You can even have stored
  422. 15:44procedures or other gates that the data
  423. 15:46has to get through in order for it to
  424. 15:48get to the database.
  425. 15:50These are all things you can do at the
  426. 15:51database level, but oftentimes that
  427. 15:54domain integrity will be pushed up to
  428. 15:56some degree to the application level. So
  429. 15:58for example, if you are doing a marathon
  430. 16:03training app and you have the database
  431. 16:06and one of the questions on the
  432. 16:07introduction survey is, hey, what is
  433. 16:10your marathon time if you've done a
  434. 16:12marathon? And somebody goes in there and
  435. 16:14puts negative 71, something that makes
  436. 16:16no sense in that context. Well, that is
  437. 16:19validation that we could check for at
  438. 16:21the application level.
  439. 16:26So some things will delegate to the
  440. 16:28database through things like check
  441. 16:30constraints, but oftentimes you can just
  442. 16:32do it at the application level and use
  443. 16:34the database for the main things, which
  444. 16:36is entity integrity, referential
  445. 16:38integrity, data types,
  446. 16:41and lookup tables for certain
  447. 16:44situations. So that's how I would
  448. 16:46usually start until you need specific
  449. 16:48scenarios in the database adding in
  450. 16:50those check constraints or other gates
  451. 16:52for that data to make it to the
  452. 16:54database. This would probably only be
  453. 16:56the case if you have multiple locations
  454. 16:59accessing that database. So let's say
  455. 17:00you have another app that also uses that
  456. 17:04database.
  457. 17:06Well, now you'd have to copy that
  458. 17:08validation over twice. So you could move
  459. 17:10that validation to the database or to a
  460. 17:13database wrapper that checks anything
  461. 17:16that goes through to the database. So
  462. 17:18imagine your database here and then you
  463. 17:20write a small piece of software that
  464. 17:23does data validation and puts data in
  465. 17:25the database or gets data back out. And
  466. 17:28then you can connect to that from your
  467. 17:30backend.
  468. 17:31And then if you have two backends,
  469. 17:35they both have to go through that same
  470. 17:39gate. So yeah, what I'm trying to say
  471. 17:40here is there are often different ways
  472. 17:42to achieve similar results. You can do
  473. 17:44it at the application level in your
  474. 17:46backend with data validation. You can do
  475. 17:49it at the database level inside the
  476. 17:50database with things like check
  477. 17:52constraints or you could write a
  478. 17:54dedicated service to access the database
  479. 17:56that does just data validation, which
  480. 18:00could then be the gate for any of the
  481. 18:02backend software going through that
  482. 18:04database. Not trying to overcomplicate
  483. 18:06it. You don't need to understand all of
  484. 18:07the different scenarios because this
  485. 18:09isn't like a system design or backend
  486. 18:11engineering video. We're just wanting to
  487. 18:13learn about data integrity at this
  488. 18:15point.
  489. 18:16The main things that we're going to
  490. 18:17focus on are these two because those are
  491. 18:19the core of design. This domain stuff is
  492. 18:22basically specific to whatever you're
  493. 18:25doing, and that's what domain here
  494. 18:27means. So the actual data values
  495. 18:29themselves, and not so much the database
  496. 18:32structure. So this deals with the values
  497. 18:35of the data, and this deals more with
  498. 18:37the structure of the DB. So that's a
  499. 18:39pretty decent way to think about it. And
  500. 18:41another thing just to keep in your mind
  501. 18:43as you are going through building a
  502. 18:45database, you want to never have to be
  503. 18:48careful.
  504. 18:50This seems weird, right? Because like,
  505. 18:53wouldn't we want to be careful if we are
  506. 18:55working on a database? The answer is
  507. 18:57yes, you should be careful, but you
  508. 18:59should design a system that will be fine
  509. 19:02even if you're not being careful. So you
  510. 19:04shouldn't have to remember to make data
  511. 19:06connect properly or to only put certain
  512. 19:09values in the tables. That requires
  513. 19:11human effort, and being careful
  514. 19:14ultimately means someone will mess up.
  515. 19:17This is the same reason why you should
  516. 19:19avoid having production connection
  517. 19:21strings on your local system, cuz even
  518. 19:24if you're not touching them and you only
  519. 19:25use them when you need them, just by
  520. 19:27having that access, at some point you're
  521. 19:31going to mess something up. So this is
  522. 19:32why in larger corporations, there will
  523. 19:34be dedicated people to working with the
  524. 19:36database and protecting the database.
  525. 19:38And then if you're a developer, you have
  526. 19:39to basically get approved to even work
  527. 19:42with the database. It's because the
  528. 19:44database administrator's job is to set
  529. 19:47up the database, configure it, and also
  530. 19:49protect it. The more that we can force
  531. 19:51in our database structure, the better.
  532. 19:53All right, let's start designing some
  533. 19:54systems. As you build an intuition for
  534. 19:56database design, you don't have to start
  535. 19:58from scratch every single time. You can
  536. 20:00kind of just do it. But for this video,
  537. 20:03we're going to start with pure sentences
  538. 20:06of what user behavior we need to
  539. 20:07support, and then I'll show you how you
  540. 20:09can break those down into certain
  541. 20:11tables, attributes, and relationships.
  542. 20:13So let's just go through some examples
  543. 20:15for a video platform.
  544. 20:19The goal here is not to be comprehensive
  545. 20:22necessarily. You can always add in
  546. 20:24It's just that the more you think of up
  547. 20:26front, the easier time you're going to
  548. 20:28have building the system. So, what can a
  549. 20:31user do?
  550. 20:34A user can create a profile. We'll
  551. 20:39just say channel. A channel uploads
  552. 20:41videos.
  553. 20:44A user subscribes to channel.
  554. 20:48Hint hint.
  555. 20:52A user comments
  556. 20:55on a video.
  557. 20:57A comment can be a reply.
  558. 21:01A user can create playlists
  559. 21:04and add videos to the playlist.
  560. 21:09And then users can like or dislike. Now,
  561. 21:12I would just have to go through this
  562. 21:13list and identify any nouns and any
  563. 21:16verbs. Nouns will likely become tables.
  564. 21:19Verbs will become certain relationships
  565. 21:22across tables. And then any specific
  566. 21:24details about a noun can be an attribute
  567. 21:27or a column in that table. So, let's
  568. 21:30just go through this exercise. I know it
  569. 21:31seems kind of obnoxious or unnecessary,
  570. 21:34but let's just do it. We have a user. I
  571. 21:37will square the nouns and then underline
  572. 21:40actions. So, we have a channel.
  573. 21:44Uploads videos. So, uploads is an
  574. 21:47action. A user again subs to a channel.
  575. 21:53We have user comments
  576. 21:56on a video.
  577. 21:59Comments can be a reply. I guess that's
  578. 22:03a verb. Some of these you might need to
  579. 22:05like circle back to and think through,
  580. 22:07is a reply a noun or is it an action?
  581. 22:10So, any that you're unsure of, just put
  582. 22:12a mark by and you can circle back. So,
  583. 22:14again we have a user create playlists.
  584. 22:17We can add and playlist again.
  585. 22:20We can like and dislike. So, now we'll
  586. 22:23just get rid of any duplicates here.
  587. 22:25I'll just them pretty small over here
  588. 22:27and then we can analyze in more detail.
  589. 22:30So we have a user, we have channel, we
  590. 22:32have videos.
  591. 22:36I'm just doing nouns here if that wasn't
  592. 22:37obvious. We have comments and we have
  593. 22:40playlists.
  594. 22:42So likes and dislikes, that's a little
  595. 22:44bit more abstract, but that is going to
  596. 22:46be an entity because it's just
  597. 22:48describing a reaction. So we'll store
  598. 22:51reactions.
  599. 22:53And then these are examples of
  600. 22:56reactions. And then if there's any
  601. 22:58you're unsure of, such as a comment
  602. 23:00being a reply, is a reply a dedicated
  603. 23:02noun that should get its own table or is
  604. 23:05there some way that we can store this?
  605. 23:07Well, a reply doesn't have any
  606. 23:08additional information compared to a
  607. 23:10comment. So a comment can just be marked
  608. 23:13as a reply to another comment or not a
  609. 23:16reply to another comment. It's actually
  610. 23:18an attribute of a comment. So for me, a
  611. 23:21reply is not going to get a dedicated
  612. 23:23table. I'll just have it as an attribute
  613. 23:25within that comment table. So hopefully
  614. 23:28that makes sense for that one. And
  615. 23:29there's a reason why this one was
  616. 23:31special and I called it out because I
  617. 23:33mentioned that verbs will become their
  618. 23:36own relationships. And this is a verb.
  619. 23:39It's just that the relationship is with
  620. 23:41itself. A comment replies to a comment.
  621. 23:44So I think that might be why it's a
  622. 23:46little bit confusing initially, but the
  623. 23:49system that I described still makes
  624. 23:50sense.
  625. 23:51So now let's go through the verbs and
  626. 23:52you'll be able to see that. So a user
  627. 23:55can create a channel. Any of these verbs
  628. 23:57will be a relationship between those
  629. 23:59tables. So a relationship between a user
  630. 24:03and a channel. There will be a
  631. 24:05relationship between a channel and a
  632. 24:08video that is uploaded to the website.
  633. 24:11There will be a relationship between the
  634. 24:13user and a channel that the user
  635. 24:16subscribed to. There will be a
  636. 24:17relationship between the user and the
  637. 24:21video that the user comments on. There
  638. 24:24will be a relationship again with
  639. 24:26comments pointing to other comments.
  640. 24:28There be a relationship between users
  641. 24:30and playlists, and then a relationship
  642. 24:33between videos and playlists. And then
  643. 24:35it comes down to just knowing what type
  644. 24:37of relationship that is needed. Is it a
  645. 24:39one-to-one, a one-to-many, or a
  646. 24:41many-to-many? So we'll get into those in
  647. 24:43this video. Let's learn the different
  648. 24:45types of relationships, and then we can
  649. 24:46apply that to what we've learned here.
  650. 24:48So let's talk about database
  651. 24:50relationships.
  652. 24:53One of the most important topics. And
  653. 24:55when you get even into database
  654. 24:57normalization and some of these other
  655. 24:59popular things with database design,
  656. 25:01it's all built on database
  657. 25:02relationships. So if you learn these,
  658. 25:05going through the normalization
  659. 25:06principles will be super, super easy. So
  660. 25:08there's three classifications here. We
  661. 25:10have one to one, one to many,
  662. 25:17and many to many.
  663. 25:21You might see these as 1 1 1 m and m n.
  664. 25:29Let's go through each one of these, and
  665. 25:30let me tell you, the last one will
  666. 25:32surprise you. A one-to-one relationship
  667. 25:35is when two entities are exclusive. And
  668. 25:37this is often a design choice, so it's
  669. 25:40not always there's a correct answer. And
  670. 25:42that's a common misunderstanding. I
  671. 25:44actually get a lot of comments people
  672. 25:45saying, hey, I need help with this
  673. 25:48database design. Should it be this way
  674. 25:49or this way? What's the correct answer?
  675. 25:52And it comes down to the business choice
  676. 25:54of which one you want. So there's not a
  677. 25:57correct answer all of the time for
  678. 25:59design choices. What exactly do I mean?
  679. 26:01Let's go through a quick example. So in
  680. 26:03the previous list, we had user and we
  681. 26:07had channel. So you could say these
  682. 26:10rules apply. A user has at most
  683. 26:14one channel.
  684. 26:16And the other way as well, a channel
  685. 26:21can be owned by at most one user. This
  686. 26:27is a classic one-to-one relationship. A
  687. 26:29user cannot own multiple channels, and a
  688. 26:31channel cannot be owned by multiple
  689. 26:33users. So, is this design correct? That
  690. 26:36comes down to the business choice. Do we
  691. 26:39want users to be able to create multiple
  692. 26:41channels? Do we want to make it so that
  693. 26:43a channel can be owned by multiple
  694. 26:45users? Often times, platforms do allow
  695. 26:47you to create multiple instances of
  696. 26:50whatever it is you're creating. In that
  697. 26:52situation, it would no longer be a
  698. 26:54one-to-one relationship. So, these
  699. 26:56things are exclusive. They're tied to
  700. 26:57each other. So, there are two ways to
  701. 27:00design
  702. 27:03a one-to-one relationship.
  703. 27:07Since the values are coupled, you could
  704. 27:08just use a column.
  705. 27:11That's the first approach. I'll show you
  706. 27:12what that looks like. I'll write the
  707. 27:13second approach down here. And that's to
  708. 27:16use a separate table.
  709. 27:18You don't always need a separate table.
  710. 27:20So, let's create a user.
  711. 27:25We'll say Caleb at CalebCurry.com. There
  712. 27:28can be any data about this user as well,
  713. 27:31such as when they signed up. So, we'll
  714. 27:32say created at. We might have their
  715. 27:35birth date or their age for any kind of
  716. 27:38content verification. We might even have
  717. 27:41something like their hashed password.
  718. 27:45Then, we could just have an attribute on
  719. 27:47here which is their channel
  720. 27:52handle.
  721. 27:55Let's say, for example, coding
  722. 27:57breakthrough.
  723. 27:58So, this column is enough to make that
  724. 28:01association in the database. So, that's
  725. 28:04the first way and it's the default way I
  726. 28:06would think about one-to-one
  727. 28:07relationships. There are situations
  728. 28:09though when you want to split it out
  729. 28:10into a separate table. Usually, that
  730. 28:12will be because of some certain reasons.
  731. 28:14So, if you have transitive dependencies,
  732. 28:17which are basically attributes about the
  733. 28:20channel, you can kind of visualize it as
  734. 28:23sub data here. Things about the channel.
  735. 28:28That would be a reason why you might
  736. 28:30want to use a separate table. And the
  737. 28:32other reason would be separation. So I
  738. 28:35see this a lot where we'll have a user
  739. 28:37profile or the user profile's personal
  740. 28:40information like addresses, and that's
  741. 28:42stored in a completely separate table
  742. 28:44just for that data isolation, as well as
  743. 28:47thinning out the primary row. So that
  744. 28:50way we don't have a ton of information
  745. 28:51in that row, we just grab what is needed
  746. 28:54for that user account. So for example,
  747. 28:56let's say we had the address here, we
  748. 28:58had their credit card number,
  749. 29:01and let's say we had their social
  750. 29:02security number or some other sensitive
  751. 29:04data. Well, instead of storing all of
  752. 29:06this within that one table, we could
  753. 29:08take this, separate it out, and then
  754. 29:11just connect it back to that original
  755. 29:13table. So you're still using a
  756. 29:15one-to-one relationship, it's just split
  757. 29:16across two tables. So wasn't trying to
  758. 29:19get too off topic here with the
  759. 29:20sensitive information stuff, but that's
  760. 29:23the other common approach you'll see
  761. 29:25when it comes to one-to-one
  762. 29:26relationships. It's a true relationship
  763. 29:28across tables, but it's still a
  764. 29:30one-to-one association. In theory, you
  765. 29:33could design it just as columns in the
  766. 29:35same table, but it's split out for some
  767. 29:37reason. So just to review that, the two
  768. 29:39ways to design a one-to-one relationship
  769. 29:41is to use a column or use a separate
  770. 29:43table and then connect it with a foreign
  771. 29:45key. And the reason you would want to do
  772. 29:47two separate tables is transitive
  773. 29:49dependencies or for separation due to
  774. 29:52privacy or the size of data. Let's now
  775. 29:54talk about a one-to-many relationship
  776. 29:57because it's just a small change. And in
  777. 29:59fact, you can go from a one-to-one
  778. 30:01relationship to a one-to-many
  779. 30:04relationship very easily if those tables
  780. 30:07are split out like we had just
  781. 30:09explained, splitting out this channel
  782. 30:11section into its own table. So let's
  783. 30:13talk about that design. So let's first
  784. 30:15start by defining it as a one-to-one
  785. 30:18relationship
  786. 30:21across tables.
  787. 30:25So we have users,
  788. 30:31and then we have channels.
  789. 30:35And And within channels, we'll have an
  790. 30:36ID
  791. 30:38and then the handle. And then over here
  792. 30:41on the user, we'll have a user ID, let's
  793. 30:43say 32. And then we'll have a reference
  794. 30:46to that channel. So this is the foreign
  795. 30:48key. It points from this table to this
  796. 30:51table. However, with a one to one
  797. 30:53relationship, it can point either way.
  798. 30:55So instead of the user pointing to the
  799. 30:57channel, you can have the channel
  800. 31:00pointing to the user. Doesn't really
  801. 31:01matter which way because they're
  802. 31:03exclusive. However, if we do want to
  803. 31:05switch to a one to many relationship
  804. 31:07later, then we can design it easier by
  805. 31:09where we put that key. So what I'm going
  806. 31:11to do, instead of pointing from the
  807. 31:13users to the channels, I'm
  808. 31:16going to go the other way.
  809. 31:21So in that situation, we do not need to
  810. 31:23reference the channels in the users
  811. 31:25table. So we might just have ID and
  812. 31:28email. And then over in the channels
  813. 31:31table,
  814. 31:33we would add in a user column saying who
  815. 31:37it's associated with.
  816. 31:40So that would be 32. And this points
  817. 31:43back to this row over here.
  818. 31:47So that is a foreign key. Now, when we
  819. 31:50get into the details of foreign keys,
  820. 31:52you can learn that you can make a
  821. 31:53foreign key unique. So that's one of the
  822. 31:56attributes we're going to put on this.
  823. 32:02And when we label that unique, it
  824. 32:04prevents us from using 32 in this table
  825. 32:07again. So if we created another channel
  826. 32:11here, let's say channel with the ID two,
  827. 32:14and we'll call this one Code Break.
  828. 32:17And then we tried to say that this was
  829. 32:19owned by that same user, this would be
  830. 32:23blocked by the database. That's what
  831. 32:25that unique constraint is going to do.
  832. 32:28So we have a one to one relationship
  833. 32:31over two tables where we have the
  834. 32:33foreign key in the channels pointing
  835. 32:35back to the users table. That unique
  836. 32:38prevents us from inserting multiple rows
  837. 32:42with repeating values. So this is not
  838. 32:44allowed. So the switch to a one to many
  839. 32:47then is very simple. All we have to do
  840. 32:53is change that
  841. 32:56and remove
  842. 32:59that unique constraint.
  843. 33:03And now this is allowed by the database
  844. 33:06and now Caleb owns two channels, Code
  845. 33:09Breakthrough and Code Break. So it
  846. 33:11basically looks like this.
  847. 33:14Here's me all happy, ID of 32, and we
  848. 33:18have two channels. Code Breakthrough,
  849. 33:20which is entirely too long. And then
  850. 33:24Code Break, which I kind of like cuz it
  851. 33:27describes my code. It's always broken.
  852. 33:29Both of these channels
  853. 33:32can have their own videos and
  854. 33:35they are both owned by that same person.
  855. 33:40So that is a one to many. So the
  856. 33:42difference between a one to one on two
  857. 33:45tables and a one to many also on two
  858. 33:48tables is just the unique constraint on
  859. 33:51the foreign key. So just to make sure
  860. 33:53you didn't miss that, one to one
  861. 33:57unique foreign key.
  862. 34:00One to many
  863. 34:03not unique foreign key.
  864. 34:06It's really that simple and which one
  865. 34:07you want to use comes down to the choice
  866. 34:09of how you want to design your app. If
  867. 34:12you want people to be able to create
  868. 34:14multiple channels, then you would do it
  869. 34:16as a one to many not using unique. If
  870. 34:18you wanted to prevent multiple channels,
  871. 34:20then you would use unique. All right,
  872. 34:22are you ready for things to get a little
  873. 34:23crazy? What if we wanted a user to be
  874. 34:26able to create multiple channels, but
  875. 34:29that channel can be managed and owned by
  876. 34:32multiple users? Oh no, that's a problem.
  877. 34:35Now we have an issue. Let's say we have
  878. 34:37another user over here,
  879. 34:40[email protected].
  880. 34:42So we go in here and we add a new row,
  881. 34:44and let's say he's sharing this code
  882. 34:46break channel.
  883. 34:52And this time it's pointing back to 47.
  884. 34:58This is the first approach people often
  885. 35:02take, but this is wrong and bad. Can you
  886. 35:05guess why this is wrong and bad? Great
  887. 35:10job. Exactly. We have repeating data
  888. 35:13here. Additionally, we have two IDs for
  889. 35:16the same channel. That's kind of a big
  890. 35:19problem here. So when we want to do a
  891. 35:22many to many,
  892. 35:25we have to use a third table, which is
  893. 35:27often called a junction table or
  894. 35:29intermediary table or a table that you
  895. 35:32put between two other tables to resolve
  896. 35:33a many to many relationship by splitting
  897. 35:35it up into two one to many
  898. 35:37relationships. That's usually the name I
  899. 35:39go with. So I'm going to erase some of
  900. 35:41this here. But first I wanted to draw
  901. 35:44the visual of what this would look like.
  902. 35:46So we have Caleb here,
  903. 35:48and then we have James here.
  904. 35:53And he also is going to be the owner of
  905. 35:57this channel. So we need to introduce a
  906. 35:59table
  907. 36:01to resolve this tension. So we have
  908. 36:03users and we have channels. And then by
  909. 36:06convention, it's kind of stupid, but
  910. 36:09you'll just see the combination of
  911. 36:10these, such as user channels.
  912. 36:13That's typically what I'll do, where
  913. 36:14I'll make one of them singular and one
  914. 36:16of them plural, but you'll see
  915. 36:17variations. So especially even between
  916. 36:20database systems, the conventions will
  917. 36:22sometimes vary.
  918. 36:24But one thing that's pretty common is
  919. 36:25using the underscores for multiple words
  920. 36:28as SQL is often going to lowercase
  921. 36:31anything you type. So to save on typing
  922. 36:33without having to use quotes everywhere,
  923. 36:35you can use underscores for the tables.
  924. 36:38And this table is going to be pretty
  925. 36:40simple. We're just going to have the
  926. 36:41user on one side and then the channel on
  927. 36:44the other. So, let's go over to the
  928. 36:46broken part here. We will put the user
  929. 36:49on one side, 32 and then the channel on
  930. 36:54the other.
  931. 36:55Again, 47 and then the channel on the
  932. 36:59other, which this was incorrect, so
  933. 37:00we'll want to use the actual correct
  934. 37:03value here, too. This allows us to get
  935. 37:05rid of this row here, which is corrupt,
  936. 37:11and just have the single source of truth
  937. 37:13row here. It will no longer store the
  938. 37:17user owner in this channels table
  939. 37:19because all of that's going to be
  940. 37:20managed right here. Which means we also
  941. 37:22need to add this first one in here. So,
  942. 37:25that was user 32 owning channel with the
  943. 37:28ID of one. So, that allows us to do the
  944. 37:32many-to-many relationship. We're
  945. 37:33basically jumping through this other
  946. 37:35table. And if you want, you can draw it
  947. 37:38out. So, let's do this top one first.
  948. 37:41The 32 is over here, and then the one
  949. 37:45goes to here.
  950. 37:47The second one here, 47
  951. 37:49goes to here, and the two goes to here.
  952. 37:53Then
  953. 37:54we'll have another one from 32
  954. 38:00which goes to here as well. Wow, the
  955. 38:03arrows probably just made it way more
  956. 38:04confusing. I'm sorry. Now, the cool
  957. 38:06thing here with the many-to-many
  958. 38:07relationship, we now have the perfect
  959. 38:09home for any attributes that describe
  960. 38:13the relationship between data. So, the
  961. 38:15first initial design might make James
  962. 38:18and Caleb co-owner of the channel. But
  963. 38:20realistically, you might want to have
  964. 38:22users that have restricted permissions.
  965. 38:25Maybe they are an editor or video poster
  966. 38:28or maybe they can manage comments, but
  967. 38:29they can't go in and delete stuff. Those
  968. 38:32tiers describe the relationship between
  969. 38:36a user and that channel. So, what we
  970. 38:38could do
  971. 38:41is we could scoot this over.
  972. 38:45And notice how I made the box enough.
  973. 38:48Thinking ahead. Usually don't do that.
  974. 38:50And here you can describe that
  975. 38:53relationship between the user and the
  976. 38:55channel. So for example, we could say
  977. 38:57owner, and then maybe manager, which is
  978. 39:00slightly less permissions, and then
  979. 39:03owner.
  980. 39:04So now James is basically working with
  981. 39:07that channel, but he's not the direct
  982. 39:09owner of that channel. So that's one of
  983. 39:11the cool things with many to many
  984. 39:12relationships. You can add in attributes
  985. 39:15here that describe that junction. You
  986. 39:17might also have timestamps and other
  987. 39:18things such as when that was
  988. 39:20established. So we haven't really gotten
  989. 39:21into keys a whole lot, but you've
  990. 39:23noticed I've been using these numbers.
  991. 39:25Well, on tables like users or channels,
  992. 39:27these will be unique, meaning you can't
  993. 39:30reuse the same user ID or the same
  994. 39:32channel ID.
  995. 39:33But for user channels, you can have
  996. 39:35repeats for any specific column. You
  997. 39:39just can't have the combination
  998. 39:40repeated. So you can think of bundling
  999. 39:43these together.
  1000. 39:46And the combination has to be unique.
  1001. 39:49So an example of something that would
  1002. 39:51not work would be 32 two
  1003. 39:55manager.
  1004. 39:57Because in that situation, we would be
  1005. 39:58the owner and the manager. We have data
  1006. 40:00that doesn't mix, and that would throw
  1007. 40:02the data integrity into question. So the
  1008. 40:05constraints will force data integrity by
  1009. 40:08saying the combination of these have to
  1010. 40:10be unique. So there's really multiple
  1011. 40:12constraints here. We have the unique,
  1012. 40:14and then we have two x foreign keys. So
  1013. 40:18here's a foreign key. Here's a foreign
  1014. 40:21key, and then here's a unique
  1015. 40:23constraint. Now, as I mentioned, once
  1016. 40:25you build a stronger intuition for
  1017. 40:27database design, you'll start to just
  1018. 40:29notice many to many relationships. So
  1019. 40:31some of the examples we had earlier fit
  1020. 40:33nicely into a many to many relationship.
  1021. 40:36Let me go through one example with you.
  1022. 40:37So we talked about videos being added to
  1023. 40:40a playlist.
  1024. 40:43So we have this verb, so we know there's
  1025. 40:45some relationship here. We just have to
  1026. 40:47decide is it one to one, one to many, or
  1027. 40:49many to many. The way you do that is you
  1028. 40:51take one of the entities, videos,
  1029. 40:55and you ask, can this be added to many
  1030. 40:58playlists?
  1031. 41:00And then you ask the same thing the
  1032. 41:02other direction. So, playlist.
  1033. 41:07Can many videos
  1034. 41:10be added to the playlist?
  1035. 41:15And the obvious answer here is also
  1036. 41:18going to be yes. If you get two yeses,
  1037. 41:21then you have a many to many
  1038. 41:22relationship. If you get one yes, then
  1039. 41:24you have a one to many relationship
  1040. 41:26where the child, the table that has the
  1041. 41:28foreign key, is the many side. That
  1042. 41:30doesn't apply here, so let's just focus
  1043. 41:32on the many to many here. This is what
  1044. 41:34it'll end up looking like. Following
  1045. 41:36that same pattern, we'll have videos and
  1046. 41:39then we'll have playlists.
  1047. 41:42And I don't like video playlists, so I
  1048. 41:45usually like to word it
  1049. 41:48left to right. So, I'm going to
  1050. 41:49rearrange this. It's the same exact
  1051. 41:51idea. I'm just
  1052. 41:53being weird with the wording here. So,
  1053. 41:55we have playlists
  1054. 41:58and videos, and then we have
  1055. 42:03playlist videos. And this table here
  1056. 42:05acts purely just as a way to associate
  1057. 42:08these two, but as I mentioned, you can
  1058. 42:10often add in other attributes in there
  1059. 42:12and then it grows up into its own
  1060. 42:14entity. So, playlist videos can also
  1061. 42:17contain the position of a video within a
  1062. 42:20playlist or the ordering or any other
  1063. 42:23attributes that describe that
  1064. 42:25connection. So, let's just go through a
  1065. 42:27quick example. We have two playlists. We
  1066. 42:29have two videos. Within the playlist, we
  1067. 42:32add the first video and then we add the
  1068. 42:36second video. Dude, I always draw up and
  1069. 42:38it makes me sick. Let's add a couple
  1070. 42:40more values here just so we can see
  1071. 42:42better. And
  1072. 42:44we'll
  1073. 42:47say playlist three has video four
  1074. 42:51and video one. And we'll also put in
  1075. 42:54here video five. So in this example, we
  1076. 42:58have five playlists. Two of the
  1077. 43:01playlists actually have videos in it.
  1078. 43:04The first one here has two videos. The
  1079. 43:07second one here has three videos. So
  1080. 43:09I'll draw the playlist like this.
  1081. 43:13One with two videos
  1082. 43:18and the other one with three videos.
  1083. 43:21Now, the ordering you could put as
  1084. 43:24another attribute. There's a downside to
  1085. 43:26that in that if you move data around,
  1086. 43:29you're going to have to do an update
  1087. 43:31across that playlist's videos. But it's
  1088. 43:34a pretty simple design and a small
  1089. 43:35tradeoff. So for example, you could have
  1090. 43:37position one, position two, position
  1091. 43:41one, two, and three. Don't necessarily
  1092. 43:44have to be sequential. But if you're
  1093. 43:47adding the rows, it's probably naturally
  1094. 43:49going to increment like that. And that
  1095. 43:51basically just describes where in the
  1096. 43:54playlist
  1097. 43:56those videos go. So that's one example
  1098. 43:59of how you could do basic ordering. And
  1099. 44:01then with a rearrangement, you can
  1100. 44:02recalculate the numbers for those rows.
  1101. 44:05There are various other approaches to
  1102. 44:07ordering, so I'm not saying that's the
  1103. 44:09only way you should do it, but in my
  1104. 44:11opinion, it's one of the simplest to
  1105. 44:12wrap your mind around. So those are
  1106. 44:14junction tables. You might see some
  1107. 44:16other approaches to achieving similar
  1108. 44:18results.
  1109. 44:20So for example, instead of having three
  1110. 44:22tables, you could just have an array.
  1111. 44:25And that is a formal type in some
  1112. 44:27databases, but you lose a lot of
  1113. 44:29benefits when you do that as well.
  1114. 44:31There's tradeoffs. It's going to be
  1115. 44:33simpler. It's going to probably be
  1116. 44:34faster. You don't have to do as many
  1117. 44:36joins, but there are downsides as well,
  1118. 44:38such as losing that referential
  1119. 44:40integrity. So instead of having this
  1120. 44:42table all together, you could just
  1121. 44:45denormalize this, which is the process
  1122. 44:47of going into fewer tables. And one of
  1123. 44:49the attributes on here would just be the
  1124. 44:51videos that the playlist has and the
  1125. 44:55position within that array would be the
  1126. 44:57ordering. The major downside here is you
  1127. 45:00don't have the enforcement of this
  1128. 45:03connection. There's no foreign key. Now,
  1129. 45:06maybe you could do something to get that
  1130. 45:09behavior, such as database triggers, but
  1131. 45:12the three table structure uses native
  1132. 45:15foreign keys and is probably the proper
  1133. 45:18way of doing it. There's always
  1134. 45:19tradeoffs though. Some people will want
  1135. 45:22to have less data fragmenting across
  1136. 45:24tables and they'll give up some
  1137. 45:27integrity in that process if they can
  1138. 45:29enforce that integrity some other way.
  1139. 45:31The nice benefit here is that the
  1140. 45:32position of the ID in the array
  1141. 45:34determines that order. Now, I'm not
  1142. 45:36saying you should do that instead, I'm
  1143. 45:38just giving some options that you might
  1144. 45:40see. So some of the other connections
  1145. 45:43that we had in that large list was
  1146. 45:45between a channel and the channel
  1147. 45:47uploading videos. So basically we're
  1148. 45:50connecting a video back to a channel
  1149. 45:52instead of a user, which does better
  1150. 45:54support channels that are owned by
  1151. 45:56multiple users.
  1152. 45:58This would be a one to many
  1153. 46:01because a channel can have multiple
  1154. 46:03videos, but a specific video cannot be
  1155. 46:05owned by multiple channels. That's
  1156. 46:07definitely one that just makes sense as
  1157. 46:09one to many. You wouldn't want to do
  1158. 46:10that as a many to many. However, you
  1159. 46:13could consider things like
  1160. 46:14collaborations.
  1161. 46:16So maybe
  1162. 46:19you do want to consider different
  1163. 46:20options. But you could maybe have a
  1164. 46:22video posted by a channel and then tag
  1165. 46:24some other channel. Kind of going off
  1166. 46:26the rails here. By default, I would do
  1167. 46:27one to many for this. Then we had
  1168. 46:29subscriptions. So a user and channel.
  1169. 46:33This is a different connection than a
  1170. 46:35user creating a channel. It's a user
  1171. 46:36subscribing to a channel, which again is
  1172. 46:40going to be many to many. A user
  1173. 46:43commenting on a video.
  1174. 46:45So this is going to be a relationship
  1175. 46:47between the user and the video. And this
  1176. 46:50is going to be many to many. Now, when
  1177. 46:54you see a many to many, you can go with
  1178. 46:56that default junction table name that I
  1179. 46:59described, where we take the two
  1180. 47:01different tables and combine the word,
  1181. 47:03or you can give a custom name specific
  1182. 47:05to that relationship. So if this is a
  1183. 47:08user commenting on a video, rather than
  1184. 47:11doing user videos, you could just say
  1185. 47:13comments.
  1186. 47:18And then what was this one? I think
  1187. 47:19subscriptions.
  1188. 47:22The next one was a user being able to
  1189. 47:24create a playlist.
  1190. 47:27This would be a one to many because the
  1191. 47:31playlist is owned by one user. We
  1192. 47:33already talked about adding videos to
  1193. 47:35playlists. And then the last one I think
  1194. 47:37that we had in that large list was likes
  1195. 47:39and dislikes, which I would just say
  1196. 47:41that is a combination between the user
  1197. 47:46and the video, but I would give it a
  1198. 47:48specific title
  1199. 47:51uh reactions.
  1200. 47:54So our V1
  1201. 47:57of our database design,
  1202. 48:00the user, channel, videos, comments,
  1203. 48:05subscriptions,
  1204. 48:07reactions.
  1205. 48:09If we did the multiple channel
  1206. 48:10ownership, we would need to add that
  1207. 48:12table in there. And I think that's it.
  1208. 48:14I'm trying to think if there's any other
  1209. 48:16junction tables that I missed, but we
  1210. 48:18got these ones.
  1211. 48:20And we're going to go with a one to many
  1212. 48:22for the user creating an account and
  1213. 48:25setting up a channel. So we don't need a
  1214. 48:27junction table for that. And then
  1215. 48:29comments were just a one to one
  1216. 48:30attribute replying to another comment.
  1217. 48:33There might be other tables or
  1218. 48:35interactions that we're not considering,
  1219. 48:37but obviously, if you're going to build
  1220. 48:39something like YouTube, there's going to
  1221. 48:41be a lot more than just this right here.
  1222. 48:43So let's talk about one last thing when
  1223. 48:46it comes to relationships.
  1224. 48:49And when it get a little freaky, you can
  1225. 48:51have relationships that go three way or
  1226. 48:55more. We talked briefly about channels
  1227. 48:57being managed by multiple users. So we
  1228. 49:00have that disconnect between the user
  1229. 49:01and the channel. And then I said you
  1230. 49:03could add in any attributes such as the
  1231. 49:06permission level for that user. But this
  1232. 49:09could be a situation where three keys
  1233. 49:12could be useful.
  1234. 49:14So splitting this up like so, where this
  1235. 49:18is dedicated to the user.
  1236. 49:21This column is dedicated to the channel
  1237. 49:26table.
  1238. 49:28And this column, instead of just putting
  1239. 49:31the permission level there inline as a
  1240. 49:35string or an enum value, we could
  1241. 49:38reference another table. We'll call it
  1242. 49:41the permissions table. And here you
  1243. 49:43would define the possible permission
  1244. 49:45levels.
  1245. 49:48So one might be owner.
  1246. 49:52Two is admin.
  1247. 49:56Three is editor. So let's say we have
  1248. 50:00user one here,
  1249. 50:03channel one here. So we could have a row
  1250. 50:05like one, one,
  1251. 50:09and two. So what this means is we have
  1252. 50:12the user ID,
  1253. 50:17channel ID, and the permission ID. So
  1254. 50:21the user with the user ID of one is an
  1255. 50:25admin of the channel with the ID of one.
  1256. 50:29In this situation, you can use a unique
  1257. 50:32constraint across three columns. So
  1258. 50:37across user ID,
  1259. 50:42channel ID, and permission ID. Yeah.
  1260. 50:45Totally didn't write uphill or anything.
  1261. 50:47And we'll get into keys, but it's
  1262. 50:49possible just to create this as the
  1263. 50:50primary key. But usually I like to have
  1264. 50:53an ID even for these junction tables.
  1265. 50:56But again, let's get into keys in a few
  1266. 50:59minutes, okay? Calm down. Now, something
  1267. 51:01you should be aware of is what this
  1268. 51:02structure does is it allows a user to
  1269. 51:05have multiple permissions because we
  1270. 51:08could go in here and say 1 1 3.
  1271. 51:12So in that situation, let's say this is
  1272. 51:15Caleb. Caleb is an admin and an editor.
  1273. 51:20That can work if these permissions are
  1274. 51:23not exclusive. So, you know, maybe you
  1275. 51:25can be the editor and the manager, and
  1276. 51:27the manager can manage comments and
  1277. 51:29stuff like that. The editor can manage
  1278. 51:31videos, and you could have both of those
  1279. 51:33independently. Again, this often comes
  1280. 51:35down to just the design of the app. If
  1281. 51:37you don't want somebody to be able to
  1282. 51:39choose multiple of these, then you would
  1283. 51:41just remove the permission ID from the
  1284. 51:45key, and then you would still have this
  1285. 51:46as a foreign key, but you couldn't
  1286. 51:49repeat the same user ID and channel, so
  1287. 51:51you would only be allowed to have one
  1288. 51:53permission ID. So we're getting into the
  1289. 51:54details of multiple keys and stuff, but
  1290. 51:57I just want you to have some intuition
  1291. 51:59about how changing what you put in the
  1292. 52:02constraint affects the way the database
  1293. 52:04functions, determining whether things
  1294. 52:06can go together or can't go together or
  1295. 52:08if things have to be unique and so
  1296. 52:10forth. It all comes down to just
  1297. 52:11designing a few constraints. Now, this
  1298. 52:14approach, we talked about it briefly
  1299. 52:16earlier when I was talking about channel
  1300. 52:18permissions, but I didn't talk about
  1301. 52:20this separate table. This idea is called
  1302. 52:23a lookup table.
  1303. 52:26And this is basically a universal way to
  1304. 52:30create an enum where you can choose from
  1305. 52:33one of a few possible values.
  1306. 52:37But it's very easy to, with a lookup
  1307. 52:40table,
  1308. 52:44update the name
  1309. 52:47or any other attributes. So if we wanted
  1310. 52:49to change it from editor to content
  1311. 52:52manager or whatever, we can just change
  1312. 52:54that row.
  1313. 52:55We don't have to go change a string in a
  1314. 52:57hundred different places, and we don't
  1315. 52:59have to change a type if we're working
  1316. 53:00with enums. And we can easily add more
  1317. 53:04in the same way just by adding a new row
  1318. 53:08here. So lookup tables are basically
  1319. 53:11one-to-many relationships used to
  1320. 53:13reference some value and then you can
  1321. 53:15put any information in additional
  1322. 53:18columns describing that value if needed.
  1323. 53:21So that's all I have on relationships.
  1324. 53:25So up next, we're going to talk about
  1325. 53:26primary keys and foreign keys. A key is
  1326. 53:29basically how we tell one row apart from
  1327. 53:32another row.
  1328. 53:33There needs to be something unique about
  1329. 53:35that row so we can talk about that row
  1330. 53:37specifically. Every table within your
  1331. 53:40database needs to have a primary key.
  1332. 53:43When you set a column as the primary
  1333. 53:45key, it's going to be indexed, meaning
  1334. 53:48we can access by that key very quickly,
  1335. 53:50and it will be unique, so you cannot use
  1336. 53:52that same primary key value again for
  1337. 53:55that table. So if you have a comment
  1338. 53:57with the ID of 300, you're not going to
  1339. 53:59be able to create another comment with
  1340. 54:01that same ID. So the key should be
  1341. 54:04unique, but it should also never change.
  1342. 54:06So you should never have a situation
  1343. 54:08where the comment that had the ID 300
  1344. 54:11changes to ID 305. The other major thing
  1345. 54:14is that every row needs to have a value
  1346. 54:17for that key. It cannot be null. So if
  1347. 54:20we haven't defined this yet, null just
  1348. 54:22means missing a value. Key columns
  1349. 54:24cannot be null. Every single row has to
  1350. 54:26have a value. They all have to be
  1351. 54:28unique, and they cannot change.
  1352. 54:30So those are the three major rules with
  1353. 54:32primary keys. That's the key that you
  1354. 54:35set for a table. There are also foreign
  1355. 54:37keys, which we'll talk about in detail
  1356. 54:39as well. These reference values from
  1357. 54:41another table. Often, it's that table's
  1358. 54:44primary key, but a column defined as a
  1359. 54:46foreign key does not by default have to
  1360. 54:49be unique. It can change and it can be
  1361. 54:53null. So the rules with keys do not
  1362. 54:56apply to foreign keys. They're just
  1363. 54:58there to make that connection and to
  1364. 55:00define what happens if you try to break
  1365. 55:02that connection or delete a row that
  1366. 55:04other rows depend on. So that's the
  1367. 55:06overview of what you should know going
  1368. 55:07into this next section. I talk about a
  1369. 55:09lot of different options and different
  1370. 55:11terminology. Don't let it overwhelm you.
  1371. 55:13The main thing you need to understand is
  1372. 55:15every row has a key, and if you want to
  1373. 55:16reference a row from another table, you
  1374. 55:18use a foreign key. So a key is a way to
  1375. 55:20uniquely identify a row. Now, up until
  1376. 55:23this point, we have just been using IDs.
  1377. 55:26This is a very common approach, and this
  1378. 55:29is an example of a surrogate key. This
  1379. 55:31is something that's made up and has no
  1380. 55:34real world value. So it's not like a
  1381. 55:36social security number or an email
  1382. 55:38address or anything that actually has
  1383. 55:41meaning outside of the database. This is
  1384. 55:44for the database to use only. We don't
  1385. 55:46expose it to users. So you probably want
  1386. 55:48to be familiar with that term or you
  1387. 55:53will sometimes see natural keys which
  1388. 55:57are actually meaningful values such as
  1389. 56:00usernames, emails, or other unique
  1390. 56:04things about that row. So which one do
  1391. 56:06you choose? Well, I think it's a good
  1392. 56:08idea to design your tables such that
  1393. 56:11they can have a natural key, but I still
  1394. 56:14prefer the surrogate keys. That's my
  1395. 56:16personal opinion. I don't think you
  1396. 56:18should entangle real world data with
  1397. 56:21keys that are designed to make
  1398. 56:23connections within databases and
  1399. 56:25reference specific rows. So I just keep
  1400. 56:28those two ideas separate and give
  1401. 56:29everything a key. I even do this for
  1402. 56:33junction tables. So for example, if your
  1403. 56:36table just looks like this, where it's
  1404. 56:37just connecting
  1405. 56:40IDs
  1406. 56:42like so. Well, I will often give it a
  1407. 56:45key of its own
  1408. 56:49so we can reference that connection with
  1409. 56:52a single column.
  1410. 56:54And then for these combinations here, I
  1411. 56:56would just label them as unique
  1412. 57:01rather than making them the actual key.
  1413. 57:03But it is possible to make two columns
  1414. 57:07the key.
  1415. 57:08That's known as a composite or compound
  1416. 57:10key. The compound key is a key that is
  1417. 57:13made up of multiple keys, whereas a
  1418. 57:16composite key is a key that's made up of
  1419. 57:18multiple things, but not every single
  1420. 57:20one of those things has to be a key.
  1421. 57:22This is kind of splitting hairs. It's
  1422. 57:24really the same idea. We have a key made
  1423. 57:26up of smaller things. So let's say for
  1424. 57:28example, we had a comments table.
  1425. 57:31You could make an association between
  1426. 57:34the user ID and the video ID.
  1427. 57:38But if you made that combination unique,
  1428. 57:40you wouldn't be able to make more than
  1429. 57:42one comment on a video because your user
  1430. 57:45ID is already in that key. So you could
  1431. 57:48add a third column here, such as the
  1432. 57:51timestamp. So in that situation, as long
  1433. 57:53as you're not posting at the exact same
  1434. 57:55moment in time, you can make another
  1435. 57:58comment. This combination
  1436. 58:00would be an example of a composite
  1437. 58:05natural key. We're using multiple
  1438. 58:07columns. They're not all keys
  1439. 58:09themselves, and it's a natural key
  1440. 58:12because we're not using purely generated
  1441. 58:14IDs. And these could even reference
  1442. 58:17other natural keys
  1443. 58:20if we wanted instead of an ID.
  1444. 58:23But the composite keys get pretty
  1445. 58:25complicated, in my opinion, rather
  1446. 58:27quickly. So I would rather just attach
  1447. 58:30to this a comment ID.
  1448. 58:36So we could just reference a specific
  1449. 58:38comment with its ID, whatever that ID
  1450. 58:42might be.
  1451. 58:45You still might protect this grouping
  1452. 58:50here. I just wouldn't make it the
  1453. 58:52primary key because if you're
  1454. 58:54referencing that in the database, it
  1455. 58:56being multiple columns, that becomes
  1456. 58:58more complicated. So I just attach a key
  1457. 59:01to everything. So I don't really use
  1458. 59:02natural keys for the actual database
  1459. 59:05connections. And then additionally,
  1460. 59:07because I'm always putting in a
  1461. 59:09surrogate ID, I don't really use these
  1462. 59:12composite keys or the compound keys for
  1463. 59:15the key itself, but maybe just as the
  1464. 59:17uniqueness constraint. So if you have
  1465. 59:19something that could have worked as a
  1466. 59:21key, such as this right here, and then
  1467. 59:23you downgrade it from a key to a unique
  1468. 59:26constraint, this is said to be a
  1469. 59:29candidate key or an alternate key.
  1470. 59:33Basically something you could have
  1471. 59:34chosen as a key. And that's important
  1472. 59:36because it's ideal that a table does
  1473. 59:39have some data in there that could exist
  1474. 59:41as a key, even if you do use surrogate
  1475. 59:44keys. It's not like a hard rule you have
  1476. 59:47to, but it goes back to the data
  1477. 59:49integrity. We don't just want to use a
  1478. 59:51surrogate key and then let the data in
  1479. 59:53the table be whatever it wants. So
  1480. 59:55candidate keys can be downgraded to
  1481. 59:58unique constraints.
  1482. 1:00:00And even this key is pretty bad, right?
  1483. 1:00:02We're using the timestamp, which if you
  1484. 1:00:05double click or the client sends the
  1485. 1:00:07data twice or there's a glitch on the
  1486. 1:00:09back end, it might send the same comment
  1487. 1:00:12briefly after the first one and they
  1488. 1:00:14both get inserted into the database.
  1489. 1:00:16So in that situation, you have comment A
  1490. 1:00:19and then comment B, and these are
  1491. 1:00:22exactly the same with just a different
  1492. 1:00:23timestamp. So it's not really a strong
  1493. 1:00:25example. So you could look at your
  1494. 1:00:27primary key or any candidate keys or
  1495. 1:00:29unique constraints and judge the quality
  1496. 1:00:32of it by how unique it can force the
  1497. 1:00:34data to be.
  1498. 1:00:36Because if you just have two comments
  1499. 1:00:37that look like this and they're both
  1500. 1:00:39from the same user on the same video,
  1501. 1:00:41it's hard to say for certain if the
  1502. 1:00:44person meant to post it twice or it's
  1503. 1:00:46duplicate data and one should be
  1504. 1:00:48removed. Comments are a challenging one
  1505. 1:00:50because there's not a really strong
  1506. 1:00:52natural key. So this is an example of
  1507. 1:00:54something you could do, but it's not a
  1508. 1:00:55flawless solution. And it has flaws
  1509. 1:00:58because it's depending on luck and
  1510. 1:01:01everything going good. It doesn't have
  1511. 1:01:03anything that's inherently a strong
  1512. 1:01:05natural key. An example of something
  1513. 1:01:07that might have a stronger natural key
  1514. 1:01:09would be something like the daily stats
  1515. 1:01:11for a video where it's associated to a
  1516. 1:01:14day and that is naturally going to be
  1517. 1:01:17unique. Long story short, let me just
  1518. 1:01:19summarize everything I just said in like
  1519. 1:01:22five words. Use a surrogate key,
  1520. 1:01:28please. Okay, so you decided to use a
  1521. 1:01:29surrogate key, but it's not that simple.
  1522. 1:01:32We could have an auto incremented
  1523. 1:01:39ID, which is what we've been using so
  1524. 1:01:41far, counting 1 2 3 4 up for eternity.
  1525. 1:01:46Or we could use a UUID or GUID,
  1526. 1:01:51a universally unique identifier or
  1527. 1:01:53globally unique identifier, which is
  1528. 1:01:55basically just getting the number
  1529. 1:01:57randomly instead of counting up from
  1530. 1:01:59one. Which of these do you choose? And
  1531. 1:02:01this actually brings up some of the
  1532. 1:02:02stuff that we talked about in the
  1533. 1:02:03beginning about your system design. The
  1534. 1:02:05choice here, I would say, comes down to
  1535. 1:02:07how you are using your database. If you
  1536. 1:02:10have a centralized database that
  1537. 1:02:13everything points to,
  1538. 1:02:15and this database has the ability to
  1539. 1:02:18auto increment IDs, then that's probably
  1540. 1:02:20what I would go with. Generating
  1541. 1:02:22globally unique IDs is more common when
  1542. 1:02:25you are in a decentralized system where
  1543. 1:02:28you might have multiple database nodes
  1544. 1:02:31because you don't want to have
  1545. 1:02:32conflicting IDs across that data. So if
  1546. 1:02:36instead of a single database node, we
  1547. 1:02:39had a database cluster and all of these
  1548. 1:02:41database nodes were auto incrementing,
  1549. 1:02:44there's a chance when you looked across
  1550. 1:02:45the data from multiple nodes that there
  1551. 1:02:48would be IDs reused.
  1552. 1:02:50So then you need to have like a
  1553. 1:02:51centralized ID distributor and all this
  1554. 1:02:54crazy system design stuff that you can
  1555. 1:02:56research if you're interested in it. One
  1556. 1:02:58simple solution is to just use a GUID.
  1557. 1:03:00And when you insert the data into the
  1558. 1:03:04database, you can generate that GUID in
  1559. 1:03:06your code and attach it to the row.
  1560. 1:03:07That's if the database doesn't have a
  1561. 1:03:10way to do it itself. You might be able
  1562. 1:03:12to just have the database create that
  1563. 1:03:13ID. Either way, basically the row needs
  1564. 1:03:16a unique ID. You can auto increment or
  1565. 1:03:19you can use a UUID. So foreign keys
  1566. 1:03:21reference data in another table. We used
  1567. 1:03:23the example of a lookup table earlier.
  1568. 1:03:26This is a very good example. I don't
  1569. 1:03:29remember the exact data, but we had
  1570. 1:03:30something like four permissions. We had
  1571. 1:03:34one owner, two admin, three editor.
  1572. 1:03:40So we'll call this the permissions
  1573. 1:03:41table.
  1574. 1:03:43And then we have another table such as
  1575. 1:03:46channel users. Well, this is actually a
  1576. 1:03:49junction table, so it just has a bunch
  1577. 1:03:50of keys. But we might have user ID one
  1578. 1:03:54for channel ID three and this person is
  1579. 1:03:59an admin. So what the foreign key does
  1580. 1:04:03is it says the value has to exist over
  1581. 1:04:07here in this list.
  1582. 1:04:09So this would be defined as a permission
  1583. 1:04:12ID. And then you would say references
  1584. 1:04:16permissions.permission
  1585. 1:04:18ID, for example. And in this situation,
  1586. 1:04:20we actually have three foreign keys all
  1587. 1:04:23pointing to different tables. And you'll
  1588. 1:04:26probably have a surrogate key for this
  1589. 1:04:28junction table, which is the primary
  1590. 1:04:30key. So I'll just say that's
  1591. 1:04:34ID one. So these underlined ones point
  1592. 1:04:36to some other tables. I'm just going to
  1593. 1:04:38draw arrows to demonstrate that. But
  1594. 1:04:41let's focus in on this one so we have a
  1595. 1:04:42concrete example.
  1596. 1:04:44So here are the rules with foreign keys.
  1597. 1:04:46Pretty simple. It has to reference a
  1598. 1:04:49unique
  1599. 1:04:51column, but the value itself does not
  1600. 1:04:54have to be defined unique on the
  1601. 1:04:57definition side.
  1602. 1:04:58It does not have to be unique. And
  1603. 1:05:01that's very important to remember. The
  1604. 1:05:04destination has to be unique, but the
  1605. 1:05:05foreign key value does not have to be
  1606. 1:05:07unique.
  1607. 1:05:09Meaning we could have other values in
  1608. 1:05:11here that also point to admin. That
  1609. 1:05:15makes sense, right? Because if you're
  1610. 1:05:17talking about all of channels and all of
  1611. 1:05:19the users, there's to be a lot of
  1612. 1:05:22admins. And then pretty much all the
  1613. 1:05:23other rules with primary keys do not
  1614. 1:05:26apply to foreign keys. This can be null
  1615. 1:05:29if we decide. So you can make it
  1616. 1:05:30nullable. This person has no
  1617. 1:05:32permissions. You can change that value.
  1618. 1:05:34We promote them up to owner. And it
  1619. 1:05:37doesn't have to be unique. So pretty
  1620. 1:05:39much we have full control over that
  1621. 1:05:41foreign key with the only rule being it
  1622. 1:05:44has to reference something unique. So
  1623. 1:05:46there are a couple of configurations
  1624. 1:05:48that you should be familiar with for a
  1625. 1:05:50foreign key constraint, and those are on
  1626. 1:05:53update and on delete.
  1627. 1:05:59The main one you should concern yourself
  1628. 1:06:01with is on delete. In 99 out of 100
  1629. 1:06:05scenarios, the foreign key is going to
  1630. 1:06:07reference a key and a primary key should
  1631. 1:06:11not change. So on update is not super
  1632. 1:06:15relevant. Basically though, let's just
  1633. 1:06:17say the key changed, then you can
  1634. 1:06:19configure what happens to the child
  1635. 1:06:21value. But as mentioned, that's not
  1636. 1:06:23super relevant. What is common though is
  1637. 1:06:26trying to delete the parent.
  1638. 1:06:31So in this example, the thing being
  1639. 1:06:34referenced is the parent and then the
  1640. 1:06:36referencing side is the child.
  1641. 1:06:39So the exact words might change from
  1642. 1:06:42database provider to database provider,
  1643. 1:06:46but the general list will look something
  1644. 1:06:47like this. No action,
  1645. 1:06:50restrict. And this is a configuration on
  1646. 1:06:53the key so you can decide what happens.
  1647. 1:06:56Cascade,
  1648. 1:06:57set null or set default. So in this
  1649. 1:07:02situation, we are referencing the value
  1650. 1:07:05one, which points to this here. And then
  1651. 1:07:08let's say we delete this data. When we
  1652. 1:07:11define that foreign key, we specify what
  1653. 1:07:14behavior we want to happen when we
  1654. 1:07:17delete the parent.
  1655. 1:07:19So by default, we have no action, which
  1656. 1:07:21is going to error and say, hey, you
  1657. 1:07:24actually can't delete this row. It's
  1658. 1:07:26probably a good safe default. No action
  1659. 1:07:29and restrict are very similar. They're
  1660. 1:07:31both going to error out. There's a minor
  1661. 1:07:33difference of when that happens.
  1662. 1:07:36And that's just in the situation of
  1663. 1:07:39doing this within a transaction. If you
  1664. 1:07:41are using no action and you get the
  1665. 1:07:43state to be valid before that
  1666. 1:07:44transaction is committed, you're good to
  1667. 1:07:46go. With restrict, it's going to just
  1668. 1:07:48error out if you change it at all. So
  1669. 1:07:50that's kind of an edge case difference.
  1670. 1:07:52You usually don't have to worry about
  1671. 1:07:53it. Both of these you can think of as
  1672. 1:07:55just not allowing you to delete that
  1673. 1:07:56parent. So you can imagine deleting that
  1674. 1:07:59and then creating a new row with the
  1675. 1:08:00same ID all within that transaction. By
  1676. 1:08:03the time the transaction ends, the state
  1677. 1:08:05is fine and we're good. But with
  1678. 1:08:07restrict, it's going to error out. For
  1679. 1:08:09some database systems, these are exactly
  1680. 1:08:11the same behavior. So research your
  1681. 1:08:13specific database system. The next one
  1682. 1:08:15is really common as well. So that's
  1683. 1:08:17cascade. And this one you will logically
  1684. 1:08:20think makes sense in many situations,
  1685. 1:08:23but is actually pretty dangerous. So
  1686. 1:08:25basically, if you delete the parent,
  1687. 1:08:29it will cascade to any references,
  1688. 1:08:31deleting that row as well. So if you
  1689. 1:08:35were to delete two, you would delete
  1690. 1:08:37this row.
  1691. 1:08:39This is nice because it allows you to
  1692. 1:08:41delete stuff without it saying error and
  1693. 1:08:43you having to go figure out a bunch of
  1694. 1:08:44relationships. But it's very dangerous
  1695. 1:08:47because you might delete one thing not
  1696. 1:08:49realizing there are side effects to that
  1697. 1:08:51delete. When would you want to do a
  1698. 1:08:53cascade? Well, you could imagine if you
  1699. 1:08:55had a video and you had comments on that
  1700. 1:08:58video and you delete the video, well, it
  1701. 1:09:00makes sense for those comments to
  1702. 1:09:01disappear. So that's an example where
  1703. 1:09:03cascade is logical. Or if you have a
  1704. 1:09:04table for user settings and you delete
  1705. 1:09:06that user account, then yeah, cascade
  1706. 1:09:09it. A safer option is set null and that
  1707. 1:09:12basically creates an orphaned row. And
  1708. 1:09:15this is a row that points to another row
  1709. 1:09:17that doesn't exist. So the child exists,
  1710. 1:09:19but there's no parent. It's a little bit
  1711. 1:09:21depressing, but this is a safer option
  1712. 1:09:24because you don't risk any
  1713. 1:09:26effects from an initial delete. It's
  1714. 1:09:28just going to break those connections.
  1715. 1:09:30In order for this to work, the column
  1716. 1:09:33must not be set to not null.
  1717. 1:09:37So, not
  1718. 1:09:40not null. Or
  1719. 1:09:43you could just say nullable. You might
  1720. 1:09:45have this keyword in your database as
  1721. 1:09:47well. Set default works similarly. If
  1722. 1:09:49you set a default value for a column, it
  1723. 1:09:51will just set it to the default instead
  1724. 1:09:53of setting it to null or deleting that
  1725. 1:09:55row entirely. So, you can use set
  1726. 1:09:57default if you need to change the value
  1727. 1:09:59to something else if a certain row is
  1728. 1:10:02deleted. Most of the time, I would
  1729. 1:10:04recommend no action or set null. That's
  1730. 1:10:07going to get you most of the
  1731. 1:10:08capabilities without the risk that
  1732. 1:10:11cascade brings. The only downside is if
  1733. 1:10:14you need that column to have a value,
  1734. 1:10:16you can't do a set null.
  1735. 1:10:19In that situation, you can use a set
  1736. 1:10:20default. And this brings up a better
  1737. 1:10:22question of when should you delete data?
  1738. 1:10:24You should do some dedicated research on
  1739. 1:10:27deleting data.
  1740. 1:10:30Now, obviously you want to be able to
  1741. 1:10:32delete data, but sometimes you just want
  1742. 1:10:35to do a soft delete or do a soft delete
  1743. 1:10:37for a period of time before data is
  1744. 1:10:40deleted. So, if you delete a user
  1745. 1:10:42account, maybe that user just gets a
  1746. 1:10:44flag saying deleted, but the row still
  1747. 1:10:47exists in the database. And then any
  1748. 1:10:49users that stay in that deleted state
  1749. 1:10:52for 30 days or 90 days ultimately get
  1750. 1:10:56deleted. This allows you to have more
  1751. 1:10:58aggressive delete behavior in your apps
  1752. 1:11:00without the risk of data loss or having
  1753. 1:11:02to go get a database backup to restore
  1754. 1:11:05some data. You're really just changing a
  1755. 1:11:07flag for a delete. So, that could get
  1756. 1:11:09into a bunch of details, so just do some
  1757. 1:11:11research. You also want to consider the
  1758. 1:11:14legal parts of it if you are in a
  1759. 1:11:16location or serving users in a location
  1760. 1:11:19where data privacy is taken seriously,
  1761. 1:11:21you might have to actually delete that
  1762. 1:11:23data and not just use a soft delete. So,
  1763. 1:11:26that is your overview on foreign keys. I
  1764. 1:11:29don't believe foreign key columns are
  1765. 1:11:31indexed by default, but if you're often
  1766. 1:11:34working with them, you might want to
  1767. 1:11:35consider adding an index. And also
  1768. 1:11:37sometimes the database, if you use a
  1769. 1:11:39unique constraint, it will automatically
  1770. 1:11:41index that column. So again, learn the
  1771. 1:11:43details of your specific relational
  1772. 1:11:45database management system. The next
  1773. 1:11:47major topic in database design is
  1774. 1:11:49normalization. Now you have already
  1775. 1:11:51learned so many fundamentals that the
  1776. 1:11:53normalization process is going to be
  1777. 1:11:55super easy. I think the reason why so
  1778. 1:11:57many people get tripped up is they start
  1779. 1:11:59studying normalization when they don't
  1780. 1:12:01understand everything we've talked about
  1781. 1:12:03so far. So what is normalization? It's
  1782. 1:12:05the process of breaking up single tables
  1783. 1:12:08into multiple tables. This is basically
  1784. 1:12:10to apply certain database design rules,
  1785. 1:12:12and these will protect our database
  1786. 1:12:14integrity. So we'll go through these
  1787. 1:12:16rather quickly because they're just
  1788. 1:12:18rewordings of things we've already done.
  1789. 1:12:20We'll just go through an example from
  1790. 1:12:22beginning to end. So let's say we have a
  1791. 1:12:25videos table.
  1792. 1:12:27We have the actual video title. We have
  1793. 1:12:30tags.
  1794. 1:12:32We have the channel it comes from, and
  1795. 1:12:35we have the owner. Let's say email
  1796. 1:12:40and owner name.
  1797. 1:12:43So this is a pretty good example because
  1798. 1:12:46we got a lot of bad things going on
  1799. 1:12:48here. And we'll start off with just some
  1800. 1:12:49kind of spreadsheet
  1801. 1:12:51looking thing.
  1802. 1:12:54And hopefully I get enough room for each
  1803. 1:12:56of these. Let's say we have a video Git
  1804. 1:13:00in one hour. Coming
  1805. 1:13:03soon, maybe.
  1806. 1:13:05Might want to subscribe. You never know.
  1807. 1:13:07Say we have another video Docker crash
  1808. 1:13:11course. And then we add in some tags for
  1809. 1:13:16this. So Git, GitHub,
  1810. 1:13:21version control. Let's just say Docker
  1811. 1:13:24and Docker Compose.
  1812. 1:13:26The channel, we're going to say Code
  1813. 1:13:28Breakthrough. Same for that
  1814. 1:13:32And then some email address.
  1815. 1:13:34And then my name. So this is awful.
  1816. 1:13:37Everything is within one table. Let's
  1817. 1:13:39just go through some of the issues.
  1818. 1:13:41Every time we post a video, we get data
  1819. 1:13:43duplication. We have that channel in
  1820. 1:13:45there multiple times. We have the email,
  1821. 1:13:47the name in there multiple times. What
  1822. 1:13:49this does is if we change the user's
  1823. 1:13:51name, we'll either have to go update it
  1824. 1:13:53everywhere else or we have conflicting
  1825. 1:13:56values, both a problem. Additionally
  1826. 1:13:59with tags, we have multiple values
  1827. 1:14:01within a single field. So it's much
  1828. 1:14:03harder to search for something like
  1829. 1:14:05docker compose, which is in this array
  1830. 1:14:09essentially. Or what if I need to change
  1831. 1:14:11the channel owner or the channel has
  1832. 1:14:13multiple owners? How am I going to do
  1833. 1:14:16that? I might need to go change all of
  1834. 1:14:18them or make it an array. This is an
  1835. 1:14:20extreme example, but it allows us to see
  1836. 1:14:22what anomalies show up. The first would
  1837. 1:14:24be an update anomaly where we change the
  1838. 1:14:27value here and it doesn't propagate to
  1839. 1:14:30all the other possible values. So now we
  1840. 1:14:32have drift. Or we have an insert
  1841. 1:14:35anomaly. And this is almost like a
  1842. 1:14:37catch-22 kind of thing where you want to
  1843. 1:14:41create a channel, but everything's
  1844. 1:14:43within one table here. So we would need
  1845. 1:14:45to also put a video and its tags. How do
  1846. 1:14:48we add a new channel? Then also delete
  1847. 1:14:50anomalies. Let's say you delete these
  1848. 1:14:52two videos and this is all the data we
  1849. 1:14:55have on the channel. Well, then did we
  1850. 1:14:58just delete the channel accidentally as
  1851. 1:15:00well?
  1852. 1:15:01Again, these seem kind of ridiculous
  1853. 1:15:02because this is the extreme case where
  1854. 1:15:05we're actually trying to store the full
  1855. 1:15:07database in one table, but microscale
  1856. 1:15:09versions of those issues show up when
  1857. 1:15:11you don't normalize a database. So I'll
  1858. 1:15:14just keep these as an example and I'll
  1859. 1:15:16crop off this table here so I have some
  1860. 1:15:17room to write. So we're going to talk
  1861. 1:15:19about the normal forms.
  1862. 1:15:22These are basically the steps in
  1863. 1:15:24database normalization.
  1864. 1:15:26There are a bunch of them, but I only
  1865. 1:15:28care about the first three. The
  1866. 1:15:31first normal form, second normal form,
  1867. 1:15:35and third normal form. The first thing I
  1868. 1:15:37should say before I forget is that these
  1869. 1:15:39build upon each other. To get to the
  1870. 1:15:41second normal form, you have to be in
  1871. 1:15:43the first. And to get to the third, you
  1872. 1:15:45have to be in the second. First normal
  1873. 1:15:46form deals with things being atomic.
  1874. 1:15:52And while I know it's not scientifically
  1875. 1:15:55accurate, the idea here is that an atom
  1876. 1:15:58is the smallest unit. So you're not
  1877. 1:16:00going to share a cell and put multiple
  1878. 1:16:03values in there. So if we decided, hey,
  1879. 1:16:05we can put multiple tags here, you might
  1880. 1:16:07think we could just duplicate the row.
  1881. 1:16:10One of them be Docker, the other one be
  1882. 1:16:12Docker Compose. That's like the first
  1883. 1:16:14step in thinking of how to fix this so
  1884. 1:16:16that we don't have multiple values in a
  1885. 1:16:19single field, but it's not the correct
  1886. 1:16:21fix because then we have two rows that
  1887. 1:16:23refer to the same entity. So we want a
  1888. 1:16:26row to uniquely identify the entity, and
  1889. 1:16:29we want that row to be uniquely
  1890. 1:16:31identifiable with an ID. So that was a
  1891. 1:16:34lot of words. I'm sorry.
  1892. 1:16:36Long story short, we want unique rows.
  1893. 1:16:39I should have just said that. So
  1894. 1:16:40eventually you'll just add IDs,
  1895. 1:16:43but there's still like so much else
  1896. 1:16:45wrong with this. So let's just run
  1897. 1:16:46through the list. The rule is anytime
  1898. 1:16:48one of these is broken, you're going to
  1899. 1:16:50introduce a new table. So this column is
  1900. 1:16:53breaking that. So we need a tags table.
  1901. 1:17:00Simple fix. This is the part in the
  1902. 1:17:02process where I would also grab any
  1903. 1:17:04duplicate values and break that column
  1904. 1:17:07off into its own table as well. So
  1905. 1:17:09that's going to be the channel here. So
  1906. 1:17:12I will update according to those
  1907. 1:17:13changes. So here is our video table now.
  1908. 1:17:18And then we'll have a tags table.
  1909. 1:17:20This will reference back to that video
  1910. 1:17:23and then the relevant tag. So the first
  1911. 1:17:26one was Git, GitHub, version control.
  1912. 1:17:31And then we'll have video two, which is
  1913. 1:17:33Docker,
  1914. 1:17:35and then Docker Compose.
  1915. 1:17:37We'll then have channel, and then all
  1916. 1:17:39that information that was originally
  1917. 1:17:42with that channel is now moved to this
  1918. 1:17:44table here. We will give this an ID.
  1919. 1:17:48And then over in the video, we can
  1920. 1:17:49reference what channel it belongs to. So
  1921. 1:17:53channel one. Both of these point here.
  1922. 1:17:56So I ended up rewriting this so I could
  1923. 1:17:59take my time and make it nice and neat
  1924. 1:18:01for you guys so you can actually see it.
  1925. 1:18:03Everything's pretty much the same. The
  1926. 1:18:04only thing is I added this surrogate tag
  1927. 1:18:06ID here, which is again, as I've
  1928. 1:18:09mentioned probably a hundred times, I
  1929. 1:18:10just add an ID to every single table.
  1930. 1:18:13Second normal form deals specifically
  1931. 1:18:15with partial dependencies, which is when
  1932. 1:18:18an attribute depends on only half of the
  1933. 1:18:21key.
  1934. 1:18:21Now, if you're using a surrogate key,
  1935. 1:18:23which is why I added this here, you
  1936. 1:18:25might be thinking, how could it depend
  1937. 1:18:27on only half of the key if we never have
  1938. 1:18:30a composite or compound key? Well, you
  1939. 1:18:32can still go off of what could be a good
  1940. 1:18:36natural composite key. So look at any of
  1941. 1:18:38the unique constraints for a table. This
  1942. 1:18:41is going to be really common for
  1943. 1:18:42junction tables because that is where
  1944. 1:18:45you often have two keys. So in this
  1945. 1:18:48situation, we added this tag ID and
  1946. 1:18:51these point back to certain videos. So I
  1947. 1:18:53have three tags for this video and two
  1948. 1:18:55tags for this video. Well, if we had
  1949. 1:18:57another attribute in here, that could be
  1950. 1:19:00a tag color, which is a common thing
  1951. 1:19:02you'll see. You'll have a multi-select
  1952. 1:19:05and as you select things, they'll show
  1953. 1:19:07up, but the different options are shaded
  1954. 1:19:10or colored differently. That's one
  1955. 1:19:12option. Or you could have a tag
  1956. 1:19:13description. So if you had a landing
  1957. 1:19:15page dedicated for content for that tag,
  1958. 1:19:18you could have the description for that
  1959. 1:19:20specific tag. Or you could have a
  1960. 1:19:22category for groups of tags. So these
  1961. 1:19:26are all in the programming category.
  1962. 1:19:28That would be a common approach as well.
  1963. 1:19:30And it's basically a way to merge
  1964. 1:19:32categories and tags. We basically have
  1965. 1:19:34tags and tag categories. So these are
  1966. 1:19:37all possibilities. Let's just go with
  1967. 1:19:39that category example because I really
  1968. 1:19:41like that. And we could group all of
  1969. 1:19:43these as technology or software. So you
  1970. 1:19:47might have category here.
  1971. 1:19:50And I'll just say tech for all of these.
  1972. 1:19:53This is an example second normal form
  1973. 1:19:55violation because the category is
  1974. 1:19:58talking about the tag, but the table is
  1975. 1:20:01dedicated to the tags for a certain
  1976. 1:20:04video. So there's no association between
  1977. 1:20:07the category and the video. This is
  1978. 1:20:09purely to describe the tags. That's a
  1979. 1:20:12partial dependency. It only depends on
  1980. 1:20:15the tag ID, not the video ID. So the
  1981. 1:20:17solution here is to just make this video
  1982. 1:20:20tags
  1983. 1:20:23and we'll introduce a new table.
  1984. 1:20:28Tag categories.
  1985. 1:20:30We'll have tag category ID
  1986. 1:20:34and then the name of that category, a
  1987. 1:20:38description or any other attributes you
  1988. 1:20:41want to store about it.
  1989. 1:20:43Then we'll replace this with a foreign
  1990. 1:20:45key.
  1991. 1:20:47So we'll say one
  1992. 1:20:50tech.
  1993. 1:20:52The description will be
  1994. 1:20:55nerd stuff. And these will all have that
  1995. 1:20:59category. So that is how you solve a
  1996. 1:21:01second normal form violation. You just
  1997. 1:21:03break it off into its own table. Now we
  1998. 1:21:05still have this repeating value, but you
  1999. 1:21:07have to remember it is a foreign key, so
  2000. 1:21:09it's tied to this. It's not like
  2001. 1:21:11repeating a string five times where if
  2002. 1:21:14we needed to change the name of that or
  2003. 1:21:16adjust the category data, we would have
  2004. 1:21:19to do it across all of those rows. Here
  2005. 1:21:21we only have to do it one place right
  2006. 1:21:23here. Now let's talk about third normal
  2007. 1:21:25form. This deals with transitive
  2008. 1:21:28dependencies. So we have partial
  2009. 1:21:34and transitive.
  2010. 1:21:39This is when we basically have a chain
  2011. 1:21:42where a column depends on another column
  2012. 1:21:44that ultimately depends on the primary
  2013. 1:21:46key, but you still have room to split
  2014. 1:21:48things out. The thing that's breaking
  2015. 1:21:50that is this right here. And you can see
  2016. 1:21:52these chains, if you were to look at a
  2017. 1:21:55different channel, that's going to
  2018. 1:21:57change the email of the owner and
  2019. 1:21:59ultimately the name of that owner
  2020. 1:22:02because we're talking about somebody
  2021. 1:22:03else.
  2022. 1:22:04So you can see it hops through multiple
  2023. 1:22:07columns. So you'll find that chain and
  2024. 1:22:09you'll basically split it off. So we'll
  2025. 1:22:11take the user portion of this and put it
  2026. 1:22:13in its own table.
  2027. 1:22:16So we'll have users over here.
  2028. 1:22:19This will have user ID, the user's
  2029. 1:22:23email,
  2030. 1:22:25and the user's name.
  2031. 1:22:27So now we can keep the channel over
  2032. 1:22:29here.
  2033. 1:22:31And we can just call it channel or we
  2034. 1:22:33can just say name since we don't have
  2035. 1:22:35the person's name in here as well.
  2036. 1:22:38So that's much cleaner. And then we will
  2037. 1:22:40need the user ID for who owns the
  2038. 1:22:42channel.
  2039. 1:22:44We're just going to assume one owner for
  2040. 1:22:46now. Let's go 73. Then over here, we'll
  2041. 1:22:49have a user with 73 and then my email
  2042. 1:22:52and my name. And that's a foreign key
  2043. 1:22:54that points to that table. Now we don't
  2044. 1:22:56have any transitive dependencies.
  2045. 1:22:58There's one other major design flaw that
  2046. 1:23:01we have in our current structure. So
  2047. 1:23:03what if we wanted to store multiple tags
  2048. 1:23:05for a video? So right here we have Git
  2049. 1:23:08in one hour.
  2050. 1:23:10So we have three tags, Git, GitHub, and
  2051. 1:23:12version control. These are all category
  2052. 1:23:15one, which if we follow that down to the
  2053. 1:23:18tag categories, we get the name tech.
  2054. 1:23:21But let's say we added another row in
  2055. 1:23:22here with an existing tag value such as
  2056. 1:23:25Git. And then we assigned it a different
  2057. 1:23:29category such as two. With our current
  2058. 1:23:31structure, every tag that's applied to a
  2059. 1:23:33video gets its own category rather than
  2060. 1:23:38that category being tightly coupled to
  2061. 1:23:41an individual tag. To fix this, we split
  2062. 1:23:43it out into another table, of course.
  2063. 1:23:46And this is why you start with one table
  2064. 1:23:47and you end up with 750. So I'm kind of
  2065. 1:23:50running out of room here. But yeah, you
  2066. 1:23:53basically
  2067. 1:23:55split these into two. You'd have video
  2068. 1:23:57tags and then a dedicated tags
  2069. 1:24:01table. We would have an ID for each
  2070. 1:24:02individual row, but we would have the
  2071. 1:24:04video ID and then the tag ID.
  2072. 1:24:06So it might look something like this
  2073. 1:24:08where we have this junction table to
  2074. 1:24:11apply tags to videos. And that way, Git
  2075. 1:24:14will always have this same category and
  2076. 1:24:16we just reference that tag metadata
  2077. 1:24:20using its ID. And this should be the
  2078. 1:24:23name of the tags. So now Git gets its
  2079. 1:24:27own category and it's always going to be
  2080. 1:24:29that category and we just reference it
  2081. 1:24:31from the video tags table. Also, I don't
  2082. 1:24:34even know how I don't notice these
  2083. 1:24:35things when I'm writing. Category.
  2084. 1:24:39There we go.
  2085. 1:24:42I'm learning how to spell. So that's an
  2086. 1:24:44overview of the normalization process.
  2087. 1:24:46It's kind of just a game of making sure
  2088. 1:24:49there's no integrity issues with your
  2089. 1:24:51database. And this is a spectrum.
  2090. 1:24:53Sometimes you'll choose to denormalize
  2091. 1:24:57for some tradeoffs. If you're creating a
  2092. 1:24:59bunch of tables and not getting a lot of
  2093. 1:25:01benefit from it, then sometimes you can
  2094. 1:25:03choose to stop that normalization
  2095. 1:25:05process a bit early, which is basically
  2096. 1:25:08what we did because there's actually
  2097. 1:25:09normal forms after this, but that's when
  2098. 1:25:11you get into excessive normalization
  2099. 1:25:14with minimal benefit. So that's why I
  2100. 1:25:17tend to just think of the first three.
  2101. 1:25:18So I think I got it all. There might be
  2102. 1:25:20little things here or there, but overall
  2103. 1:25:21you get the idea. So that's an overview
  2104. 1:25:23of database design. Much of the process
  2105. 1:25:25is breaking things out into more tables.
  2106. 1:25:28But how do you then get that data to
  2107. 1:25:30come back together? That's the process
  2108. 1:25:32of joins and you'll do that when you're
  2109. 1:25:34writing SQL. So SQL is the language you
  2110. 1:25:38use to interact with the database. If
  2111. 1:25:40you've made it this far into the video,
  2112. 1:25:41you probably already know that. But
  2113. 1:25:43there's a lot to writing SQL and a lot
  2114. 1:25:45of it ties to what we talked about in
  2115. 1:25:47this video. So when it comes to things
  2116. 1:25:48like joins and views or indexes, the
  2117. 1:25:51different data types that we use for
  2118. 1:25:53data definition for data integrity
  2119. 1:25:56purposes, how do we actually turn our
  2120. 1:25:58concepts like our foreign key
  2121. 1:26:00constraints or unique constraints into
  2122. 1:26:02real things in our database? That's what
  2123. 1:26:04we're going to do with SQL. And there's
  2124. 1:26:06enough to talk about on this topic that
  2125. 1:26:08I wanted to just do a dedicated video to
  2126. 1:26:10it. So from here, please be sure to
  2127. 1:26:12check out my SQL course to continue the
  2128. 1:26:14discussion, as there's still a lot more
  2129. 1:26:16I want to go through. But at this point,
  2130. 1:26:18you should have a pretty solid
  2131. 1:26:19foundation of database design. If you
  2132. 1:26:21want to get more into the system design
  2133. 1:26:23with databases, you can check out my
  2134. 1:26:25system design playlist and my backend
  2135. 1:26:27engineering playlist. This will talk
  2136. 1:26:29about things like the cap theorem and
  2137. 1:26:31other database principles that come up
  2138. 1:26:33more often with system design
  2139. 1:26:34interviews. So a little bit less about
  2140. 1:26:37application development and designing
  2141. 1:26:39the database tables and more about
  2142. 1:26:41scaling applications that use databases.
  2143. 1:26:44That'll talk about things like
  2144. 1:26:46consistent hashing and how to use IDs
  2145. 1:26:48properly, stuff that goes beyond what we
  2146. 1:26:50talked about here. So hopefully this
  2147. 1:26:52pushes you in the right direction and
  2148. 1:26:53you can continue learning with my other
  2149. 1:26:55resources. Thank you for watching and
  2150. 1:26:57let me know what other content you'd
  2151. 1:26:58like to see and I'll see you in the next
  2152. 1:27:00one. Peace out.

About this transcript

This page contains the full transcript of Database Design Full Course - How to Design a Relational Database (1.5 Hours!) by Caleb Curry, generated from the public captions YouTube serves with the video. The transcript has 14,232 words across 2,152 segments, with the original timestamps preserved so you can click any line to jump to that moment in the embedded player.

What you can do with it

Use the transcript to take notes, quote the speaker, build a study guide, generate a summary with ChatGPT or Claude via the YouTube Summary tool, or export it as a timed subtitle file with YouTube to SRT. You can also re-open it in the transcriber to choose a caption track available on the video.

Free YouTube transcript tool

YouTube2Text is a free YouTube transcript generator — no signup, no daily limit. Paste any YouTube link and get the full transcript instantly, with timestamps, click-to-jump, caption language selection, AI prompts for ChatGPT, Claude, and Gemini, and exports to TXT, SRT, VTT, or Markdown.