Welcome Guest, Not a member yet? Register   Sign In
Help with Foreign Keys
#8

[eluser]sl3dg3hamm3r[/eluser]
The different types of constraints you can use are pretty well explained here.

As said before, these rules help you to ensure data-integrity WITHOUT programming any logic/checks.

Again an example: A small shop with two tables 'customer' and 'boughtproduct' - boughtproduct contains all products customers have ever bought. This means it also has a foreign key referencing the customer-table.

Now you could define a 'delete cascade' - constraint. If you delete the customer, all its related products are also deleted, transaction-safe. You don't need to bother in your code about this.

Another contraint: 'delete restrict'. IF there are products with the customer connected, the db will return an error if you try to delete the customer. There you would need to code a little bit, since you should check before deleting a customer if there are products. Otherwise the user might face an ugly error from the db (which anyhow shouldn't be shown for safety-reasons).

There are more constraints described if you follow the link. It is all about data-integrity, which can be provided by the database itself, without writing any code. This makes it save, because this kind of checks you might forget quite easily while developing your application.

Hope this cleared it up a little bit...
Sl3dg3


Messages In This Thread
Help with Foreign Keys - by El Forum - 10-28-2008, 12:39 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 03:11 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 05:07 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 05:11 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 05:16 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 05:18 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 09:06 AM
Help with Foreign Keys - by El Forum - 10-28-2008, 10:58 AM



Theme © iAndrew 2016 - Forum software by © MyBB