Delete rows in relational database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Tea Maker
    New Member
    • Jul 2007
    • 45

    Delete rows in relational database

    Hi,
    I have a relational database. Pretty simple I created it just for learning from it.

    I have two tables "Categories " and "Subcategories" .

    I have a field called "CategoryID " in the Subcategories table that is a foreign key , and is related to the first table (categories).

    Now what I want to do is, when I delete a row in the "Categories " table, I also want to delete the related rows in the other table (if any exist) at the same time.

    I hope I explained the problem accurately, if you need any details please let me know

    Thanks in advace,
    Wassim
  • patelaashish
    New Member
    • Oct 2006
    • 26

    #2
    Hi,

    You need to delete the rows from the subcategories table first and then delete the corresponding rows from the categories table.

    It should be pretty easy as the CategoryID should be in SubCategories table and that the field you need to use to delete rows from the subcategories table.

    I hope this helps.

    Aash.

    Comment

    • Jim Doherty
      Recognized Expert Contributor
      • Aug 2007
      • 897

      #3
      Look at the CASCADE DELETE capability defined when you set the relationship between the two tables. This is inherent and designed to provide for integrity as part of the db structure

      Jim

      Comment

      • Tea Maker
        New Member
        • Jul 2007
        • 45

        #4
        Originally posted by Jim Doherty
        Look at the CASCADE DELETE capability defined when you set the relationship between the two tables. This is inherent and designed to provide for integrity as part of the db structure

        Jim
        Hi Jim,

        yes that's what I did exactly. That's amazing. if you force your tables to "Cascade Delete", and you delete a row in a parent table, all the rows in the childs tables will also be removed :)

        Thanks

        Comment

        • Jim Doherty
          Recognized Expert Contributor
          • Aug 2007
          • 897

          #5
          Originally posted by Tea Maker
          Hi Jim,

          yes that's what I did exactly. That's amazing. if you force your tables to "Cascade Delete", and you delete a row in a parent table, all the rows in the childs tables will also be removed :)

          Thanks
          You got it..... glad it helped you

          Jim

          Comment

          Working...