Deleting Rows off Multiple tables

  • Hello All,

    I am still trying to learn the ropes of T-SQL and I would just like to ask how to go about deleting rows off different tables. Let's say TABLE A has a column named A1 which references a row containing data and that data can be referenced from TABLE B.

    How would I go about deleting the row containing the same data off the two different table? Would it be possible with one script or I would have to go and do it separately.

     

     

     


    Regards,

    Timber

  • First of all.  This sounds like you need to create a relation between those 2 tables to guarantee data integrity.  The first thing you can do is to set this relation to auto-delete the child data when the parent data is destroyed.  This may not always be the best solution as you can sometimes wipe out half the data in the db with a single error (if almost all your foreign keys are created with on delete cascade on).

     

    The other option is to create a stored proc that does the 2 deletes.  Always assuming that you have a foreign key, you need to delete the child data first, then the parent data with 2 distinct delete statements.   Both options are viable, just take the one you like better for your environement.

     

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply