-
Title:
Related Tables - Intro to Relational Databases
-
Description:
-
So a database,
will usually have several tables in it.
-
Here's how we might start for
our image voting app.
-
The first table represents pictures
that people have uploaded of animals.
-
The second represents people's votes.
-
Fluffy, Monster, and
-
George are all animals,
whose pictures someone has uploaded.
-
Because, we might have two
animals with the same name.
-
We give each one a numeric id here.
-
The pictures tables says
that Fluffy has id 1.
-
Monster has id 2.
-
George has id 3.
-
And so on.
-
The votes table says, which images
have been displayed together for
-
voting, and
which one the user picked as the cutest.
-
Here, the app displayed monster,
ID 2, and George, ID 3,
-
and the user voted for George.
-
2 and 3 were matched up,
and 3 was the winner.
-
Note that in the votes table,
-
the columns are called left,
right, and winner.
-
But they match up, to the column
called id in the pictures table.
-
You can read every row as a sentence.
-
In the pictures table, the sentences
say Fluffy has id number 1, and
-
the filename fluffsocute.jpg,
Monster has id number 2,
-
and filename monstie-basket.png and
so forth.
-
In the votes table,
the sentences are picture 2 and
-
picture 3, were displayed and
the user voted for picture 3.
-
Picture 1 and
picture 3 were displayed, and
-
the user voted for picture 1 and so on.
-
So this 3 and this 3, refer to the same
thing, a cute critter with id number 3.
-
But they're in different tables, and
the columns have different names,
-
because they play different
roles in different sentences.
-
Over here we're saying,
which picture has which id?
-
And over here we're saying, which ones
have been displayed together, and
-
who got the vote?
-
So this is the kind of sentence that
this table actually represents.
-
If you wanted to come up with sentences
like Monster was shown to Fluffy, and
-
Fluffy got the vote instead
of 2 was shown with 1,
-
and 1 got the vote for this row here.
-
We would have to connect rows from
this table, with rows from that table.
-
That's something we can do
with a database query as well.
-
Queries that do this are called joins,
and
-
we'll be seeing a lot of
them later on in the course.
-
For now, just remember that
a value with the same meaning,
-
can occur in different tables and
have different column names, and
-
that we can derive new tables by
linking up existing tables using joins.