Marwan Ayad
Marwan Ayad

Reputation: 65

Delete Multi rows with 2 condition

i have a Table like below


CategoryGroup


now i have table like this

product_group_id -- product_group_en_name -- parent_group_id
    1                    Phones                Null   <----  category name
    2                    LapTops               Null   <----  category name
    3                    Nokia x3               1  <---- product under Phones Category
    4                    HP probook             2  <---- product under LapTops Category

here what i want (when i delete Category , i want all products under that category delete too)

Upvotes: 0

Views: 101

Answers (2)

Mahmoud Gamal
Mahmoud Gamal

Reputation: 79979

Use the Cascading Referential Integrity Constraint:

ON DELETE CASCADE

which:

Specifies that if an attempt is made to delete a row with a key referenced by foreign keys in existing rows in other tables, all rows containing those foreign keys are also deleted.

When defining this foregin key parent_group_id

Upvotes: 2

evgenyl
evgenyl

Reputation: 8117

You can sdd foreign key to second table with option DELETE CASCADE. See http://www.mssqltips.com/sqlservertip/2743/using-delete-cascade-option-for-foreign-keys/

Upvotes: 0

Related Questions