How to Use Foreign Key Constraints


show more Using foreign key constraints provides you with in-depth training on Developer. Taught by Bill Weinman as part of the MySQL Essential Training show less
please wait ...

Using foreign key constraints

In a relational data base, it's common to have one table refer to another table by its primary key. Problems can arise however if the reference points to a key that doesn't exist. The foreign key constraint is designed to prevent this from happening. We'll be using the scratch database for this lesson and again I'm going to copy from the exercise files. Because well, in this case it actually happens to be a very significant amount of typing.

And the reason for this is that in order to demonstrate the foreign key constraint, we really need to have several tables with different purposes and have them. Interconnected with a few rows of data. And so we have three tables here, a client table, a book table, and a lend table. So the client table just really has a name for the client and an ID. You notice it's integer, AUTO_INCREMENT PRIMARY KEY. And a book table for the books that are going to be lent to the clients, with the title of the book and an ID, very simple tables.

And a lend table...

Using foreign key constraints
Video duration: 7m 58s 4h 24m Beginner

Viewers:

Using foreign key constraints provides you with in-depth training on Developer. Taught by Bill Weinman as part of the MySQL Essential Training

Subject:
Developer
Software:
MySQL
Author:
please wait ...