5.7 Foreign Keys for Comments Table
Join over 3 million cybersecurity professionals advancing their career
Sign up with
Required fields are marked with an *
Already have an account? Sign In »
7 hours 36 minutes
I welcome back to Module five sequel development. We are currently in less than two foreign keys, and we're moving into Lesson 2.3,
the foreign key for the comments table.
So in the previous lesson, we implemented the foreign key for images and articles. And this lesson we're gonna move on to the next table, which is the comments table, and that will have to foreign key requirements we need to implement.
And that is
a restraint or a foreign key constraint to the user table
and a foreign key constraint to the article table,
so that when somebody generates a comment,
we are ensuring that that user exists,
and that article also exists.
So we're going to do the same thing that we did for the images table. But we're going to do it twice.
Let's go ahead, get sorry.
So if we go to the Properties tab
and we click on the Foreign Keys, we right click in the empty area and select create new foreign key.
Let's go ahead. You could do it in either order. The order doesn't matter
and let's go ahead and do the user's first.
So we're going to say that the author i d
references the i D column on the user's table.
Go ahead and select. Okay, leaving the rest of the options at the default setting.
And we're gonna go ahead and add in another foreign key.
And we already did the user. So now we need to reference the articles,
and we're going to select the article I D. Reference is the i. D.
Column on the article Stable.
now go ahead and click. Save
should take just a moment.
And now if we go and look at the R diagram will see that it hasn't updated it.
And even if you select refresh, it does not seem to update that e r diagram.
So it seems the best way to do that's actually close the, ah, the table section and then reopen it.
Okay, it looks like the diagram hasn't updated. Let's go ahead and close that again.
That's right. Click on the table and select. Refresh
the way for that to refresh the data base. Instance.
It's double click on it.
Okay, I got the diagram to show up correctly. What I actually had to do is closing the comments table
and refreshing from within. This interface was actually not enough, but I had to do was actually had to close the entire D. Beaver instance and then reopen it. So if you're having trouble with the e R diagram updating, you give that a shot,
it can be a bit of a pain when that happens. But it just appears to be the behavior of the application on occasion.
So now we can see
that the comments is bound to the user stable and the articles I D.
So when a common is added to the database, the database will enforce the rule that the author I d exists
as an I. D on the user table and that the article I d
exists as an I. D in the article tables
Now you might say,
Well, is isn't ah
convention good enough to just ensure that
you Ah, of course, ad
comments that belonged to a national user and
comments that belonged to the actual article and in general, yes. But as you get more programmers on the team,
someone could accidentally introduced a bad algorithm
that somehow on occasion adds the
a comment that belongs to the user. That doesn't exist for some reason, and by enforcing these constraints on the database itself, you can ensure that doesn't happen on any other layer of the application. So it's a good idea to to create those constraints when it makes sense in your application.
In any case, that completes this lesson. I hope you were able to get those foreign key constraints in, and I will see you in the next lesson. Thank you.