00:00
(upbeat music)
00:02
So before we dive in,
00:06
let's first define what a schema is for a database.
00:09
So it's essentially going to define the overall structure
00:12
of the database itself.
00:13
It's gonna contain the table definitions,
00:16
the columns of each of those tables,
00:18
the relationships between the various tables itself,
00:21
depending on how in-depth
00:22
your particular structuring diagram application is,
00:26
it could contain additional information
00:28
like the particular column type for each of those columns,
00:31
whether or not they're nullable,
00:32
whether or not they're unique, so on and so forth.
00:35
Essentially defining as granular as you wanna go,
00:38
the particular definition for your database,
00:41
specifically for your application.
00:43
So I've already gone ahead and diagrammed out the database
00:46
that we're going to be working with throughout this series.
00:48
I use dbdiagram.io to do this,
00:51
and this is the output of that diagram.
00:53
So each rectangle here represents a table.
00:55
So we have a roles table, users table, watch list table,
00:58
crew movies, movies, movie statuses,
01:01
cynists, and cast movies.
01:03
Now, cynists is a weird word.
01:05
I had never heard of it before
01:06
actually starting out the structure diagram,
01:08
but if we take a look at the definition of it,
01:09
it's essentially a filmmaker.
01:11
It's an enthusiast or a devotee of filmmaking or movies.
01:15
So it's essentially a single word that we can use
01:18
to reference crew members as well as cast members,
01:21
so that we can have them all confined within one table
01:24
because sometimes you have a director
01:26
who's also a cast member or a writer
01:28
who's also a cast member,
01:29
so they're mix and matchable in that sense.
01:31
So let's hide our browser back away.
01:32
So that's what our cynists table is going to contain.
01:34
It's going to contain our crew members, our cast members.
01:37
Each crew member is going to have a unique ID.
01:39
This is going to serve as our primary key.
01:41
That primary key is a unique identifier for our database,
01:45
similarly to how we're using the file name
01:47
within our application at this present point in time
01:49
to uniquely identify each one of the movies
01:52
inside our application.
01:53
But this ID primary key within each of these tables
01:55
that you see is specific to our database.
01:58
And our database is going to be in charge
02:00
of assigning an ID per record
02:02
that we insert into our database.
02:04
So we don't need to worry about
02:05
whether or not an ID is unique.
02:07
Our database will take care of that automatically for us
02:09
as we insert each record.
02:11
It's kind of just going to be an index that starts at one.
02:14
Each time that we insert a new record,
02:16
it's going to go from one to two,
02:17
two to three, three to four, so on and so forth,
02:20
making sure that each one of those records IDs
02:22
are unique in each one of our separate tables.
02:24
So our synapse are going to have an ID,
02:26
a first name, last name, a headshot URL,
02:28
which will be a URL for an image
02:30
of the particular person's headshot.
02:32
They created that, so when they were inserted
02:34
into the database and then updated that.
02:36
So when the record was last updated inside of the database,
02:38
by default, the created at and updated at
02:40
whenever they're first entered
02:41
are going to be the same value.
02:43
And you'll see that the created at, updated at, and ID
02:46
are shared columns throughout each one of the tables
02:48
that we have, our roles, users, watch lists, crew movies,
02:51
so on and so forth, all have an ID marked with the key here
02:54
to denote that it is a primary key,
02:56
and a created at and updated at column,
02:59
both of type timestamp.
03:00
You'll also see that the first name, last name,
03:02
headshot URL, as well as various other columns
03:04
here within our schema, have a VARCHAR
03:07
with a number inside of parentheses inside of it.
03:10
The VARCHAR is a database safe type for strings, essentially.
03:13
And then the number inside of the parentheses
03:16
is the length that we want to allow.
03:18
So for example, our first name and last name
03:20
can be up to, but not more than 100 characters.
03:23
Our headshot URL can be up to, but not more than 255.
03:27
The name for our movie status can be up to,
03:29
but not over 50 characters, so on and so forth.
03:32
And then for our abstract here within our movie itself,
03:34
we have a text column type.
03:36
The text column type is just essentially
03:38
a really long form version of a string.
03:40
It's not particularly limited
03:42
in the sense that our VARCHARs are.
03:44
And then we have our ints, which our primary keys are,
03:46
but so too our relationship-based information,
03:49
so like status ID relating to our movie statuses,
03:52
writer ID going to our cynists,
03:54
director ID going to our cynists, and so on and so forth.
03:57
The int type is going to be a number.
03:59
However, it's going to be a whole number.
04:00
So one, two, three, it's not going to include 1.1
04:04
or decimal-based types like that.
04:05
It's going to be specifically whole numbers.
04:07
Okay, so now that we have a sense of everything going on
04:09
inside of our schema with how we're defining things,
04:12
let's talk about the actual definition itself
04:14
for our schema.
04:15
So we have this split into really two different sections.
04:17
We have this section down here at the bottom
04:20
encompassing our movie statuses,
04:22
movies, crew movies, cynists, cast movies.
04:25
This is the portion that we're going to be focusing on
04:27
first in this series.
04:28
And then we have our roles, users, and watch lists
04:31
that we'll use within this series
04:33
to kind of wrap everything up with a bow
04:35
to cover how you would approach an entire feature
04:39
within our application here.
04:40
So these bottom ones are going to be in charge
04:42
of teaching us how we can define relationships,
04:45
migrations, models, queries, so on and so forth
04:48
within our application.
04:49
And then we'll wrap things up with a bow
04:50
with our roles, users, and watch lists
04:53
by covering how we would approach
04:54
defining those particular features
04:56
inside of our application.
04:57
Now we haven't talked about relationships yet,
04:58
but you'll notice that each of these tables
05:00
has a line pointing to and from it.
05:02
Relationships are defined by taking a records
05:04
and a tables primary key, so that ID column,
05:07
and referencing it elsewhere inside of another table.
05:10
That creates a relationship.
05:12
Now there's various different types of relationships.
05:13
You have one-to-one, one-to-many, or many-to-one,
05:17
depending on which side of the relationship
05:18
you're looking at, and then many-to-many as well.
05:21
And within our schema, these relationships
05:22
are defined by these lines.
05:24
So within our movie statuses, our ID for our movie status
05:27
is pointing to the status ID column on our movie itself,
05:30
essentially saying that the movie is assigned a status
05:33
of whatever the value is matching the ID
05:35
inside of our movie statuses table.
05:37
So for example, our movie statuses table
05:39
might have status of released, post-production,
05:42
production, writing, or various other things like that.
05:45
So if a movie was released, it might have an ID of one.
05:48
If a movie's in post-production,
05:49
it might have an ID of two, so on and so forth.
05:52
So if our particular movie record is released,
05:54
it's going to have a status ID of one,
05:56
matching the released record
05:58
inside of our movie statuses table.
05:59
If the movie's in post-production,
06:01
our status ID would be two,
06:02
matching the post-production record
06:04
inside of our movie statuses table.
06:06
If you're looking at this from our movie statuses table,
06:08
this would be a one-to-many relationship
06:10
because one record inside of our movie statuses table
06:13
can have many movies.
06:14
But if you're looking at this from the movie side,
06:16
it's going to be a many-to-one
06:17
because we can have many movies
06:19
pointing to one particular movie status.
06:21
We have the same thing going on with our users and our roles
06:24
and our users and the watch list.
06:26
And our watch list also matches to a movie as well.
06:29
And then within our movie,
06:30
we also have a writer ID and a director ID,
06:32
both pointing to a cynist's ID.
06:34
But we also have a many-to-many relationship
06:35
between our movies and our cynists as well,
06:37
denoted via our crew movies and cast movies
06:40
pivot tables.
06:41
Pivot tables are intermediary tables.
06:43
They stand in between the relationship
06:45
to define what two records go together,
06:48
in addition to various other information we may need.
06:51
Particularly, we might have a sort order
06:53
denoting who the top build cast member was
06:56
for the particular movie,
06:57
so on and so forth, going down the list as such.
06:59
So for example,
07:00
if we take a look at our crew movies pivot table here,
07:02
we have both a cynist ID
07:03
pointing to one particular cynist member,
07:06
and then we also have a movie ID
07:07
pointing to one particular movie.
07:09
A movie can have many crew members,
07:11
and a crew member can be a part of many movies.
07:14
This pivot table is how we can define
07:16
both of those together.
07:17
So let's walk through a quick example
07:19
of our crew movies many-to-many relationship,
07:21
because this can be a little bit confusing.
07:22
So we have our crew movies here.
07:24
We have an ID, our cynist ID, movie ID, and title.
07:27
For our movies,
07:28
let's say that we have one with an ID of one, two, and three.
07:32
And then for our cynists,
07:33
let's say that we have IDs of 10, 11, and 12.
07:37
Our many-to-many table here
07:38
essentially allows us to say that cynist 10
07:41
has been a part of multiple movies.
07:44
So one, two, and three, for example.
07:46
Cynist 11 might be part of just movies one and two,
07:50
and cynist 12 might just be a part of movie three.
07:52
So we have cynist ID of 10, movie ID of one.
07:56
Maybe their title for this was writer.
07:58
And then we would have cynist 10 on movie two.
08:01
Maybe they were somebody's assistant
08:02
or something for that movie.
08:04
And then we would have cynist 10 on movie three.
08:07
Maybe they were a writer again.
08:08
Each of these records would get a unique ID,
08:10
so we would have one, two, and three there.
08:13
And so with this,
08:13
we've defined that cynist number 10
08:15
has been a part of movies one, two, and three.
08:18
Conversely, we've also noted that movie one
08:20
has a cast member of cynist 10,
08:23
movie two has a cast member of cynist 10,
08:24
and movie three has a cast member of cynist 10.
08:26
And with each of these,
08:28
we've assigned that cynist their unique role
08:30
for that particular movie of writer, assistant, and writer.
08:33
Now for cynist 11, they were only a part of two movies.
08:36
So we can add in our IDs of four and five.
08:39
For cynist 11, they were in part of one.
08:41
Maybe they just got coffee.
08:43
And then on movie two, maybe they got the bagels.
08:45
So in addition to cynist 10,
08:47
we've now also defined that cynist 11
08:49
has been a part of movie one and movie two,
08:52
and their various roles for those two movies.
08:54
And now we also have the flip side of that
08:56
where movie one now has two different cast members.
08:58
They have both cynist 10 and cynist 11.
09:00
So our many-to-many table really just allows us
09:02
to mash together two different tables
09:04
to say that they both relate to one another,
09:06
and we can define additional information
09:08
on how they specifically relate to one another as well.
09:12
If how these tables relate to one another
09:14
isn't quite making sense yet, don't worry.
09:16
That's still a good step away.
09:17
We have to define everything first within our application,
09:20
get our actual tables created,
09:22
along with all of their columns,
09:23
get data inside of each one of those tables,
09:26
and then we can start to look at
09:27
how they relate to one another.
09:28
So we're gonna step through it step-by-step.
09:30
So if things aren't quite making sense yet,
09:32
don't fret, we'll get there.
Join The Discussion! (2 Comments)
Please sign in or sign up for free to join in on the dicussion.
frp
I want more! With a rebel yell, I cry "More! More! More!"
:)
Good stuff!
Please sign in or sign up for free to reply
tomgobich
Lol, thank you!! :D
Please sign in or sign up for free to reply