![]() We can't point to something that's not there (A foreign key insists that we point it to nothing, or something that is there. This is the data integrity part of the equation. You can't delete that, it's referenced by something". What happens if I start deleting products? Unless we've specified some cascading options (see this), the database will throw a wobbly and say "Nope. This means to create an order, we have to have an existing customer and an existing product. We can specify that CustomerID and ProductID are "NOT NULL" (just like other columns). Without foreign keys, we are freely able to:īy using foreign keys, we can redesign the above, thusly: CustomerTableĬustomerID -> References CustomerTable.ID In reality, you'd have the ability to order many products, but I am keeping it simple for brevity): CustomerTable Let's talk about the second one a bit more.Ĭonsider a simple scenario where you have the following three tables (I have simplified this. There have been some good answers, allow me to extrapolate a little bit.Īs "dasblinkenlight" states, speed of the retrieval (the database knows in advance about the relationship between the two tables and can potentially optimize the query), and more importantly, referential/data integrity.
0 Comments
Leave a Reply. |