Jump to content
Larry Ullman's Book Forums
Sign in to follow this  

Foreign Keys

Recommended Posts

Hi Larry,

Happy New Year!

Hope you had a great festive season!

Larry, I am still learning php and mysql, so please forgive me if the questions seem really basic, but I've tried to research and understand certain concepts, but I'm still confused.

My understanding of foreign keys in a database is that:

1) They provide referential integrity.

2) It is used to link two tables together.

3) It will ensure that a table referencing another table will always be referencing a row that exists in its own table, and a table will never be orphaned.

Now in your book: 'PHP and MySQL for Dynamic Web Sites' which I purchased recently. On page 198, you created a foreign 'customer_id' key in the 'accounts', table that references the 'customers' table.

I understand this concept and it makes sense.


But on page 170 in the 'Effortless E-Commerce' book, you again, created a foreign 'customer_id' key in the 'orders' table that references the 'customers' table on page 169.


So you have different methods and approaches in both books, but they do the same thing!


I'm really confused by this because i'm thinking:

1) How can the foreign 'customer_id' key in the 'orders' table, on page 170('Effortless E-Commerce'), reference the 'customers' table without explicitly referencing it like how you did it on page 198 in the 'PHP and MySQL for Dynamic' book?

2) I've never seen a foreign key get created in this way on page 170 in 'Effortless E-Commerce' book, so can you explain to me


i) How this works, ie, how does the database knows 'customer_id' is a foreign key, and that it is referencing the 'customers' table on page 170, without you explicitly writing it.

I looked in phpmyadmin, and looked in the designer veiw of your database, but there are no links or relationships demonstrated, or shown between any of the tables, even though the diagram on page 167 in the 'Effortless E-Commerce' book, clearly states that there a one-to-many relationship between the tables.


3) So why doesn't the tables in the Ecommerce2 database in phpmyadmin (in the designer veiw, or even in the database tables), not reflect or show the one-to-many relationships that you have stated exists between the tables on page 167.


Thank you in advance


Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this