Delete/update a trigger and cascade

Source: Internet
Author: User

The trigger cannot be used to perform cascade update and deletion. If the foreignkey-to-field name relationship (or the foreignkey-to-unique relationship) uses SQL Server to establish dri. This dri constraint is used for the first test. If you update or delete a dri constraint, only the trigger is triggered when all constraints are passed. Therefore, the DRI foreignkey constraint cannot be associated with any update or cascade deletion.

You can use triggers to update and delete cascade operations without declaring the foreignkey (or reference) constraint. Field Names and unique constraints should still be used,.

Delete trigger, delete rows in the primary table in the relevant table, or set all corresponding foreignkeys to null (or their default values ). Cascade deletion easily executes nested triggers and deletes all rows in the relevant table. Cascade rows into rows before cascade rows must be stacked to other levels in the relevant table. Cascade setnull and setdefault may be caused by multiple rows of considerations and trigger issues. However, foreignkeys is not part of the field names of the relevant tables. If they can be updated, they only need to be in the slave trigger.

Used to implement cascade updates. An update trigger on the master table should perform the required data modifications in the auxiliary table. Again, as long as the foreignkey being updated is not part of the field name dependent on the table, it is only in the update trigger.

Provides examples provided with SQL Server 4.2 X to implement a reference integrity trigger in document update and deletion. The attached SQL Server trigger document description is used to enforce business rules, instead of referring to the integrity trigger, however, the information on how to trigger a job and multirow considerations is informative (See Chapter 6th of this SQL Server "Database Developer assistant ).

In the following example, a cascade Delete titles table is used to delete a trigger that matches all rows in the foreignkey value titleauthor table. Because title_id is part of the field name of titleauthor, this trigger assumes that there is no subsequent table with foreignkeys titleauthor reference. Note that this operation will run normally and even act as a multiline delete operation.

CREATE TRIGGER DelCascadeTrig  ON titles  FOR DELETEAS  DELETE titleauthor    FROM titleauthor, deleted    WHERE titleauthor.title_id = deleted.title_id

The following is an example of setnull to delete the trigger that updates all rows in the titleauthor table with matching foreign key values. Again, because title_id is part of the field name of titleauthor, this trigger assumes there are no tables with foreignkeys titleauthor reference at the subsequent level. This will even work correctly as a multi-row delete operation.

CREATE TRIGGER DelSetNullTrig  ON titles  FOR DELETEAS    UPDATE titleauthor      SET titleauthor.title_id = NULL        FROM titleauthor, deleted        WHERE titleauthor.title_id = deleted.title_id

The following is an example of a cascade update trigger updating all rows in the titleauthor table with matched foreign key values in the titles table. Again, because title_id is part of the field name of titleauthor, this trigger assumes there are no tables with foreignkeys titleauthor reference at the subsequent level.

CREATE TRIGGER UpdCascadeTrigBad  ON titles  FOR UPDATEAS  IF UPDATE(title_id)  BEGIN    UPDATE titleauthor      SET titleauthor.title_id = inserted.title_id        FROM titleauthor, deleted, inserted        WHERE titleauthor.title_id = deleted.title_id    END  END

This will not work properly for multi-row update because there is no need to match its corresponding row Insert table and deleted table given row without adding the second unique identifier will never change its value method. This problem occurs when you need to take a part of the field names in the table on which foreignkey depends and the field names in the table are referenced by other foreignkeys.

To prevent multi-row update, the previous trigger should overwrite the original table that will prevent the update from affecting multiple rows (in this case, the title ). Note that the update in the trigger may well update multiple rows in titleauthor. This solution may only cause the problem to occur again at the next level of the stack.

CREATE TRIGGER UpdCascadeTrig  ON titles  FOR UPDATEAS  IF UPDATE(title_id)  BEGIN    IF @@ROWCOUNT = 1      UPDATE titleauthor        SET titleauthor.title_id = inserted.title_id          FROM titleauthor, deleted, inserted          WHERE titleauthor.title_id = deleted.title_id    ELSE      ROLLBACK TRANSACTION      RAISERROR ('Multi-row update on table "titles" not allowed.')    END  END

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.