Jump to content

Recommended Posts

I supposed to make a web based inventory system, i have a table name products and other table name brand. wat i want is its just a one query in the brand table, when i update or delete some data in the brand table the data in the product table will be deleted also based on the foriegn key in the product table.

 

 

Foreign keys -- sure no problem.  If you design it right, then updates to a brand would have no effect, because your normalized design would also use numeric keys to link product and brand together.  In that way, changing for example, the name of a brand would not effect any products that were related to that brand, because the "brandname" column is in the brand table.

 

Now, when you state that in deleting a brand you want to delete all related products, that is known as a cascading delete.  Depending on the database, that can be set up automatically, or may require a trigger.  In MySQL you can set up a cascading delete, assuming a few things, one of which was mentioned by priti:

 

Assuming you're using MySQL:

 

-You need to be using MySQL 5

-You need to have created your tables using the Innodb engine.

-Your table declaration needs to include the ON DELETE CASCADE clause which would have been something like this in your definition of the product table:

 

FOREIGN KEY (brand_id) REFERENCES brand(brand_id)

                      ON DELETE CASCADE

 

If you set this up properly, deleting a brand will delete all products of that brand.  While cascading deletes are convenient for some things, they also represent a significant danger as a user could unknowingly wipe a lot of rows out of a database with one careless delete.  Often you want to control this programatically.  Caveat Emptor.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.