Warren
Warren

Reputation: 2014

SQL: Cascade "insert" and insert on update if not exists

What is the best way to accomplish this?

Table A and Table B have "Master-Slave" relationship via a FK on Table B. The key is set up for cascade delete and update.

Table B is new and thus does not have as many records as A.

As Table A is inserted, I want Table B to have a new record with the ID field of Table A completed with everything else blank ready for user input.

As Table A is updated, I want Table B to have a new record with the ID field of Table A completed with everything else blank ready for user input if Table A's ID does not yet exist in Table B.

Triggers, I assume?

Many thanks!

Upvotes: 4

Views: 8216

Answers (1)

TizzyFoe
TizzyFoe

Reputation: 1499

I think you need to use an insert trigger on table A.

whenever you insert into A, check if the ID exists in B and if not, then insert into B.

Upvotes: 3

Related Questions