Database Design Full Course - How to Design a Relational Database (1.5 Hours!) — Transcript
Full transcript
- 0:00Welcome to your database design course.
- 0:02This video is designed to cover
- 0:03everything we talk about in my eight
- 0:04hour database design video without
- 0:06having to spend all day studying the
- 0:08topic. But there's no real information
- 0:10lost because the way I designed this
- 0:12video is to take you from the complete
- 0:14beginning to being able to design
- 0:16databases confidently. And best of all,
- 0:18you'll be able to look at existing
- 0:19database designs and be able to identify
- 0:21certain issues that could cause problems
- 0:23later on down the road. Database design
- 0:25is one of those things where a little
- 0:26bit of effort goes a really long way and
- 0:28if you mess things up, you might end up
- 0:30causing issues with your apps down the
- 0:31road, having to come back and spending
- 0:33hours or days fixing your database
- 0:35design and wasting a lot of time
- 0:37figuring out how to enforce data
- 0:38integrity. So if you start with the
- 0:40database design from the beginning,
- 0:41you're going to save yourself so much
- 0:43time in the future. So we'll start off
- 0:44with some foundational stuff. If you've
- 0:46done anything with databases or
- 0:47application development before, a lot of
- 0:49this might be a review, but let's just
- 0:50go through it to make sure everybody's
- 0:51on the same page for the upcoming
- 0:53content. You can, however, use the notes
- 0:55and the timestamps if you need to jump
- 0:56to any section. So when I'm talking
- 0:58about databases, I'll often draw it
- 1:00something like this, which is like the
- 1:02universal database icon. And what do we
- 1:05actually store in a database? Well, put
- 1:08simply, we store data. Wow. That's
- 1:11incredible, Caleb. Never thought of
- 1:13that. So what exactly would be
- 1:14classified as data? Well, really
- 1:16anything you want to store to be able to
- 1:18access that information later. You can
- 1:20think of it just as info that we need to
- 1:22persist for later access. So a common
- 1:24example of this is if you have a website
- 1:27or an app, it could be a desktop app or
- 1:30a mobile app, and there might be an
- 1:32option to log in. Well, in order for
- 1:34that system to know if you're logging in
- 1:36correctly, it needs to check data in the
- 1:38database. So that's one example, storing
- 1:41user information. You can also store
- 1:43events, when the person logged in, their
- 1:45activity. You can store things like user
- 1:47settings, what are their preferences.
- 1:49Any of the data that they access on your
- 1:51website needs to be stored somewhere as
- 1:52well. Now, this is like a visual diagram
- 1:55of what a database looks like, but what
- 1:56is a database? Well, it's just a
- 1:59software going to be installed on a
- 2:01server.
- 2:05So it might be obvious, might not be if
- 2:07you've never really seen servers, but
- 2:09there's not an actual system that looks
- 2:11like this in person, right? It's just a
- 2:13regular computer. So there will be
- 2:15server racks or even just computers like
- 2:17your own personal computer, and you will
- 2:19install a relational database management
- 2:23system.
- 2:24So relational here, that refers to
- 2:26tables. So everything is tabular data.
- 2:29So when you look at it and you visualize
- 2:30that data, it's going to look almost
- 2:32like a spreadsheet. So we'll talk a
- 2:34little bit about that structure, but
- 2:35first I want you to understand that the
- 2:38database is just the software on a
- 2:40computer. That computer could be
- 2:41dedicated to that database or it could
- 2:44also be running your backend or other
- 2:46services as well. So it really depends
- 2:48on what scale you are at, how much
- 2:50computing you dedicate to this database.
- 2:52But fortunately, all the database design
- 2:55stuff is going to be pretty much the
- 2:57same. The only time it's going to make a
- 2:59difference is if you get to things like
- 3:01sharding and splitting up data across
- 3:04regions. That gets into some more
- 3:06complex system design topics. I do have
- 3:08dedicated lessons on that. For most
- 3:10applications and what you're probably
- 3:12going to be using is just a centralized
- 3:14database in one location.
- 3:16So that means somewhere we have this
- 3:18database stored and then all the
- 3:20requests get funnelled to this one
- 3:22location. So that's a little bit about
- 3:23how it works behind the scenes, but for
- 3:24the rest of the video, we'll pretty much
- 3:26ignore the computer hardware and just
- 3:28talk about how things work for the
- 3:30actual database software. Okay, so we
- 3:33talked about this being built as tables.
- 3:37And when you hear the term relation,
- 3:40fundamentally this means tables, but
- 3:42you'll also hear relationships.
- 3:47These are fundamentally different if
- 3:49you're studying database design
- 3:51formally, but many people do call
- 3:53relationships relations and that gets a
- 3:55little confusing. But that's what it
- 3:56means when we're talking about a
- 3:58relational database management system.
- 4:00We're building things structured as
- 4:02tables. So if we can visualize the data
- 4:04as tables, we're basically going to have
- 4:06columns at the top and then we'll have
- 4:09different rows of data.
- 4:11And this looks a lot like a spreadsheet.
- 4:14So, when would you want to use a
- 4:16database over just a simple spreadsheet
- 4:18if they structure data the same way?
- 4:20Well, this really comes down to the use
- 4:21case and the scale at which you need to
- 4:23run. Generally, with a spreadsheet, I
- 4:25think of it as something that a human
- 4:27will go in there, look at the data,
- 4:28enter in numbers, run calculations,
- 4:30whatever they need to do, and typically
- 4:32that user is connected to that
- 4:33spreadsheet directly.
- 4:35With databases, when you're starting
- 4:36out, that might be the case, or if
- 4:38you're a database administrator, that
- 4:39might be the case. You might be working
- 4:41with the database directly. But in most
- 4:43cases, it's going to be an application
- 4:45that connects to the database. So, it
- 4:47looks something like this, where
- 4:48somebody tries to log in, we have a
- 4:50backend application that processes that
- 4:52request and accesses anything it might
- 4:54need to access from the database. For
- 4:56good software engineering principles, we
- 4:58want to avoid direct connection to that
- 5:01database when we can. By that, I mean we
- 5:04want to avoid having people go in here
- 5:06and mess with the data in the database.
- 5:09This is risky because, well, people make
- 5:12mistakes, but also it can put the data
- 5:15itself in jeopardy. So, what we do is we
- 5:17program the exact things we want to do
- 5:19in some software, and then we can
- 5:21interact with the database through that
- 5:23software. Beyond that, there's other
- 5:25benefits as well. Databases are designed
- 5:27with indexing for fast querying. They
- 5:30have formal relationships so you can
- 5:32connect data across tables. They also
- 5:34have procedural programming languages,
- 5:36and often they can support many
- 5:38connections at one time. Some of those
- 5:40things can exist with spreadsheets, but
- 5:42it's really something for a small scale
- 5:45and not something you would want to use
- 5:47for the backend of an application. I
- 5:49have seen people use basic spreadsheets
- 5:51for the backends of applications, but
- 5:53that's going to limit them if they want
- 5:54to get to a certain level. So, really
- 5:56that's just for like personal
- 5:58applications. And if I was to start
- 5:59something just for fun, I would skip the
- 6:01spreadsheet altogether and just go with
- 6:03a small database instance. It's easy to
- 6:05get started with SQLite or Postgres.
- 6:08You can often just use it within a
- 6:10container. You don't even have to have a
- 6:11tight integration with your system. So
- 6:13the general structure for software
- 6:15development is going to be some front
- 6:18end or client that connects to a back
- 6:21end, and the back end is what accesses
- 6:23the database. We've already mentioned
- 6:25this, but I just want to draw it out
- 6:27because I wanted to specifically mention
- 6:29that we should not go from the client
- 6:31directly. So for [clears throat]
- 6:33example, if you are building a website
- 6:35or a mobile app or a desktop app, you do
- 6:38not want to put that connection string
- 6:42in the client.
- 6:44Anything on the client you should assume
- 6:46is insecure. So if you have some
- 6:48connection string to a shared database,
- 6:50that client can manipulate that database
- 6:52in ways you wouldn't want them to. So
- 6:54this is the general structure for a
- 6:56centralized
- 6:59database. Something you would get with
- 7:00Postgres, MySQL, Oracle, SQL Server, or
- 7:04any of the other major database systems
- 7:06out there. But this brings up the
- 7:07question, what if you have some software
- 7:10that needs to store information
- 7:13and it's not in this client server
- 7:15structure? Let's just say it's some
- 7:17local video editing software and we need
- 7:19to store what edits exist or what files
- 7:22exist or whatever it might be. Well,
- 7:25there's often going to be a dedicated
- 7:28database just for that instance. So in
- 7:32this situation, if someone else
- 7:33downloaded the software,
- 7:37they would have the database copied as
- 7:39well. So this is a fundamentally
- 7:42different structure than what we have
- 7:44here. Because in this situation, if we
- 7:47have multiple connections, let's
- 7:50say three clients are connected. I'll
- 7:53just draw them over here so you can
- 7:54visualize them.
- 7:58All three of these are going to connect
- 8:00to that same backend service. In this
- 8:03instance, you're going to have two
- 8:05separate clients with two separate
- 8:08database instances. They're not shared,
- 8:10so you don't have that centralized
- 8:12connection.
- 8:13This idea is often called an embedded
- 8:15database
- 8:18because the database is embedded in
- 8:21whatever system is running that
- 8:23software. It goes with the software. So
- 8:25that's what you're going to get for
- 8:26things like refrigerators that need to
- 8:29store any information, any smart
- 8:31technology or offline databases, they're
- 8:34likely going to be installed on that
- 8:36system. So this is really getting into
- 8:37the architecture of databases and a
- 8:40little bit less on actually designing
- 8:42tables, but this is very important
- 8:44because it can influence the way you
- 8:46design tables. If your database has to
- 8:49support many users, you might have a
- 8:52different design than you would if you
- 8:54were in a distributed environment. So
- 8:56starting off, let's just assume we're in
- 8:58this environment here. And in that
- 9:00situation, we might have some separation
- 9:02where this user cannot access everything
- 9:05from this user and vice versa. And those
- 9:08complexities is why the software layer
- 9:11is so important. So if you're building
- 9:13on top of a database, let's
- 9:18say this is your app,
- 9:21you can code the rules
- 9:28for access. You control everything that
- 9:31gets through to the database.
- 9:34So you could tie this to auth, for
- 9:36example, and make sure the person is
- 9:37logged in.
- 9:39You could tie it to region,
- 9:42only giving the information for a
- 9:45certain location.
- 9:47You can also validate data.
- 9:54So that software layer is super
- 9:55important.
- 9:57Some stuff will be done in the database
- 9:59and then other stuff will be done at the
- 10:01application layer. So what I want to do
- 10:02now is I want to zoom in on this section
- 10:05now that we got the architecture and the
- 10:07fundamentals out of the way, and we can
- 10:09talk what the actual database structure
- 10:12will look like. So, let's just get some
- 10:13definitions out of the way. These are
- 10:17pretty formal, so you might not see
- 10:19these so much in interviews, but if
- 10:21you're in school, you probably will.
- 10:25So, entity type, and then we have
- 10:31attributes. So, when you're working with
- 10:33a database, you're not just going to
- 10:34store one thing, you're going to store
- 10:36multiple of those things. So, for
- 10:38example, if you have a table for
- 10:40comments,
- 10:43you're going to have multiple rows, each
- 10:45describing a comment. The entity type is
- 10:48the thing you're trying to describe, the
- 10:50type of data. The attributes are what
- 10:52things you care about with this comment.
- 10:55So, for example,
- 10:57the posting timestamp,
- 11:00the person who posted it, the poster,
- 11:03what they commented on, and you can put
- 11:06any other data that you care about
- 11:07related to that comment. These are the
- 11:11attributes. So, one specific comment
- 11:14would be an entity, and then the
- 11:16attributes become the columns in the
- 11:18table. So, if we wanted to design this
- 11:21out as a table,
- 11:23it would look something like this. And
- 11:25I'm going to give it an ID column, the
- 11:28poster ID, the timestamp, I'll just
- 11:31write TS to keep it quick. And let's say
- 11:33these are videos, so we could say the
- 11:35video ID, what video we're commenting
- 11:38on. These will be the columns, and then
- 11:41we could give specific data, which would
- 11:44be the entities.
- 11:47So, ID 7,
- 11:50poster ID 32, timestamp,
- 11:53some really long number, video ID 72.
- 11:57And this would represent one comment. We
- 11:59could then add another one, and this
- 12:01person commented on the same video. So,
- 12:03in this situation, I'm just using
- 12:05numbers, and you might be thinking,
- 12:06well, where's the actual data? Well,
- 12:09that's one of the benefits of
- 12:10relationships. We can tie this to
- 12:14another table for videos.
- 12:19And then we'll have a video in there
- 12:20with the ID of 72.
- 12:23So that's how we know that this comment
- 12:25is associated with that video, and these
- 12:28are connected in the database using
- 12:30foreign keys. So we'll get into all of
- 12:32that. Then every single row is going to
- 12:35have an ID.
- 12:38That's how you can uniquely identify
- 12:40that row. So these do not reference
- 12:42another table. These describe that
- 12:44specific comment. So if you wanted to
- 12:47refer to a specific comment, you could
- 12:49say comment with the ID of nine, and
- 12:52that will give you that exact row. So to
- 12:54continue with some of the vocabulary, we
- 12:56say row, but you might also hear tuple,
- 12:59pretty common in more formal database
- 13:02design classes and stuff like that, or
- 13:04you might hear record.
- 13:05And this is basically just an entry in
- 13:07the database. For the columns, we'll
- 13:10have attributes
- 13:13or you'll often hear fields. Again,
- 13:16you're probably not going to get quizzed
- 13:17on these things, but you will run into
- 13:19these as you're studying database
- 13:20design, so it's good to be familiar with
- 13:22them. So why do we need to make these
- 13:23things random numbers that connect to
- 13:25other tables? And why do we have to
- 13:27split up the data across other tables?
- 13:30Well, it's all part of a system to build
- 13:32and protect data integrity.
- 13:35So data integrity is basically saying
- 13:38that the data is trustworthy, the
- 13:40existing data that's already there, and
- 13:42that the structure of the database will
- 13:44protect us from bad data or data that
- 13:47ruins our data integrity. So there is a
- 13:50few different classifications.
- 13:53So I'll just write the different options
- 13:55up here. These are the different angles
- 13:57that you want to think about protecting
- 13:59your data. So entity integrity is all
- 14:02about looking at a single row, one
- 14:04entity, and trusting that data, making
- 14:07sure that there's no duplicates, so that
- 14:09comment is not in that database
- 14:11mistakenly twice. Additionally, that row
- 14:13is dedicated to just one individual
- 14:15comment. We're not trying to cram
- 14:17multiple comments into a single row.
- 14:19This one's relatively easy to achieve
- 14:21with a couple of design decisions, which
- 14:23we'll talk about with relationships and
- 14:25normalization. Basically, this answers
- 14:27the question, can I look at a row and
- 14:29trust that data? Referential integrity,
- 14:31this goes into the relationships across
- 14:33tables with foreign keys. What we don't
- 14:36want is a pointer that points to
- 14:37nothing. So if 72 doesn't exist, that's
- 14:40a problem. There are certain situations
- 14:43where you do want that, but by default,
- 14:45you don't unless you explicitly do.
- 14:47Hopefully, that's not like super
- 14:49confusing. Basically, in general, we are
- 14:52going to want to always make sure that
- 14:56this is a strong connection
- 14:59and that we can't go in here and make
- 15:00any bogus data. So if we go in here and
- 15:02change this to 77 and 77 doesn't exist,
- 15:06well, the database should say, yo, that
- 15:08doesn't work. You need to actually pick
- 15:10a value that exists. Domain integrity,
- 15:13this is specific to the data that is
- 15:15stored in the database. Does it make
- 15:18sense for what you're trying to
- 15:19represent? This can be achieved a few
- 15:22different ways.
- 15:24Data types will get us the basics.
- 15:27Lookup tables,
- 15:29this allows us to choose from different
- 15:30options. You can also use things like
- 15:33enums.
- 15:34Then many databases will have check
- 15:36constraints so you can actually check
- 15:38the value, making sure it's not too big
- 15:41or too small. You can even have stored
- 15:44procedures or other gates that the data
- 15:46has to get through in order for it to
- 15:48get to the database.
- 15:50These are all things you can do at the
- 15:51database level, but oftentimes that
- 15:54domain integrity will be pushed up to
- 15:56some degree to the application level. So
- 15:58for example, if you are doing a marathon
- 16:03training app and you have the database
- 16:06and one of the questions on the
- 16:07introduction survey is, hey, what is
- 16:10your marathon time if you've done a
- 16:12marathon? And somebody goes in there and
- 16:14puts negative 71, something that makes
- 16:16no sense in that context. Well, that is
- 16:19validation that we could check for at
- 16:21the application level.
- 16:26So some things will delegate to the
- 16:28database through things like check
- 16:30constraints, but oftentimes you can just
- 16:32do it at the application level and use
- 16:34the database for the main things, which
- 16:36is entity integrity, referential
- 16:38integrity, data types,
- 16:41and lookup tables for certain
- 16:44situations. So that's how I would
- 16:46usually start until you need specific
- 16:48scenarios in the database adding in
- 16:50those check constraints or other gates
- 16:52for that data to make it to the
- 16:54database. This would probably only be
- 16:56the case if you have multiple locations
- 16:59accessing that database. So let's say
- 17:00you have another app that also uses that
- 17:04database.
- 17:06Well, now you'd have to copy that
- 17:08validation over twice. So you could move
- 17:10that validation to the database or to a
- 17:13database wrapper that checks anything
- 17:16that goes through to the database. So
- 17:18imagine your database here and then you
- 17:20write a small piece of software that
- 17:23does data validation and puts data in
- 17:25the database or gets data back out. And
- 17:28then you can connect to that from your
- 17:30backend.
- 17:31And then if you have two backends,
- 17:35they both have to go through that same
- 17:39gate. So yeah, what I'm trying to say
- 17:40here is there are often different ways
- 17:42to achieve similar results. You can do
- 17:44it at the application level in your
- 17:46backend with data validation. You can do
- 17:49it at the database level inside the
- 17:50database with things like check
- 17:52constraints or you could write a
- 17:54dedicated service to access the database
- 17:56that does just data validation, which
- 18:00could then be the gate for any of the
- 18:02backend software going through that
- 18:04database. Not trying to overcomplicate
- 18:06it. You don't need to understand all of
- 18:07the different scenarios because this
- 18:09isn't like a system design or backend
- 18:11engineering video. We're just wanting to
- 18:13learn about data integrity at this
- 18:15point.
- 18:16The main things that we're going to
- 18:17focus on are these two because those are
- 18:19the core of design. This domain stuff is
- 18:22basically specific to whatever you're
- 18:25doing, and that's what domain here
- 18:27means. So the actual data values
- 18:29themselves, and not so much the database
- 18:32structure. So this deals with the values
- 18:35of the data, and this deals more with
- 18:37the structure of the DB. So that's a
- 18:39pretty decent way to think about it. And
- 18:41another thing just to keep in your mind
- 18:43as you are going through building a
- 18:45database, you want to never have to be
- 18:48careful.
- 18:50This seems weird, right? Because like,
- 18:53wouldn't we want to be careful if we are
- 18:55working on a database? The answer is
- 18:57yes, you should be careful, but you
- 18:59should design a system that will be fine
- 19:02even if you're not being careful. So you
- 19:04shouldn't have to remember to make data
- 19:06connect properly or to only put certain
- 19:09values in the tables. That requires
- 19:11human effort, and being careful
- 19:14ultimately means someone will mess up.
- 19:17This is the same reason why you should
- 19:19avoid having production connection
- 19:21strings on your local system, cuz even
- 19:24if you're not touching them and you only
- 19:25use them when you need them, just by
- 19:27having that access, at some point you're
- 19:31going to mess something up. So this is
- 19:32why in larger corporations, there will
- 19:34be dedicated people to working with the
- 19:36database and protecting the database.
- 19:38And then if you're a developer, you have
- 19:39to basically get approved to even work
- 19:42with the database. It's because the
- 19:44database administrator's job is to set
- 19:47up the database, configure it, and also
- 19:49protect it. The more that we can force
- 19:51in our database structure, the better.
- 19:53All right, let's start designing some
- 19:54systems. As you build an intuition for
- 19:56database design, you don't have to start
- 19:58from scratch every single time. You can
- 20:00kind of just do it. But for this video,
- 20:03we're going to start with pure sentences
- 20:06of what user behavior we need to
- 20:07support, and then I'll show you how you
- 20:09can break those down into certain
- 20:11tables, attributes, and relationships.
- 20:13So let's just go through some examples
- 20:15for a video platform.
- 20:19The goal here is not to be comprehensive
- 20:22necessarily. You can always add in
- 20:24It's just that the more you think of up
- 20:26front, the easier time you're going to
- 20:28have building the system. So, what can a
- 20:31user do?
- 20:34A user can create a profile. We'll
- 20:39just say channel. A channel uploads
- 20:41videos.
- 20:44A user subscribes to channel.
- 20:48Hint hint.
- 20:52A user comments
- 20:55on a video.
- 20:57A comment can be a reply.
- 21:01A user can create playlists
- 21:04and add videos to the playlist.
- 21:09And then users can like or dislike. Now,
- 21:12I would just have to go through this
- 21:13list and identify any nouns and any
- 21:16verbs. Nouns will likely become tables.
- 21:19Verbs will become certain relationships
- 21:22across tables. And then any specific
- 21:24details about a noun can be an attribute
- 21:27or a column in that table. So, let's
- 21:30just go through this exercise. I know it
- 21:31seems kind of obnoxious or unnecessary,
- 21:34but let's just do it. We have a user. I
- 21:37will square the nouns and then underline
- 21:40actions. So, we have a channel.
- 21:44Uploads videos. So, uploads is an
- 21:47action. A user again subs to a channel.
- 21:53We have user comments
- 21:56on a video.
- 21:59Comments can be a reply. I guess that's
- 22:03a verb. Some of these you might need to
- 22:05like circle back to and think through,
- 22:07is a reply a noun or is it an action?
- 22:10So, any that you're unsure of, just put
- 22:12a mark by and you can circle back. So,
- 22:14again we have a user create playlists.
- 22:17We can add and playlist again.
- 22:20We can like and dislike. So, now we'll
- 22:23just get rid of any duplicates here.
- 22:25I'll just them pretty small over here
- 22:27and then we can analyze in more detail.
- 22:30So we have a user, we have channel, we
- 22:32have videos.
- 22:36I'm just doing nouns here if that wasn't
- 22:37obvious. We have comments and we have
- 22:40playlists.
- 22:42So likes and dislikes, that's a little
- 22:44bit more abstract, but that is going to
- 22:46be an entity because it's just
- 22:48describing a reaction. So we'll store
- 22:51reactions.
- 22:53And then these are examples of
- 22:56reactions. And then if there's any
- 22:58you're unsure of, such as a comment
- 23:00being a reply, is a reply a dedicated
- 23:02noun that should get its own table or is
- 23:05there some way that we can store this?
- 23:07Well, a reply doesn't have any
- 23:08additional information compared to a
- 23:10comment. So a comment can just be marked
- 23:13as a reply to another comment or not a
- 23:16reply to another comment. It's actually
- 23:18an attribute of a comment. So for me, a
- 23:21reply is not going to get a dedicated
- 23:23table. I'll just have it as an attribute
- 23:25within that comment table. So hopefully
- 23:28that makes sense for that one. And
- 23:29there's a reason why this one was
- 23:31special and I called it out because I
- 23:33mentioned that verbs will become their
- 23:36own relationships. And this is a verb.
- 23:39It's just that the relationship is with
- 23:41itself. A comment replies to a comment.
- 23:44So I think that might be why it's a
- 23:46little bit confusing initially, but the
- 23:49system that I described still makes
- 23:50sense.
- 23:51So now let's go through the verbs and
- 23:52you'll be able to see that. So a user
- 23:55can create a channel. Any of these verbs
- 23:57will be a relationship between those
- 23:59tables. So a relationship between a user
- 24:03and a channel. There will be a
- 24:05relationship between a channel and a
- 24:08video that is uploaded to the website.
- 24:11There will be a relationship between the
- 24:13user and a channel that the user
- 24:16subscribed to. There will be a
- 24:17relationship between the user and the
- 24:21video that the user comments on. There
- 24:24will be a relationship again with
- 24:26comments pointing to other comments.
- 24:28There be a relationship between users
- 24:30and playlists, and then a relationship
- 24:33between videos and playlists. And then
- 24:35it comes down to just knowing what type
- 24:37of relationship that is needed. Is it a
- 24:39one-to-one, a one-to-many, or a
- 24:41many-to-many? So we'll get into those in
- 24:43this video. Let's learn the different
- 24:45types of relationships, and then we can
- 24:46apply that to what we've learned here.
- 24:48So let's talk about database
- 24:50relationships.
- 24:53One of the most important topics. And
- 24:55when you get even into database
- 24:57normalization and some of these other
- 24:59popular things with database design,
- 25:01it's all built on database
- 25:02relationships. So if you learn these,
- 25:05going through the normalization
- 25:06principles will be super, super easy. So
- 25:08there's three classifications here. We
- 25:10have one to one, one to many,
- 25:17and many to many.
- 25:21You might see these as 1 1 1 m and m n.
- 25:29Let's go through each one of these, and
- 25:30let me tell you, the last one will
- 25:32surprise you. A one-to-one relationship
- 25:35is when two entities are exclusive. And
- 25:37this is often a design choice, so it's
- 25:40not always there's a correct answer. And
- 25:42that's a common misunderstanding. I
- 25:44actually get a lot of comments people
- 25:45saying, hey, I need help with this
- 25:48database design. Should it be this way
- 25:49or this way? What's the correct answer?
- 25:52And it comes down to the business choice
- 25:54of which one you want. So there's not a
- 25:57correct answer all of the time for
- 25:59design choices. What exactly do I mean?
- 26:01Let's go through a quick example. So in
- 26:03the previous list, we had user and we
- 26:07had channel. So you could say these
- 26:10rules apply. A user has at most
- 26:14one channel.
- 26:16And the other way as well, a channel
- 26:21can be owned by at most one user. This
- 26:27is a classic one-to-one relationship. A
- 26:29user cannot own multiple channels, and a
- 26:31channel cannot be owned by multiple
- 26:33users. So, is this design correct? That
- 26:36comes down to the business choice. Do we
- 26:39want users to be able to create multiple
- 26:41channels? Do we want to make it so that
- 26:43a channel can be owned by multiple
- 26:45users? Often times, platforms do allow
- 26:47you to create multiple instances of
- 26:50whatever it is you're creating. In that
- 26:52situation, it would no longer be a
- 26:54one-to-one relationship. So, these
- 26:56things are exclusive. They're tied to
- 26:57each other. So, there are two ways to
- 27:00design
- 27:03a one-to-one relationship.
- 27:07Since the values are coupled, you could
- 27:08just use a column.
- 27:11That's the first approach. I'll show you
- 27:12what that looks like. I'll write the
- 27:13second approach down here. And that's to
- 27:16use a separate table.
- 27:18You don't always need a separate table.
- 27:20So, let's create a user.
- 27:25We'll say Caleb at CalebCurry.com. There
- 27:28can be any data about this user as well,
- 27:31such as when they signed up. So, we'll
- 27:32say created at. We might have their
- 27:35birth date or their age for any kind of
- 27:38content verification. We might even have
- 27:41something like their hashed password.
- 27:45Then, we could just have an attribute on
- 27:47here which is their channel
- 27:52handle.
- 27:55Let's say, for example, coding
- 27:57breakthrough.
- 27:58So, this column is enough to make that
- 28:01association in the database. So, that's
- 28:04the first way and it's the default way I
- 28:06would think about one-to-one
- 28:07relationships. There are situations
- 28:09though when you want to split it out
- 28:10into a separate table. Usually, that
- 28:12will be because of some certain reasons.
- 28:14So, if you have transitive dependencies,
- 28:17which are basically attributes about the
- 28:20channel, you can kind of visualize it as
- 28:23sub data here. Things about the channel.
- 28:28That would be a reason why you might
- 28:30want to use a separate table. And the
- 28:32other reason would be separation. So I
- 28:35see this a lot where we'll have a user
- 28:37profile or the user profile's personal
- 28:40information like addresses, and that's
- 28:42stored in a completely separate table
- 28:44just for that data isolation, as well as
- 28:47thinning out the primary row. So that
- 28:50way we don't have a ton of information
- 28:51in that row, we just grab what is needed
- 28:54for that user account. So for example,
- 28:56let's say we had the address here, we
- 28:58had their credit card number,
- 29:01and let's say we had their social
- 29:02security number or some other sensitive
- 29:04data. Well, instead of storing all of
- 29:06this within that one table, we could
- 29:08take this, separate it out, and then
- 29:11just connect it back to that original
- 29:13table. So you're still using a
- 29:15one-to-one relationship, it's just split
- 29:16across two tables. So wasn't trying to
- 29:19get too off topic here with the
- 29:20sensitive information stuff, but that's
- 29:23the other common approach you'll see
- 29:25when it comes to one-to-one
- 29:26relationships. It's a true relationship
- 29:28across tables, but it's still a
- 29:30one-to-one association. In theory, you
- 29:33could design it just as columns in the
- 29:35same table, but it's split out for some
- 29:37reason. So just to review that, the two
- 29:39ways to design a one-to-one relationship
- 29:41is to use a column or use a separate
- 29:43table and then connect it with a foreign
- 29:45key. And the reason you would want to do
- 29:47two separate tables is transitive
- 29:49dependencies or for separation due to
- 29:52privacy or the size of data. Let's now
- 29:54talk about a one-to-many relationship
- 29:57because it's just a small change. And in
- 29:59fact, you can go from a one-to-one
- 30:01relationship to a one-to-many
- 30:04relationship very easily if those tables
- 30:07are split out like we had just
- 30:09explained, splitting out this channel
- 30:11section into its own table. So let's
- 30:13talk about that design. So let's first
- 30:15start by defining it as a one-to-one
- 30:18relationship
- 30:21across tables.
- 30:25So we have users,
- 30:31and then we have channels.
- 30:35And And within channels, we'll have an
- 30:36ID
- 30:38and then the handle. And then over here
- 30:41on the user, we'll have a user ID, let's
- 30:43say 32. And then we'll have a reference
- 30:46to that channel. So this is the foreign
- 30:48key. It points from this table to this
- 30:51table. However, with a one to one
- 30:53relationship, it can point either way.
- 30:55So instead of the user pointing to the
- 30:57channel, you can have the channel
- 31:00pointing to the user. Doesn't really
- 31:01matter which way because they're
- 31:03exclusive. However, if we do want to
- 31:05switch to a one to many relationship
- 31:07later, then we can design it easier by
- 31:09where we put that key. So what I'm going
- 31:11to do, instead of pointing from the
- 31:13users to the channels, I'm
- 31:16going to go the other way.
- 31:21So in that situation, we do not need to
- 31:23reference the channels in the users
- 31:25table. So we might just have ID and
- 31:28email. And then over in the channels
- 31:31table,
- 31:33we would add in a user column saying who
- 31:37it's associated with.
- 31:40So that would be 32. And this points
- 31:43back to this row over here.
- 31:47So that is a foreign key. Now, when we
- 31:50get into the details of foreign keys,
- 31:52you can learn that you can make a
- 31:53foreign key unique. So that's one of the
- 31:56attributes we're going to put on this.
- 32:02And when we label that unique, it
- 32:04prevents us from using 32 in this table
- 32:07again. So if we created another channel
- 32:11here, let's say channel with the ID two,
- 32:14and we'll call this one Code Break.
- 32:17And then we tried to say that this was
- 32:19owned by that same user, this would be
- 32:23blocked by the database. That's what
- 32:25that unique constraint is going to do.
- 32:28So we have a one to one relationship
- 32:31over two tables where we have the
- 32:33foreign key in the channels pointing
- 32:35back to the users table. That unique
- 32:38prevents us from inserting multiple rows
- 32:42with repeating values. So this is not
- 32:44allowed. So the switch to a one to many
- 32:47then is very simple. All we have to do
- 32:53is change that
- 32:56and remove
- 32:59that unique constraint.
- 33:03And now this is allowed by the database
- 33:06and now Caleb owns two channels, Code
- 33:09Breakthrough and Code Break. So it
- 33:11basically looks like this.
- 33:14Here's me all happy, ID of 32, and we
- 33:18have two channels. Code Breakthrough,
- 33:20which is entirely too long. And then
- 33:24Code Break, which I kind of like cuz it
- 33:27describes my code. It's always broken.
- 33:29Both of these channels
- 33:32can have their own videos and
- 33:35they are both owned by that same person.
- 33:40So that is a one to many. So the
- 33:42difference between a one to one on two
- 33:45tables and a one to many also on two
- 33:48tables is just the unique constraint on
- 33:51the foreign key. So just to make sure
- 33:53you didn't miss that, one to one
- 33:57unique foreign key.
- 34:00One to many
- 34:03not unique foreign key.
- 34:06It's really that simple and which one
- 34:07you want to use comes down to the choice
- 34:09of how you want to design your app. If
- 34:12you want people to be able to create
- 34:14multiple channels, then you would do it
- 34:16as a one to many not using unique. If
- 34:18you wanted to prevent multiple channels,
- 34:20then you would use unique. All right,
- 34:22are you ready for things to get a little
- 34:23crazy? What if we wanted a user to be
- 34:26able to create multiple channels, but
- 34:29that channel can be managed and owned by
- 34:32multiple users? Oh no, that's a problem.
- 34:35Now we have an issue. Let's say we have
- 34:37another user over here,
- 34:40[email protected].
- 34:42So we go in here and we add a new row,
- 34:44and let's say he's sharing this code
- 34:46break channel.
- 34:52And this time it's pointing back to 47.
- 34:58This is the first approach people often
- 35:02take, but this is wrong and bad. Can you
- 35:05guess why this is wrong and bad? Great
- 35:10job. Exactly. We have repeating data
- 35:13here. Additionally, we have two IDs for
- 35:16the same channel. That's kind of a big
- 35:19problem here. So when we want to do a
- 35:22many to many,
- 35:25we have to use a third table, which is
- 35:27often called a junction table or
- 35:29intermediary table or a table that you
- 35:32put between two other tables to resolve
- 35:33a many to many relationship by splitting
- 35:35it up into two one to many
- 35:37relationships. That's usually the name I
- 35:39go with. So I'm going to erase some of
- 35:41this here. But first I wanted to draw
- 35:44the visual of what this would look like.
- 35:46So we have Caleb here,
- 35:48and then we have James here.
- 35:53And he also is going to be the owner of
- 35:57this channel. So we need to introduce a
- 35:59table
- 36:01to resolve this tension. So we have
- 36:03users and we have channels. And then by
- 36:06convention, it's kind of stupid, but
- 36:09you'll just see the combination of
- 36:10these, such as user channels.
- 36:13That's typically what I'll do, where
- 36:14I'll make one of them singular and one
- 36:16of them plural, but you'll see
- 36:17variations. So especially even between
- 36:20database systems, the conventions will
- 36:22sometimes vary.
- 36:24But one thing that's pretty common is
- 36:25using the underscores for multiple words
- 36:28as SQL is often going to lowercase
- 36:31anything you type. So to save on typing
- 36:33without having to use quotes everywhere,
- 36:35you can use underscores for the tables.
- 36:38And this table is going to be pretty
- 36:40simple. We're just going to have the
- 36:41user on one side and then the channel on
- 36:44the other. So, let's go over to the
- 36:46broken part here. We will put the user
- 36:49on one side, 32 and then the channel on
- 36:54the other.
- 36:55Again, 47 and then the channel on the
- 36:59other, which this was incorrect, so
- 37:00we'll want to use the actual correct
- 37:03value here, too. This allows us to get
- 37:05rid of this row here, which is corrupt,
- 37:11and just have the single source of truth
- 37:13row here. It will no longer store the
- 37:17user owner in this channels table
- 37:19because all of that's going to be
- 37:20managed right here. Which means we also
- 37:22need to add this first one in here. So,
- 37:25that was user 32 owning channel with the
- 37:28ID of one. So, that allows us to do the
- 37:32many-to-many relationship. We're
- 37:33basically jumping through this other
- 37:35table. And if you want, you can draw it
- 37:38out. So, let's do this top one first.
- 37:41The 32 is over here, and then the one
- 37:45goes to here.
- 37:47The second one here, 47
- 37:49goes to here, and the two goes to here.
- 37:53Then
- 37:54we'll have another one from 32
- 38:00which goes to here as well. Wow, the
- 38:03arrows probably just made it way more
- 38:04confusing. I'm sorry. Now, the cool
- 38:06thing here with the many-to-many
- 38:07relationship, we now have the perfect
- 38:09home for any attributes that describe
- 38:13the relationship between data. So, the
- 38:15first initial design might make James
- 38:18and Caleb co-owner of the channel. But
- 38:20realistically, you might want to have
- 38:22users that have restricted permissions.
- 38:25Maybe they are an editor or video poster
- 38:28or maybe they can manage comments, but
- 38:29they can't go in and delete stuff. Those
- 38:32tiers describe the relationship between
- 38:36a user and that channel. So, what we
- 38:38could do
- 38:41is we could scoot this over.
- 38:45And notice how I made the box enough.
- 38:48Thinking ahead. Usually don't do that.
- 38:50And here you can describe that
- 38:53relationship between the user and the
- 38:55channel. So for example, we could say
- 38:57owner, and then maybe manager, which is
- 39:00slightly less permissions, and then
- 39:03owner.
- 39:04So now James is basically working with
- 39:07that channel, but he's not the direct
- 39:09owner of that channel. So that's one of
- 39:11the cool things with many to many
- 39:12relationships. You can add in attributes
- 39:15here that describe that junction. You
- 39:17might also have timestamps and other
- 39:18things such as when that was
- 39:20established. So we haven't really gotten
- 39:21into keys a whole lot, but you've
- 39:23noticed I've been using these numbers.
- 39:25Well, on tables like users or channels,
- 39:27these will be unique, meaning you can't
- 39:30reuse the same user ID or the same
- 39:32channel ID.
- 39:33But for user channels, you can have
- 39:35repeats for any specific column. You
- 39:39just can't have the combination
- 39:40repeated. So you can think of bundling
- 39:43these together.
- 39:46And the combination has to be unique.
- 39:49So an example of something that would
- 39:51not work would be 32 two
- 39:55manager.
- 39:57Because in that situation, we would be
- 39:58the owner and the manager. We have data
- 40:00that doesn't mix, and that would throw
- 40:02the data integrity into question. So the
- 40:05constraints will force data integrity by
- 40:08saying the combination of these have to
- 40:10be unique. So there's really multiple
- 40:12constraints here. We have the unique,
- 40:14and then we have two x foreign keys. So
- 40:18here's a foreign key. Here's a foreign
- 40:21key, and then here's a unique
- 40:23constraint. Now, as I mentioned, once
- 40:25you build a stronger intuition for
- 40:27database design, you'll start to just
- 40:29notice many to many relationships. So
- 40:31some of the examples we had earlier fit
- 40:33nicely into a many to many relationship.
- 40:36Let me go through one example with you.
- 40:37So we talked about videos being added to
- 40:40a playlist.
- 40:43So we have this verb, so we know there's
- 40:45some relationship here. We just have to
- 40:47decide is it one to one, one to many, or
- 40:49many to many. The way you do that is you
- 40:51take one of the entities, videos,
- 40:55and you ask, can this be added to many
- 40:58playlists?
- 41:00And then you ask the same thing the
- 41:02other direction. So, playlist.
- 41:07Can many videos
- 41:10be added to the playlist?
- 41:15And the obvious answer here is also
- 41:18going to be yes. If you get two yeses,
- 41:21then you have a many to many
- 41:22relationship. If you get one yes, then
- 41:24you have a one to many relationship
- 41:26where the child, the table that has the
- 41:28foreign key, is the many side. That
- 41:30doesn't apply here, so let's just focus
- 41:32on the many to many here. This is what
- 41:34it'll end up looking like. Following
- 41:36that same pattern, we'll have videos and
- 41:39then we'll have playlists.
- 41:42And I don't like video playlists, so I
- 41:45usually like to word it
- 41:48left to right. So, I'm going to
- 41:49rearrange this. It's the same exact
- 41:51idea. I'm just
- 41:53being weird with the wording here. So,
- 41:55we have playlists
- 41:58and videos, and then we have
- 42:03playlist videos. And this table here
- 42:05acts purely just as a way to associate
- 42:08these two, but as I mentioned, you can
- 42:10often add in other attributes in there
- 42:12and then it grows up into its own
- 42:14entity. So, playlist videos can also
- 42:17contain the position of a video within a
- 42:20playlist or the ordering or any other
- 42:23attributes that describe that
- 42:25connection. So, let's just go through a
- 42:27quick example. We have two playlists. We
- 42:29have two videos. Within the playlist, we
- 42:32add the first video and then we add the
- 42:36second video. Dude, I always draw up and
- 42:38it makes me sick. Let's add a couple
- 42:40more values here just so we can see
- 42:42better. And
- 42:44we'll
- 42:47say playlist three has video four
- 42:51and video one. And we'll also put in
- 42:54here video five. So in this example, we
- 42:58have five playlists. Two of the
- 43:01playlists actually have videos in it.
- 43:04The first one here has two videos. The
- 43:07second one here has three videos. So
- 43:09I'll draw the playlist like this.
- 43:13One with two videos
- 43:18and the other one with three videos.
- 43:21Now, the ordering you could put as
- 43:24another attribute. There's a downside to
- 43:26that in that if you move data around,
- 43:29you're going to have to do an update
- 43:31across that playlist's videos. But it's
- 43:34a pretty simple design and a small
- 43:35tradeoff. So for example, you could have
- 43:37position one, position two, position
- 43:41one, two, and three. Don't necessarily
- 43:44have to be sequential. But if you're
- 43:47adding the rows, it's probably naturally
- 43:49going to increment like that. And that
- 43:51basically just describes where in the
- 43:54playlist
- 43:56those videos go. So that's one example
- 43:59of how you could do basic ordering. And
- 44:01then with a rearrangement, you can
- 44:02recalculate the numbers for those rows.
- 44:05There are various other approaches to
- 44:07ordering, so I'm not saying that's the
- 44:09only way you should do it, but in my
- 44:11opinion, it's one of the simplest to
- 44:12wrap your mind around. So those are
- 44:14junction tables. You might see some
- 44:16other approaches to achieving similar
- 44:18results.
- 44:20So for example, instead of having three
- 44:22tables, you could just have an array.
- 44:25And that is a formal type in some
- 44:27databases, but you lose a lot of
- 44:29benefits when you do that as well.
- 44:31There's tradeoffs. It's going to be
- 44:33simpler. It's going to probably be
- 44:34faster. You don't have to do as many
- 44:36joins, but there are downsides as well,
- 44:38such as losing that referential
- 44:40integrity. So instead of having this
- 44:42table all together, you could just
- 44:45denormalize this, which is the process
- 44:47of going into fewer tables. And one of
- 44:49the attributes on here would just be the
- 44:51videos that the playlist has and the
- 44:55position within that array would be the
- 44:57ordering. The major downside here is you
- 45:00don't have the enforcement of this
- 45:03connection. There's no foreign key. Now,
- 45:06maybe you could do something to get that
- 45:09behavior, such as database triggers, but
- 45:12the three table structure uses native
- 45:15foreign keys and is probably the proper
- 45:18way of doing it. There's always
- 45:19tradeoffs though. Some people will want
- 45:22to have less data fragmenting across
- 45:24tables and they'll give up some
- 45:27integrity in that process if they can
- 45:29enforce that integrity some other way.
- 45:31The nice benefit here is that the
- 45:32position of the ID in the array
- 45:34determines that order. Now, I'm not
- 45:36saying you should do that instead, I'm
- 45:38just giving some options that you might
- 45:40see. So some of the other connections
- 45:43that we had in that large list was
- 45:45between a channel and the channel
- 45:47uploading videos. So basically we're
- 45:50connecting a video back to a channel
- 45:52instead of a user, which does better
- 45:54support channels that are owned by
- 45:56multiple users.
- 45:58This would be a one to many
- 46:01because a channel can have multiple
- 46:03videos, but a specific video cannot be
- 46:05owned by multiple channels. That's
- 46:07definitely one that just makes sense as
- 46:09one to many. You wouldn't want to do
- 46:10that as a many to many. However, you
- 46:13could consider things like
- 46:14collaborations.
- 46:16So maybe
- 46:19you do want to consider different
- 46:20options. But you could maybe have a
- 46:22video posted by a channel and then tag
- 46:24some other channel. Kind of going off
- 46:26the rails here. By default, I would do
- 46:27one to many for this. Then we had
- 46:29subscriptions. So a user and channel.
- 46:33This is a different connection than a
- 46:35user creating a channel. It's a user
- 46:36subscribing to a channel, which again is
- 46:40going to be many to many. A user
- 46:43commenting on a video.
- 46:45So this is going to be a relationship
- 46:47between the user and the video. And this
- 46:50is going to be many to many. Now, when
- 46:54you see a many to many, you can go with
- 46:56that default junction table name that I
- 46:59described, where we take the two
- 47:01different tables and combine the word,
- 47:03or you can give a custom name specific
- 47:05to that relationship. So if this is a
- 47:08user commenting on a video, rather than
- 47:11doing user videos, you could just say
- 47:13comments.
- 47:18And then what was this one? I think
- 47:19subscriptions.
- 47:22The next one was a user being able to
- 47:24create a playlist.
- 47:27This would be a one to many because the
- 47:31playlist is owned by one user. We
- 47:33already talked about adding videos to
- 47:35playlists. And then the last one I think
- 47:37that we had in that large list was likes
- 47:39and dislikes, which I would just say
- 47:41that is a combination between the user
- 47:46and the video, but I would give it a
- 47:48specific title
- 47:51uh reactions.
- 47:54So our V1
- 47:57of our database design,
- 48:00the user, channel, videos, comments,
- 48:05subscriptions,
- 48:07reactions.
- 48:09If we did the multiple channel
- 48:10ownership, we would need to add that
- 48:12table in there. And I think that's it.
- 48:14I'm trying to think if there's any other
- 48:16junction tables that I missed, but we
- 48:18got these ones.
- 48:20And we're going to go with a one to many
- 48:22for the user creating an account and
- 48:25setting up a channel. So we don't need a
- 48:27junction table for that. And then
- 48:29comments were just a one to one
- 48:30attribute replying to another comment.
- 48:33There might be other tables or
- 48:35interactions that we're not considering,
- 48:37but obviously, if you're going to build
- 48:39something like YouTube, there's going to
- 48:41be a lot more than just this right here.
- 48:43So let's talk about one last thing when
- 48:46it comes to relationships.
- 48:49And when it get a little freaky, you can
- 48:51have relationships that go three way or
- 48:55more. We talked briefly about channels
- 48:57being managed by multiple users. So we
- 49:00have that disconnect between the user
- 49:01and the channel. And then I said you
- 49:03could add in any attributes such as the
- 49:06permission level for that user. But this
- 49:09could be a situation where three keys
- 49:12could be useful.
- 49:14So splitting this up like so, where this
- 49:18is dedicated to the user.
- 49:21This column is dedicated to the channel
- 49:26table.
- 49:28And this column, instead of just putting
- 49:31the permission level there inline as a
- 49:35string or an enum value, we could
- 49:38reference another table. We'll call it
- 49:41the permissions table. And here you
- 49:43would define the possible permission
- 49:45levels.
- 49:48So one might be owner.
- 49:52Two is admin.
- 49:56Three is editor. So let's say we have
- 50:00user one here,
- 50:03channel one here. So we could have a row
- 50:05like one, one,
- 50:09and two. So what this means is we have
- 50:12the user ID,
- 50:17channel ID, and the permission ID. So
- 50:21the user with the user ID of one is an
- 50:25admin of the channel with the ID of one.
- 50:29In this situation, you can use a unique
- 50:32constraint across three columns. So
- 50:37across user ID,
- 50:42channel ID, and permission ID. Yeah.
- 50:45Totally didn't write uphill or anything.
- 50:47And we'll get into keys, but it's
- 50:49possible just to create this as the
- 50:50primary key. But usually I like to have
- 50:53an ID even for these junction tables.
- 50:56But again, let's get into keys in a few
- 50:59minutes, okay? Calm down. Now, something
- 51:01you should be aware of is what this
- 51:02structure does is it allows a user to
- 51:05have multiple permissions because we
- 51:08could go in here and say 1 1 3.
- 51:12So in that situation, let's say this is
- 51:15Caleb. Caleb is an admin and an editor.
- 51:20That can work if these permissions are
- 51:23not exclusive. So, you know, maybe you
- 51:25can be the editor and the manager, and
- 51:27the manager can manage comments and
- 51:29stuff like that. The editor can manage
- 51:31videos, and you could have both of those
- 51:33independently. Again, this often comes
- 51:35down to just the design of the app. If
- 51:37you don't want somebody to be able to
- 51:39choose multiple of these, then you would
- 51:41just remove the permission ID from the
- 51:45key, and then you would still have this
- 51:46as a foreign key, but you couldn't
- 51:49repeat the same user ID and channel, so
- 51:51you would only be allowed to have one
- 51:53permission ID. So we're getting into the
- 51:54details of multiple keys and stuff, but
- 51:57I just want you to have some intuition
- 51:59about how changing what you put in the
- 52:02constraint affects the way the database
- 52:04functions, determining whether things
- 52:06can go together or can't go together or
- 52:08if things have to be unique and so
- 52:10forth. It all comes down to just
- 52:11designing a few constraints. Now, this
- 52:14approach, we talked about it briefly
- 52:16earlier when I was talking about channel
- 52:18permissions, but I didn't talk about
- 52:20this separate table. This idea is called
- 52:23a lookup table.
- 52:26And this is basically a universal way to
- 52:30create an enum where you can choose from
- 52:33one of a few possible values.
- 52:37But it's very easy to, with a lookup
- 52:40table,
- 52:44update the name
- 52:47or any other attributes. So if we wanted
- 52:49to change it from editor to content
- 52:52manager or whatever, we can just change
- 52:54that row.
- 52:55We don't have to go change a string in a
- 52:57hundred different places, and we don't
- 52:59have to change a type if we're working
- 53:00with enums. And we can easily add more
- 53:04in the same way just by adding a new row
- 53:08here. So lookup tables are basically
- 53:11one-to-many relationships used to
- 53:13reference some value and then you can
- 53:15put any information in additional
- 53:18columns describing that value if needed.
- 53:21So that's all I have on relationships.
- 53:25So up next, we're going to talk about
- 53:26primary keys and foreign keys. A key is
- 53:29basically how we tell one row apart from
- 53:32another row.
- 53:33There needs to be something unique about
- 53:35that row so we can talk about that row
- 53:37specifically. Every table within your
- 53:40database needs to have a primary key.
- 53:43When you set a column as the primary
- 53:45key, it's going to be indexed, meaning
- 53:48we can access by that key very quickly,
- 53:50and it will be unique, so you cannot use
- 53:52that same primary key value again for
- 53:55that table. So if you have a comment
- 53:57with the ID of 300, you're not going to
- 53:59be able to create another comment with
- 54:01that same ID. So the key should be
- 54:04unique, but it should also never change.
- 54:06So you should never have a situation
- 54:08where the comment that had the ID 300
- 54:11changes to ID 305. The other major thing
- 54:14is that every row needs to have a value
- 54:17for that key. It cannot be null. So if
- 54:20we haven't defined this yet, null just
- 54:22means missing a value. Key columns
- 54:24cannot be null. Every single row has to
- 54:26have a value. They all have to be
- 54:28unique, and they cannot change.
- 54:30So those are the three major rules with
- 54:32primary keys. That's the key that you
- 54:35set for a table. There are also foreign
- 54:37keys, which we'll talk about in detail
- 54:39as well. These reference values from
- 54:41another table. Often, it's that table's
- 54:44primary key, but a column defined as a
- 54:46foreign key does not by default have to
- 54:49be unique. It can change and it can be
- 54:53null. So the rules with keys do not
- 54:56apply to foreign keys. They're just
- 54:58there to make that connection and to
- 55:00define what happens if you try to break
- 55:02that connection or delete a row that
- 55:04other rows depend on. So that's the
- 55:06overview of what you should know going
- 55:07into this next section. I talk about a
- 55:09lot of different options and different
- 55:11terminology. Don't let it overwhelm you.
- 55:13The main thing you need to understand is
- 55:15every row has a key, and if you want to
- 55:16reference a row from another table, you
- 55:18use a foreign key. So a key is a way to
- 55:20uniquely identify a row. Now, up until
- 55:23this point, we have just been using IDs.
- 55:26This is a very common approach, and this
- 55:29is an example of a surrogate key. This
- 55:31is something that's made up and has no
- 55:34real world value. So it's not like a
- 55:36social security number or an email
- 55:38address or anything that actually has
- 55:41meaning outside of the database. This is
- 55:44for the database to use only. We don't
- 55:46expose it to users. So you probably want
- 55:48to be familiar with that term or you
- 55:53will sometimes see natural keys which
- 55:57are actually meaningful values such as
- 56:00usernames, emails, or other unique
- 56:04things about that row. So which one do
- 56:06you choose? Well, I think it's a good
- 56:08idea to design your tables such that
- 56:11they can have a natural key, but I still
- 56:14prefer the surrogate keys. That's my
- 56:16personal opinion. I don't think you
- 56:18should entangle real world data with
- 56:21keys that are designed to make
- 56:23connections within databases and
- 56:25reference specific rows. So I just keep
- 56:28those two ideas separate and give
- 56:29everything a key. I even do this for
- 56:33junction tables. So for example, if your
- 56:36table just looks like this, where it's
- 56:37just connecting
- 56:40IDs
- 56:42like so. Well, I will often give it a
- 56:45key of its own
- 56:49so we can reference that connection with
- 56:52a single column.
- 56:54And then for these combinations here, I
- 56:56would just label them as unique
- 57:01rather than making them the actual key.
- 57:03But it is possible to make two columns
- 57:07the key.
- 57:08That's known as a composite or compound
- 57:10key. The compound key is a key that is
- 57:13made up of multiple keys, whereas a
- 57:16composite key is a key that's made up of
- 57:18multiple things, but not every single
- 57:20one of those things has to be a key.
- 57:22This is kind of splitting hairs. It's
- 57:24really the same idea. We have a key made
- 57:26up of smaller things. So let's say for
- 57:28example, we had a comments table.
- 57:31You could make an association between
- 57:34the user ID and the video ID.
- 57:38But if you made that combination unique,
- 57:40you wouldn't be able to make more than
- 57:42one comment on a video because your user
- 57:45ID is already in that key. So you could
- 57:48add a third column here, such as the
- 57:51timestamp. So in that situation, as long
- 57:53as you're not posting at the exact same
- 57:55moment in time, you can make another
- 57:58comment. This combination
- 58:00would be an example of a composite
- 58:05natural key. We're using multiple
- 58:07columns. They're not all keys
- 58:09themselves, and it's a natural key
- 58:12because we're not using purely generated
- 58:14IDs. And these could even reference
- 58:17other natural keys
- 58:20if we wanted instead of an ID.
- 58:23But the composite keys get pretty
- 58:25complicated, in my opinion, rather
- 58:27quickly. So I would rather just attach
- 58:30to this a comment ID.
- 58:36So we could just reference a specific
- 58:38comment with its ID, whatever that ID
- 58:42might be.
- 58:45You still might protect this grouping
- 58:50here. I just wouldn't make it the
- 58:52primary key because if you're
- 58:54referencing that in the database, it
- 58:56being multiple columns, that becomes
- 58:58more complicated. So I just attach a key
- 59:01to everything. So I don't really use
- 59:02natural keys for the actual database
- 59:05connections. And then additionally,
- 59:07because I'm always putting in a
- 59:09surrogate ID, I don't really use these
- 59:12composite keys or the compound keys for
- 59:15the key itself, but maybe just as the
- 59:17uniqueness constraint. So if you have
- 59:19something that could have worked as a
- 59:21key, such as this right here, and then
- 59:23you downgrade it from a key to a unique
- 59:26constraint, this is said to be a
- 59:29candidate key or an alternate key.
- 59:33Basically something you could have
- 59:34chosen as a key. And that's important
- 59:36because it's ideal that a table does
- 59:39have some data in there that could exist
- 59:41as a key, even if you do use surrogate
- 59:44keys. It's not like a hard rule you have
- 59:47to, but it goes back to the data
- 59:49integrity. We don't just want to use a
- 59:51surrogate key and then let the data in
- 59:53the table be whatever it wants. So
- 59:55candidate keys can be downgraded to
- 59:58unique constraints.
- 1:00:00And even this key is pretty bad, right?
- 1:00:02We're using the timestamp, which if you
- 1:00:05double click or the client sends the
- 1:00:07data twice or there's a glitch on the
- 1:00:09back end, it might send the same comment
- 1:00:12briefly after the first one and they
- 1:00:14both get inserted into the database.
- 1:00:16So in that situation, you have comment A
- 1:00:19and then comment B, and these are
- 1:00:22exactly the same with just a different
- 1:00:23timestamp. So it's not really a strong
- 1:00:25example. So you could look at your
- 1:00:27primary key or any candidate keys or
- 1:00:29unique constraints and judge the quality
- 1:00:32of it by how unique it can force the
- 1:00:34data to be.
- 1:00:36Because if you just have two comments
- 1:00:37that look like this and they're both
- 1:00:39from the same user on the same video,
- 1:00:41it's hard to say for certain if the
- 1:00:44person meant to post it twice or it's
- 1:00:46duplicate data and one should be
- 1:00:48removed. Comments are a challenging one
- 1:00:50because there's not a really strong
- 1:00:52natural key. So this is an example of
- 1:00:54something you could do, but it's not a
- 1:00:55flawless solution. And it has flaws
- 1:00:58because it's depending on luck and
- 1:01:01everything going good. It doesn't have
- 1:01:03anything that's inherently a strong
- 1:01:05natural key. An example of something
- 1:01:07that might have a stronger natural key
- 1:01:09would be something like the daily stats
- 1:01:11for a video where it's associated to a
- 1:01:14day and that is naturally going to be
- 1:01:17unique. Long story short, let me just
- 1:01:19summarize everything I just said in like
- 1:01:22five words. Use a surrogate key,
- 1:01:28please. Okay, so you decided to use a
- 1:01:29surrogate key, but it's not that simple.
- 1:01:32We could have an auto incremented
- 1:01:39ID, which is what we've been using so
- 1:01:41far, counting 1 2 3 4 up for eternity.
- 1:01:46Or we could use a UUID or GUID,
- 1:01:51a universally unique identifier or
- 1:01:53globally unique identifier, which is
- 1:01:55basically just getting the number
- 1:01:57randomly instead of counting up from
- 1:01:59one. Which of these do you choose? And
- 1:02:01this actually brings up some of the
- 1:02:02stuff that we talked about in the
- 1:02:03beginning about your system design. The
- 1:02:05choice here, I would say, comes down to
- 1:02:07how you are using your database. If you
- 1:02:10have a centralized database that
- 1:02:13everything points to,
- 1:02:15and this database has the ability to
- 1:02:18auto increment IDs, then that's probably
- 1:02:20what I would go with. Generating
- 1:02:22globally unique IDs is more common when
- 1:02:25you are in a decentralized system where
- 1:02:28you might have multiple database nodes
- 1:02:31because you don't want to have
- 1:02:32conflicting IDs across that data. So if
- 1:02:36instead of a single database node, we
- 1:02:39had a database cluster and all of these
- 1:02:41database nodes were auto incrementing,
- 1:02:44there's a chance when you looked across
- 1:02:45the data from multiple nodes that there
- 1:02:48would be IDs reused.
- 1:02:50So then you need to have like a
- 1:02:51centralized ID distributor and all this
- 1:02:54crazy system design stuff that you can
- 1:02:56research if you're interested in it. One
- 1:02:58simple solution is to just use a GUID.
- 1:03:00And when you insert the data into the
- 1:03:04database, you can generate that GUID in
- 1:03:06your code and attach it to the row.
- 1:03:07That's if the database doesn't have a
- 1:03:10way to do it itself. You might be able
- 1:03:12to just have the database create that
- 1:03:13ID. Either way, basically the row needs
- 1:03:16a unique ID. You can auto increment or
- 1:03:19you can use a UUID. So foreign keys
- 1:03:21reference data in another table. We used
- 1:03:23the example of a lookup table earlier.
- 1:03:26This is a very good example. I don't
- 1:03:29remember the exact data, but we had
- 1:03:30something like four permissions. We had
- 1:03:34one owner, two admin, three editor.
- 1:03:40So we'll call this the permissions
- 1:03:41table.
- 1:03:43And then we have another table such as
- 1:03:46channel users. Well, this is actually a
- 1:03:49junction table, so it just has a bunch
- 1:03:50of keys. But we might have user ID one
- 1:03:54for channel ID three and this person is
- 1:03:59an admin. So what the foreign key does
- 1:04:03is it says the value has to exist over
- 1:04:07here in this list.
- 1:04:09So this would be defined as a permission
- 1:04:12ID. And then you would say references
- 1:04:16permissions.permission
- 1:04:18ID, for example. And in this situation,
- 1:04:20we actually have three foreign keys all
- 1:04:23pointing to different tables. And you'll
- 1:04:26probably have a surrogate key for this
- 1:04:28junction table, which is the primary
- 1:04:30key. So I'll just say that's
- 1:04:34ID one. So these underlined ones point
- 1:04:36to some other tables. I'm just going to
- 1:04:38draw arrows to demonstrate that. But
- 1:04:41let's focus in on this one so we have a
- 1:04:42concrete example.
- 1:04:44So here are the rules with foreign keys.
- 1:04:46Pretty simple. It has to reference a
- 1:04:49unique
- 1:04:51column, but the value itself does not
- 1:04:54have to be defined unique on the
- 1:04:57definition side.
- 1:04:58It does not have to be unique. And
- 1:05:01that's very important to remember. The
- 1:05:04destination has to be unique, but the
- 1:05:05foreign key value does not have to be
- 1:05:07unique.
- 1:05:09Meaning we could have other values in
- 1:05:11here that also point to admin. That
- 1:05:15makes sense, right? Because if you're
- 1:05:17talking about all of channels and all of
- 1:05:19the users, there's to be a lot of
- 1:05:22admins. And then pretty much all the
- 1:05:23other rules with primary keys do not
- 1:05:26apply to foreign keys. This can be null
- 1:05:29if we decide. So you can make it
- 1:05:30nullable. This person has no
- 1:05:32permissions. You can change that value.
- 1:05:34We promote them up to owner. And it
- 1:05:37doesn't have to be unique. So pretty
- 1:05:39much we have full control over that
- 1:05:41foreign key with the only rule being it
- 1:05:44has to reference something unique. So
- 1:05:46there are a couple of configurations
- 1:05:48that you should be familiar with for a
- 1:05:50foreign key constraint, and those are on
- 1:05:53update and on delete.
- 1:05:59The main one you should concern yourself
- 1:06:01with is on delete. In 99 out of 100
- 1:06:05scenarios, the foreign key is going to
- 1:06:07reference a key and a primary key should
- 1:06:11not change. So on update is not super
- 1:06:15relevant. Basically though, let's just
- 1:06:17say the key changed, then you can
- 1:06:19configure what happens to the child
- 1:06:21value. But as mentioned, that's not
- 1:06:23super relevant. What is common though is
- 1:06:26trying to delete the parent.
- 1:06:31So in this example, the thing being
- 1:06:34referenced is the parent and then the
- 1:06:36referencing side is the child.
- 1:06:39So the exact words might change from
- 1:06:42database provider to database provider,
- 1:06:46but the general list will look something
- 1:06:47like this. No action,
- 1:06:50restrict. And this is a configuration on
- 1:06:53the key so you can decide what happens.
- 1:06:56Cascade,
- 1:06:57set null or set default. So in this
- 1:07:02situation, we are referencing the value
- 1:07:05one, which points to this here. And then
- 1:07:08let's say we delete this data. When we
- 1:07:11define that foreign key, we specify what
- 1:07:14behavior we want to happen when we
- 1:07:17delete the parent.
- 1:07:19So by default, we have no action, which
- 1:07:21is going to error and say, hey, you
- 1:07:24actually can't delete this row. It's
- 1:07:26probably a good safe default. No action
- 1:07:29and restrict are very similar. They're
- 1:07:31both going to error out. There's a minor
- 1:07:33difference of when that happens.
- 1:07:36And that's just in the situation of
- 1:07:39doing this within a transaction. If you
- 1:07:41are using no action and you get the
- 1:07:43state to be valid before that
- 1:07:44transaction is committed, you're good to
- 1:07:46go. With restrict, it's going to just
- 1:07:48error out if you change it at all. So
- 1:07:50that's kind of an edge case difference.
- 1:07:52You usually don't have to worry about
- 1:07:53it. Both of these you can think of as
- 1:07:55just not allowing you to delete that
- 1:07:56parent. So you can imagine deleting that
- 1:07:59and then creating a new row with the
- 1:08:00same ID all within that transaction. By
- 1:08:03the time the transaction ends, the state
- 1:08:05is fine and we're good. But with
- 1:08:07restrict, it's going to error out. For
- 1:08:09some database systems, these are exactly
- 1:08:11the same behavior. So research your
- 1:08:13specific database system. The next one
- 1:08:15is really common as well. So that's
- 1:08:17cascade. And this one you will logically
- 1:08:20think makes sense in many situations,
- 1:08:23but is actually pretty dangerous. So
- 1:08:25basically, if you delete the parent,
- 1:08:29it will cascade to any references,
- 1:08:31deleting that row as well. So if you
- 1:08:35were to delete two, you would delete
- 1:08:37this row.
- 1:08:39This is nice because it allows you to
- 1:08:41delete stuff without it saying error and
- 1:08:43you having to go figure out a bunch of
- 1:08:44relationships. But it's very dangerous
- 1:08:47because you might delete one thing not
- 1:08:49realizing there are side effects to that
- 1:08:51delete. When would you want to do a
- 1:08:53cascade? Well, you could imagine if you
- 1:08:55had a video and you had comments on that
- 1:08:58video and you delete the video, well, it
- 1:09:00makes sense for those comments to
- 1:09:01disappear. So that's an example where
- 1:09:03cascade is logical. Or if you have a
- 1:09:04table for user settings and you delete
- 1:09:06that user account, then yeah, cascade
- 1:09:09it. A safer option is set null and that
- 1:09:12basically creates an orphaned row. And
- 1:09:15this is a row that points to another row
- 1:09:17that doesn't exist. So the child exists,
- 1:09:19but there's no parent. It's a little bit
- 1:09:21depressing, but this is a safer option
- 1:09:24because you don't risk any
- 1:09:26effects from an initial delete. It's
- 1:09:28just going to break those connections.
- 1:09:30In order for this to work, the column
- 1:09:33must not be set to not null.
- 1:09:37So, not
- 1:09:40not null. Or
- 1:09:43you could just say nullable. You might
- 1:09:45have this keyword in your database as
- 1:09:47well. Set default works similarly. If
- 1:09:49you set a default value for a column, it
- 1:09:51will just set it to the default instead
- 1:09:53of setting it to null or deleting that
- 1:09:55row entirely. So, you can use set
- 1:09:57default if you need to change the value
- 1:09:59to something else if a certain row is
- 1:10:02deleted. Most of the time, I would
- 1:10:04recommend no action or set null. That's
- 1:10:07going to get you most of the
- 1:10:08capabilities without the risk that
- 1:10:11cascade brings. The only downside is if
- 1:10:14you need that column to have a value,
- 1:10:16you can't do a set null.
- 1:10:19In that situation, you can use a set
- 1:10:20default. And this brings up a better
- 1:10:22question of when should you delete data?
- 1:10:24You should do some dedicated research on
- 1:10:27deleting data.
- 1:10:30Now, obviously you want to be able to
- 1:10:32delete data, but sometimes you just want
- 1:10:35to do a soft delete or do a soft delete
- 1:10:37for a period of time before data is
- 1:10:40deleted. So, if you delete a user
- 1:10:42account, maybe that user just gets a
- 1:10:44flag saying deleted, but the row still
- 1:10:47exists in the database. And then any
- 1:10:49users that stay in that deleted state
- 1:10:52for 30 days or 90 days ultimately get
- 1:10:56deleted. This allows you to have more
- 1:10:58aggressive delete behavior in your apps
- 1:11:00without the risk of data loss or having
- 1:11:02to go get a database backup to restore
- 1:11:05some data. You're really just changing a
- 1:11:07flag for a delete. So, that could get
- 1:11:09into a bunch of details, so just do some
- 1:11:11research. You also want to consider the
- 1:11:14legal parts of it if you are in a
- 1:11:16location or serving users in a location
- 1:11:19where data privacy is taken seriously,
- 1:11:21you might have to actually delete that
- 1:11:23data and not just use a soft delete. So,
- 1:11:26that is your overview on foreign keys. I
- 1:11:29don't believe foreign key columns are
- 1:11:31indexed by default, but if you're often
- 1:11:34working with them, you might want to
- 1:11:35consider adding an index. And also
- 1:11:37sometimes the database, if you use a
- 1:11:39unique constraint, it will automatically
- 1:11:41index that column. So again, learn the
- 1:11:43details of your specific relational
- 1:11:45database management system. The next
- 1:11:47major topic in database design is
- 1:11:49normalization. Now you have already
- 1:11:51learned so many fundamentals that the
- 1:11:53normalization process is going to be
- 1:11:55super easy. I think the reason why so
- 1:11:57many people get tripped up is they start
- 1:11:59studying normalization when they don't
- 1:12:01understand everything we've talked about
- 1:12:03so far. So what is normalization? It's
- 1:12:05the process of breaking up single tables
- 1:12:08into multiple tables. This is basically
- 1:12:10to apply certain database design rules,
- 1:12:12and these will protect our database
- 1:12:14integrity. So we'll go through these
- 1:12:16rather quickly because they're just
- 1:12:18rewordings of things we've already done.
- 1:12:20We'll just go through an example from
- 1:12:22beginning to end. So let's say we have a
- 1:12:25videos table.
- 1:12:27We have the actual video title. We have
- 1:12:30tags.
- 1:12:32We have the channel it comes from, and
- 1:12:35we have the owner. Let's say email
- 1:12:40and owner name.
- 1:12:43So this is a pretty good example because
- 1:12:46we got a lot of bad things going on
- 1:12:48here. And we'll start off with just some
- 1:12:49kind of spreadsheet
- 1:12:51looking thing.
- 1:12:54And hopefully I get enough room for each
- 1:12:56of these. Let's say we have a video Git
- 1:13:00in one hour. Coming
- 1:13:03soon, maybe.
- 1:13:05Might want to subscribe. You never know.
- 1:13:07Say we have another video Docker crash
- 1:13:11course. And then we add in some tags for
- 1:13:16this. So Git, GitHub,
- 1:13:21version control. Let's just say Docker
- 1:13:24and Docker Compose.
- 1:13:26The channel, we're going to say Code
- 1:13:28Breakthrough. Same for that
- 1:13:32And then some email address.
- 1:13:34And then my name. So this is awful.
- 1:13:37Everything is within one table. Let's
- 1:13:39just go through some of the issues.
- 1:13:41Every time we post a video, we get data
- 1:13:43duplication. We have that channel in
- 1:13:45there multiple times. We have the email,
- 1:13:47the name in there multiple times. What
- 1:13:49this does is if we change the user's
- 1:13:51name, we'll either have to go update it
- 1:13:53everywhere else or we have conflicting
- 1:13:56values, both a problem. Additionally
- 1:13:59with tags, we have multiple values
- 1:14:01within a single field. So it's much
- 1:14:03harder to search for something like
- 1:14:05docker compose, which is in this array
- 1:14:09essentially. Or what if I need to change
- 1:14:11the channel owner or the channel has
- 1:14:13multiple owners? How am I going to do
- 1:14:16that? I might need to go change all of
- 1:14:18them or make it an array. This is an
- 1:14:20extreme example, but it allows us to see
- 1:14:22what anomalies show up. The first would
- 1:14:24be an update anomaly where we change the
- 1:14:27value here and it doesn't propagate to
- 1:14:30all the other possible values. So now we
- 1:14:32have drift. Or we have an insert
- 1:14:35anomaly. And this is almost like a
- 1:14:37catch-22 kind of thing where you want to
- 1:14:41create a channel, but everything's
- 1:14:43within one table here. So we would need
- 1:14:45to also put a video and its tags. How do
- 1:14:48we add a new channel? Then also delete
- 1:14:50anomalies. Let's say you delete these
- 1:14:52two videos and this is all the data we
- 1:14:55have on the channel. Well, then did we
- 1:14:58just delete the channel accidentally as
- 1:15:00well?
- 1:15:01Again, these seem kind of ridiculous
- 1:15:02because this is the extreme case where
- 1:15:05we're actually trying to store the full
- 1:15:07database in one table, but microscale
- 1:15:09versions of those issues show up when
- 1:15:11you don't normalize a database. So I'll
- 1:15:14just keep these as an example and I'll
- 1:15:16crop off this table here so I have some
- 1:15:17room to write. So we're going to talk
- 1:15:19about the normal forms.
- 1:15:22These are basically the steps in
- 1:15:24database normalization.
- 1:15:26There are a bunch of them, but I only
- 1:15:28care about the first three. The
- 1:15:31first normal form, second normal form,
- 1:15:35and third normal form. The first thing I
- 1:15:37should say before I forget is that these
- 1:15:39build upon each other. To get to the
- 1:15:41second normal form, you have to be in
- 1:15:43the first. And to get to the third, you
- 1:15:45have to be in the second. First normal
- 1:15:46form deals with things being atomic.
- 1:15:52And while I know it's not scientifically
- 1:15:55accurate, the idea here is that an atom
- 1:15:58is the smallest unit. So you're not
- 1:16:00going to share a cell and put multiple
- 1:16:03values in there. So if we decided, hey,
- 1:16:05we can put multiple tags here, you might
- 1:16:07think we could just duplicate the row.
- 1:16:10One of them be Docker, the other one be
- 1:16:12Docker Compose. That's like the first
- 1:16:14step in thinking of how to fix this so
- 1:16:16that we don't have multiple values in a
- 1:16:19single field, but it's not the correct
- 1:16:21fix because then we have two rows that
- 1:16:23refer to the same entity. So we want a
- 1:16:26row to uniquely identify the entity, and
- 1:16:29we want that row to be uniquely
- 1:16:31identifiable with an ID. So that was a
- 1:16:34lot of words. I'm sorry.
- 1:16:36Long story short, we want unique rows.
- 1:16:39I should have just said that. So
- 1:16:40eventually you'll just add IDs,
- 1:16:43but there's still like so much else
- 1:16:45wrong with this. So let's just run
- 1:16:46through the list. The rule is anytime
- 1:16:48one of these is broken, you're going to
- 1:16:50introduce a new table. So this column is
- 1:16:53breaking that. So we need a tags table.
- 1:17:00Simple fix. This is the part in the
- 1:17:02process where I would also grab any
- 1:17:04duplicate values and break that column
- 1:17:07off into its own table as well. So
- 1:17:09that's going to be the channel here. So
- 1:17:12I will update according to those
- 1:17:13changes. So here is our video table now.
- 1:17:18And then we'll have a tags table.
- 1:17:20This will reference back to that video
- 1:17:23and then the relevant tag. So the first
- 1:17:26one was Git, GitHub, version control.
- 1:17:31And then we'll have video two, which is
- 1:17:33Docker,
- 1:17:35and then Docker Compose.
- 1:17:37We'll then have channel, and then all
- 1:17:39that information that was originally
- 1:17:42with that channel is now moved to this
- 1:17:44table here. We will give this an ID.
- 1:17:48And then over in the video, we can
- 1:17:49reference what channel it belongs to. So
- 1:17:53channel one. Both of these point here.
- 1:17:56So I ended up rewriting this so I could
- 1:17:59take my time and make it nice and neat
- 1:18:01for you guys so you can actually see it.
- 1:18:03Everything's pretty much the same. The
- 1:18:04only thing is I added this surrogate tag
- 1:18:06ID here, which is again, as I've
- 1:18:09mentioned probably a hundred times, I
- 1:18:10just add an ID to every single table.
- 1:18:13Second normal form deals specifically
- 1:18:15with partial dependencies, which is when
- 1:18:18an attribute depends on only half of the
- 1:18:21key.
- 1:18:21Now, if you're using a surrogate key,
- 1:18:23which is why I added this here, you
- 1:18:25might be thinking, how could it depend
- 1:18:27on only half of the key if we never have
- 1:18:30a composite or compound key? Well, you
- 1:18:32can still go off of what could be a good
- 1:18:36natural composite key. So look at any of
- 1:18:38the unique constraints for a table. This
- 1:18:41is going to be really common for
- 1:18:42junction tables because that is where
- 1:18:45you often have two keys. So in this
- 1:18:48situation, we added this tag ID and
- 1:18:51these point back to certain videos. So I
- 1:18:53have three tags for this video and two
- 1:18:55tags for this video. Well, if we had
- 1:18:57another attribute in here, that could be
- 1:19:00a tag color, which is a common thing
- 1:19:02you'll see. You'll have a multi-select
- 1:19:05and as you select things, they'll show
- 1:19:07up, but the different options are shaded
- 1:19:10or colored differently. That's one
- 1:19:12option. Or you could have a tag
- 1:19:13description. So if you had a landing
- 1:19:15page dedicated for content for that tag,
- 1:19:18you could have the description for that
- 1:19:20specific tag. Or you could have a
- 1:19:22category for groups of tags. So these
- 1:19:26are all in the programming category.
- 1:19:28That would be a common approach as well.
- 1:19:30And it's basically a way to merge
- 1:19:32categories and tags. We basically have
- 1:19:34tags and tag categories. So these are
- 1:19:37all possibilities. Let's just go with
- 1:19:39that category example because I really
- 1:19:41like that. And we could group all of
- 1:19:43these as technology or software. So you
- 1:19:47might have category here.
- 1:19:50And I'll just say tech for all of these.
- 1:19:53This is an example second normal form
- 1:19:55violation because the category is
- 1:19:58talking about the tag, but the table is
- 1:20:01dedicated to the tags for a certain
- 1:20:04video. So there's no association between
- 1:20:07the category and the video. This is
- 1:20:09purely to describe the tags. That's a
- 1:20:12partial dependency. It only depends on
- 1:20:15the tag ID, not the video ID. So the
- 1:20:17solution here is to just make this video
- 1:20:20tags
- 1:20:23and we'll introduce a new table.
- 1:20:28Tag categories.
- 1:20:30We'll have tag category ID
- 1:20:34and then the name of that category, a
- 1:20:38description or any other attributes you
- 1:20:41want to store about it.
- 1:20:43Then we'll replace this with a foreign
- 1:20:45key.
- 1:20:47So we'll say one
- 1:20:50tech.
- 1:20:52The description will be
- 1:20:55nerd stuff. And these will all have that
- 1:20:59category. So that is how you solve a
- 1:21:01second normal form violation. You just
- 1:21:03break it off into its own table. Now we
- 1:21:05still have this repeating value, but you
- 1:21:07have to remember it is a foreign key, so
- 1:21:09it's tied to this. It's not like
- 1:21:11repeating a string five times where if
- 1:21:14we needed to change the name of that or
- 1:21:16adjust the category data, we would have
- 1:21:19to do it across all of those rows. Here
- 1:21:21we only have to do it one place right
- 1:21:23here. Now let's talk about third normal
- 1:21:25form. This deals with transitive
- 1:21:28dependencies. So we have partial
- 1:21:34and transitive.
- 1:21:39This is when we basically have a chain
- 1:21:42where a column depends on another column
- 1:21:44that ultimately depends on the primary
- 1:21:46key, but you still have room to split
- 1:21:48things out. The thing that's breaking
- 1:21:50that is this right here. And you can see
- 1:21:52these chains, if you were to look at a
- 1:21:55different channel, that's going to
- 1:21:57change the email of the owner and
- 1:21:59ultimately the name of that owner
- 1:22:02because we're talking about somebody
- 1:22:03else.
- 1:22:04So you can see it hops through multiple
- 1:22:07columns. So you'll find that chain and
- 1:22:09you'll basically split it off. So we'll
- 1:22:11take the user portion of this and put it
- 1:22:13in its own table.
- 1:22:16So we'll have users over here.
- 1:22:19This will have user ID, the user's
- 1:22:23email,
- 1:22:25and the user's name.
- 1:22:27So now we can keep the channel over
- 1:22:29here.
- 1:22:31And we can just call it channel or we
- 1:22:33can just say name since we don't have
- 1:22:35the person's name in here as well.
- 1:22:38So that's much cleaner. And then we will
- 1:22:40need the user ID for who owns the
- 1:22:42channel.
- 1:22:44We're just going to assume one owner for
- 1:22:46now. Let's go 73. Then over here, we'll
- 1:22:49have a user with 73 and then my email
- 1:22:52and my name. And that's a foreign key
- 1:22:54that points to that table. Now we don't
- 1:22:56have any transitive dependencies.
- 1:22:58There's one other major design flaw that
- 1:23:01we have in our current structure. So
- 1:23:03what if we wanted to store multiple tags
- 1:23:05for a video? So right here we have Git
- 1:23:08in one hour.
- 1:23:10So we have three tags, Git, GitHub, and
- 1:23:12version control. These are all category
- 1:23:15one, which if we follow that down to the
- 1:23:18tag categories, we get the name tech.
- 1:23:21But let's say we added another row in
- 1:23:22here with an existing tag value such as
- 1:23:25Git. And then we assigned it a different
- 1:23:29category such as two. With our current
- 1:23:31structure, every tag that's applied to a
- 1:23:33video gets its own category rather than
- 1:23:38that category being tightly coupled to
- 1:23:41an individual tag. To fix this, we split
- 1:23:43it out into another table, of course.
- 1:23:46And this is why you start with one table
- 1:23:47and you end up with 750. So I'm kind of
- 1:23:50running out of room here. But yeah, you
- 1:23:53basically
- 1:23:55split these into two. You'd have video
- 1:23:57tags and then a dedicated tags
- 1:24:01table. We would have an ID for each
- 1:24:02individual row, but we would have the
- 1:24:04video ID and then the tag ID.
- 1:24:06So it might look something like this
- 1:24:08where we have this junction table to
- 1:24:11apply tags to videos. And that way, Git
- 1:24:14will always have this same category and
- 1:24:16we just reference that tag metadata
- 1:24:20using its ID. And this should be the
- 1:24:23name of the tags. So now Git gets its
- 1:24:27own category and it's always going to be
- 1:24:29that category and we just reference it
- 1:24:31from the video tags table. Also, I don't
- 1:24:34even know how I don't notice these
- 1:24:35things when I'm writing. Category.
- 1:24:39There we go.
- 1:24:42I'm learning how to spell. So that's an
- 1:24:44overview of the normalization process.
- 1:24:46It's kind of just a game of making sure
- 1:24:49there's no integrity issues with your
- 1:24:51database. And this is a spectrum.
- 1:24:53Sometimes you'll choose to denormalize
- 1:24:57for some tradeoffs. If you're creating a
- 1:24:59bunch of tables and not getting a lot of
- 1:25:01benefit from it, then sometimes you can
- 1:25:03choose to stop that normalization
- 1:25:05process a bit early, which is basically
- 1:25:08what we did because there's actually
- 1:25:09normal forms after this, but that's when
- 1:25:11you get into excessive normalization
- 1:25:14with minimal benefit. So that's why I
- 1:25:17tend to just think of the first three.
- 1:25:18So I think I got it all. There might be
- 1:25:20little things here or there, but overall
- 1:25:21you get the idea. So that's an overview
- 1:25:23of database design. Much of the process
- 1:25:25is breaking things out into more tables.
- 1:25:28But how do you then get that data to
- 1:25:30come back together? That's the process
- 1:25:32of joins and you'll do that when you're
- 1:25:34writing SQL. So SQL is the language you
- 1:25:38use to interact with the database. If
- 1:25:40you've made it this far into the video,
- 1:25:41you probably already know that. But
- 1:25:43there's a lot to writing SQL and a lot
- 1:25:45of it ties to what we talked about in
- 1:25:47this video. So when it comes to things
- 1:25:48like joins and views or indexes, the
- 1:25:51different data types that we use for
- 1:25:53data definition for data integrity
- 1:25:56purposes, how do we actually turn our
- 1:25:58concepts like our foreign key
- 1:26:00constraints or unique constraints into
- 1:26:02real things in our database? That's what
- 1:26:04we're going to do with SQL. And there's
- 1:26:06enough to talk about on this topic that
- 1:26:08I wanted to just do a dedicated video to
- 1:26:10it. So from here, please be sure to
- 1:26:12check out my SQL course to continue the
- 1:26:14discussion, as there's still a lot more
- 1:26:16I want to go through. But at this point,
- 1:26:18you should have a pretty solid
- 1:26:19foundation of database design. If you
- 1:26:21want to get more into the system design
- 1:26:23with databases, you can check out my
- 1:26:25system design playlist and my backend
- 1:26:27engineering playlist. This will talk
- 1:26:29about things like the cap theorem and
- 1:26:31other database principles that come up
- 1:26:33more often with system design
- 1:26:34interviews. So a little bit less about
- 1:26:37application development and designing
- 1:26:39the database tables and more about
- 1:26:41scaling applications that use databases.
- 1:26:44That'll talk about things like
- 1:26:46consistent hashing and how to use IDs
- 1:26:48properly, stuff that goes beyond what we
- 1:26:50talked about here. So hopefully this
- 1:26:52pushes you in the right direction and
- 1:26:53you can continue learning with my other
- 1:26:55resources. Thank you for watching and
- 1:26:57let me know what other content you'd
- 1:26:58like to see and I'll see you in the next
- 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.