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

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!

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.

Related

Trigger delete on many-to-many middle table deletion

There are two tables A and B. As they have a many-to-many relation, there's also table C.
A
------
id PK
B
------
id PK
C
------
id_A PK
id_B PK
Now, a row B only exists only exists when at least one row of A has a relation to it, and one B may contain a relation to two or more different rows of A.
My question is, how do i automatically delete a row from B if there isn't any foreign to it in C? My initial though was to set a trigger, but i'm not to sure about this and i'd want a second opinion in how to proceed. Thank you.
First, one assumes that the data is initially set up correctly. That is, the only b records are the ones that meet your condition.
Then, the solution involves triggers on table c. When a row is deleted, it would check:
Does id_b have any other rows in the table?
If not, then delete the row.
This can actually be a bit tricky. In general, you don't want to query the table being triggered. So, I might suggest an alternative approach:
Add a counter on b.
Add insert/update/delete triggers on c that increments or decrements the count in b.
If the counter is 0 (or 1 before decrementing), then delete the row.
Gosh, you might find that the counter itself is sufficient, and there is no need to actually delete the row. You can get that effect if you use a view:
create view v_b as
select b.*
from b
where ab_counter > 0;
You could also create a view on b and not have to deal with triggers at all:
create view v_b as
select b.*
from b
where exists (select 1 from c where c.b_id = b.id);
#Gordon's solution above is great, However a slight modification might help.
First, one assumes that the data is initially set up correctly. That is, the only b records are the ones that meet your condition.
Then, the solution involves triggers on table c. When a row is deleted, it would check:
Does id_b have any other rows in the table?
If not, then delete the
row.
This is a bit tricky because you have to check if other rows exist. This check can be automated by using,
FOREIGN KEY(id) REFERENCES B(id) ON DELETE RESTRICT
on table C. Now you only need to delete row from B in the trigger without any checks, since the restrict constraint will automatically check if row exists in table C and restrict the delete of a referenced row in table B else delete is successful.

BEFORE or INSTEAD OF Insert SQL Server triggers

I have a small problem. What I want to achieve is before inserting data to table A, I want to check if this data is in table B and table C.
When tables B and C don't have this data, I'd like to add data from 'inserted' to table B and C (and insert data to table A at the end), but if they have already this row, I don't want to insert anything to table A and B (and don't know to insert anything to table A as well).
Is there any way I can control if the data from inserted will be inserted in table A or not?

Achieving autonomous transaction in SQL server 2008

I have a requirement as below.
I am using SQL Server 2008.
1. Table A (Id)
2. Table B (Id, Attr1, Attr2, Attr3)
Table A and Table B have same no of rows. Id is primary key in both the tables but there is no referential relationship defined.
I have a trigger on Table A for INSERT. If a record is inserted in Table A, I insert the same ID in Table B and calculate few attributes and populate them in Table B for that ID. I am achieving this using a trigger.
Now if the transaction fails in Table B, I don't want the transaction in Table A also to be failed. Irrespective of the trigger succeeds or fails in updating Table B, I want the Table A transaction to be success and not to be dependent on Table B transaction. How do I achieve that?
Helpful link How to create an autonomous transaction in SQL Server 2008

How do I only update rows in table A with the data which has changed in table B

I'm inserting data into table A. After a few days have past the data in the Table A might be out of date from the live system.
What I want to do is load the rows that would have changed from the live system into say a temp table and update only the rows which have to corresponding GUID to update with the correct data.
I need help with how the update statement should be formatted.
This would update only the column name Table A with column name of TableB with UID matches on both table: Give a try
update set a.name=b.name from TestA a INNER JOIN TestB b on a.UID=b.UID;

SQL Server INSERT trigger does not work

I have a trigger to copy over the data from Table A to table B when table A is changed
The trigger is like this :
ALTER TRIGGER ATrigger
ON A AFTER INSERT, DELETE, UPDATE
AS
BEGIN
SET NOCOUNT ON;
DELETE FROM B WHERE id IN (SELECT id FROM deleted)
INSERT INTO B(Id, col1,col2) (SELECT i.Id, i.col1, i.col2 FROM inserted i)
END
But i see not all the data inserted in A are copied to B, the data copied seems very random
I was searching around, found it might caused by multi-insert, someone is suggesting using cusor, but i think for mine, it should be ok to insert or delete from the inserted, deleted table using this two sql.
Please advise, thanks!
I'm not certain this is your problem but your trigger has 2 "gotchas". First on an insert the deleted table will have no rows in it so no deletes will be done. Second is the reverse and potentially your problem. On a delete the inserted table has no rows. So all of the IDs are going to be deleted from table B but not re-inserted. On top of this if ID is not a unique key for table A then when you insert a second copy of it you will be deleting all of your history in table B and only adding the "new" history.
If you can provide more information on the structure of the 2 tables and the purpose of the trigger, not to mention any patterns on the rows being inserted or not being inserted as the case may be we can be of more help.