I Have Table Name Called Widgets And There is Delete Trigger On it. When i Delete one specific Row I want Delete Also Other Rows Which Has Relotionship
Trigger Like
ALTER TRIGGER [dbo].[Trigger_sys_widgets_Delete]
ON [dbo].[sys_widgets]
AFTER DELETE
AS
BEGIN
Delete From sys_Widgets Where parent in (Select id From deleted);
END
But This doesn't delete other Rows Just First Row Deleted !
Can i make This Trigger Works on Depete loop till Nothing Left in relative Childs
Also Try To Cascade Delete Like
Alter Table sys_widgets Add Constraint FK_sys_widgets foreign Key (parent) References sys_widgets(id) On Delete Cascade;
Is This Possible To Do That For Same Table
You may use recursive CTE to delete all children by one trigger run.
ALTER TRIGGER [dbo].[Trigger_sys_widgets_Delete]
ON [dbo].[sys_widgets]
AFTER DELETE
AS
BEGIN
WITH ForDelete AS
(
SELECT widgets.id
FROM [dbo].[sys_widgets] AS widgets
INNER JOIN deleted ON widgets.parent = deleted.id
UNION ALL
SELECT widgets.id
FROM [dbo].[sys_widgets] AS widgets
INNER JOIN ForDelete ON widgets.parent = ForDelete.id
)
DELETE FROM [dbo].[sys_widgets]
WHERE id in (SELECT id FROM ForDelete);
END
Related
Hey guys i created two tables in oracle sql, first one has 2 columns, and the second has 3 columns(one of them is a foreign key from Pk of first table).
I want to create a trigger that AFTER i update the column of the foreign key in second table, will update the other columns according to the value of the pk.
Table1(idF, name)
table2(id, idF, name)
I want to create a trigger that when i update idF(foreign key) in table2 will display the same name as in table1.
You can create trigger in oracle as follows:
Create or replace trigger trg_table2
Before update of idf on table2
For each row
When (old.idf <> new.idf and new.idf is not null)
Begin
Select name into :new.name
from table1
Where idf = :new.idf;
End;
/
you can use this after update trigger on table1
delimiter $$
create trigger MyTrigger after update on table1
for each row
begin
update table2 set name = new.name where idF = new.idF;
end$$
I'm working with SQLite,I am using XOR single table inheritance, I want to create a trigger that enables me to:
Check before insertion if the InstructionRefs.id is already created in the table RideHeightRefs
Ckeck before insertion that the InstructionRefs.id does not exist in the other inherited table StrappingRefs.
I took some oracle PL/SQL code and changed it, I guess I am writing it wrong starting from IF NOT EXISTS (SELECT id...):
CREATE TRIGGER IF NOT EXISTS insert_instructionRefs_trigger BEFORE INSERT ON InstructionRefs
BEGIN
IF NOT EXISTS (SELECT id FROM RideHeightRefs AS RHR INNER JOIN InstructionRefs
IR ON RHR.id = IR.id)
BEGIN
SELECT RAISE(FAIL, '"RideHeightRefs" key is unknown. Insertion in
"instructionRefs" is impossible.')
END'
IF NOT EXISTS (SELECT *
FROM (SELECT RideHeightRefs
FROM StrappingRefs
UNION ALL
SELECT RideHeightRefs
FROM InstructionRefs) T
WHERE RideHeightRefs IN (SELECT RideHeightRefs
FROM NEW))
BEGIN
SELECT RAISE(FAIL, '"RideHeightRefs" key is used in another table. Insertion in "StrappingRefs" is impossible.')
END
END
How can I modify the code to make it compatible with sqlite syntax ?
To check that the corresponding row in the base table exists, just use a foreign key constraint.
SQLite has no IF statement. To check for something, add a WHERE clause to the SELECT FAIL, or use the trigger's WHEN clause:
CREATE TRIGGER IF NOT EXISTS insert_instructionRefs_trigger
BEFORE INSERT ON InstructionRefs
WHEN EXISTS (SELECT *
FROM StrappingRefs
WHERE id = NEW.id)
BEGIN
SELECT RAISE(FAIL, '"RideHeightRefs" key is used in another table. Insertion in "StrappingRefs" is impossible.');
END;
I'm trying to create after insert trigger to delete a row from table whenever I insert the same row in the opposite table using the ID
But it doesn't work
Create Trigger [dbo].[MeritedDeceased] On [dbo].[Deceased]
After Insert
As
Begin
Delete From dbo.Merited Where dbo.Deceased.ID = dbo.Merited.ID
End
I think you mean to do something like this (maybe)...
CREATE TRIGGER [dbo].[MeritedDeceased] ON [dbo].[Deceased]
AFTER INSERT
AS
BEGIN
DELETE M
FROM [dbo].[MeritedDeceased] M
INNER JOIN Inserted I
ON I.ID = M.ID
;
END
;
Translation: Every time a row is inserted into dbo.Deceased, delete any rows with the same (inserted) ID from dbo.MeritedDeceased.
FYI, this TRIGGER won't just delete one row, but also a batch of rows that were inserted together. If that's what you want, then this should help.
Yii::app()->db->createCommand("DROP TRIGGER IF EXISTS update_todo;")->execute();
Yii::app()->db->createCommand("CREATE TRIGGER update_todo AFTER DELETE ON user_todo_send FOR EACH ROW BEGIN "
. " UPDATE todo SET status = 1 WHERE id = OLD.id_todo; END;")->execute();
In response, I receive an error :
Can't update table 'todo' in stored function/trigger because it is
already used by statement which invoked this stored function/trigger..
I am going to guess that you are really using MySQL. Use an after delete trigger. Using your syntax, that would look like this (and I assume you have the right delimiter statements:
DROP TRIGGER IF EXISTS `update_todo`;
CREATE TRIGGER `update_todo` AFTER DELETE ON `user_todo_send` FOR EACH ROW
BEGIN
UPDATE `todo`
SET status = 1
WHERE id IN (SELECT OLD.id_todo
FROM user_todo_send
WHERE OLD.id_todo = todo.id
);
END;
This is really simpler to write as:
DROP TRIGGER IF EXISTS `update_todo`;
CREATE TRIGGER `update_todo` AFTER DELETE ON `user_todo_send` FOR EACH ROW
BEGIN
UPDATE `todo`
SET status = 1
WHERE id = OLD.id_todo
END;
Or, forget triggers altogether and add a cascading delete foreign key reference.
A need a trigger to run after a row is deleted from a table.
This trigger should delete rows from another table where a field on that table has a value matching the value of one of the columns of the deleted row from the other table.
If triggering the delete from a delete:
create trigger trg_mainTable
on dbo.mainTable
after delete
as
begin
delete
from dbo.relatedTable
where someColumn in
(
select someColumn
from deleted
)
end
go
If triggering the delete from an update:
create trigger trg_mainTable
on dbo.mainTable
after update
as
begin
if update(someColumn)
begin
delete
from dbo.relatedTable
where someColumn in
(
select someColumn
from deleted
)
end
end
go