In my MySQL base, with engine=innodb and 1 file by partition, I have:
Table 1 partitioned by date
Table 2 that counts the entries of table 1 by date
Table 1 is filled by a software, and Table 2 is filled by triggers on Table 1, as below:
CREATE TRIGGER tgi_table1 AFTER INSERT ON table1 FOR EACH ROW INSERT INTO table2 values ( NEW.date, 1 ) ON DUPLICATE KEY UPDATE count = count+1;
CREATE TRIGGER tgd_table1 AFTER DELETE ON table1 FOR EACH ROW UPDATE table2 SET count = count-1;
Also, there is a script that runs periodically and execute DROP PARTITION on the old partitions of table1.
My question is: will these DROP PARTITION activate the trigger tgd_table1?
Thank you.
No, it wont use DELETE query for deleting records. It is dropping the data in a different way. Here is the explanation from mysql docs:
DELETE: The trigger activates whenever a row is deleted from the table; for example, through DELETE and REPLACE statements. DROP TABLE and TRUNCATE TABLE statements on the table do not activate this trigger, because they do not use DELETE. Dropping a partition does not activate DELETE triggers, either.
Related
I am trying to create a trigger to test my application such that as soon as a row is inserted delete it.
CREATE TRIGGER triger1 AFTER INSERT ON tableA
DELETE from tableA where columnA = new.coulmnA;
But it is throwing me error saying 'DELETE' (delete) is not a valid input at this position.
You cannot delete rows from the same table where you create you TRIGGER. This would create a deadlock.
That is because MYSQL locks the table on an INSERT operation.
I have to Write one or more triggers that keep track of how many total records have been in the sakila_film table in a single variable, but I am having trouble figuring out how to do this trigger the table is taken from http://dev.mysql.com/doc/sakila/en/sakila-structure-tables-film.html .This is what I have tried the code below but I am getting an error and I don't know any other way i could do it.
create trigger records after Update on sakila_film Count(*) from sakila_film;
Where are you planning on storing this count of total rows in the film table?
If you are going to manage this with triggers, it seems like you'd need two triggers. One for INSERT, one for DELETE. An UPDATE statement won't change the number of rows in the table. (Note that triggers are NOT fired for foreign key actions; which is a concern if there's a foreign defined with a DELETE rule.) And the triggers will not be fired for a TRUNCATE statement.
Setting aside those concerns, we'd need somewhere to store the rowcount over time. As an example:
CREATE TABLE rowcount_history_film
( ts TIMESTAMP DEFAULT CURRENT_TIMESTAMP
, rowcount INT NOT NULL
, KEY rowcount_history_film_IX1 (ts)
) Engine=MyISAM;
And then we could use trigger to insert a row into that table whenever a DML statement changes the number of rows in the film table:
DELIMITER $$
CREATE TRIGGER film_ad
AFTER DELETE ON film
FOR EACH ROW
BEGIN
INSERT INTO rowcount_history_film (ts, rowcount)
SELECT NOW(), COUNT(*) FROM film;
END$$
CREATE TRIGGER film_ai
AFTER INSERT ON film
FOR EACH ROW
BEGIN
INSERT INTO rowcount_history_film (ts, rowcount)
SELECT NOW(), COUNT(*) FROM film;
END$$
DELIMITER ;
I'm using Mysql in phpMyAdmin,where i have to delete a entry from tableA if i insert a row with same primary key.I thought to do it in trigger of tableA BEFORE INSERT
For Ex,
if the tableA contains
1 Hai Hello
here 1 is the primary key
And now if i insert a row 1 Bye Hello then the trigger BEFORE INSERT will delete the old entry and then the new row (2nd) will be inserted. But Mysql has restriction of not being able to update a table inside a trigger defined for that same table.
It gives the error
#1442 - Can't update table 'tableA' in stored
function/trigger because it is already used by statement which invoked
this stored function/trigger.
So i changed my way, i called a procedure from trigger BEFORE INSERT of tableA and in that procedure i do the task what i thought to do in trigger. But unfortunately i'm getting the same error.
In trigger BEFORE INSERT i simply called the procedure as
CALL proce1(new.Reg_No);
In procedure i have done this
DECLARE toup integer;
select count(*) into toup from tableA where Reg_No=reg;/*Here Reg_No is primary key */
if toup > 0 then
delete from tableA where Reg_No=reg;
end if;
Need some other Idea to achieve this. Help Me.....
I don't like to use triggers so much because they are hard to manage somethimes. Also they will cause you a downgrade in performance. I am not against trigger as they can be handy in some cases.
In your case have you though of using REPLACE(....) or INSERT INTO .... ON DUPLICATE KEY UPDATE or even INSERT INTO IGNORE .....
REPLACE(....) will delete a record if a record is found and insert a new one with the same ID.
INSERT INTO .... ON DUPLICATE KEY UPDATE will allow you to override existing field if a duplicate is found.
INSERT INTO IGNORE ..... will allow you to ignore the new inserted row if one already exists
Since you mentioned in the comments that you are importing records from a file, then try to use LOAD DATA INFILE logic which will allow you to REPLACE field on duplicate
LOAD DATA LOCAL INFILE 'x3export.txt'
REPLACE INTO TABLE x3export
So i see that SQL has some tables which hold the newly inserted and deleted data from tables one may refer. I didn't notice such a table for updated data. I am currently working with triggers and i need to apply a trigger to an update. How do i do that?
USE [examene]
GO
/****** Object: Trigger [dbo].[trig1] Script Date: 6/8/2013 6:48:26 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER trigger [dbo].[trig1] on [dbo].[participari]
after insert,update,delete
as
begin
if (exists (select * from deleted))
rollback
if (exists (select * from inserted,proiecte
where inserted.idpr = proiecte.idpr
and deadline<dela union
select * from inserted,proiecte
where inserted.idpr = proiecte.idpr and inserted.panala>proiecte.deadline))
rollback
end
this is my trigger so far
There is no such thing as updated virtual table. When update occurs old values might be found in deleted and new values in inserted
Use the inserted and deleted Tables
The deleted table stores copies of the affected rows during DELETE and
UPDATE statements. During the execution of a DELETE or UPDATE
statement, rows are deleted from the trigger table and transferred to
the deleted table.
The deleted table and the trigger table ordinarily
have no rows in common. The inserted table stores copies of the
affected rows during INSERT and UPDATE statements. During an insert or
update transaction, new rows are added to both the inserted table and
the trigger table. The rows in the inserted table are copies of the
new rows in the trigger table.
An update transaction is similar to a delete operation followed by an
insert operation; the old rows are copied to the deleted table first,
and then the new rows are copied to the trigger table and to the
inserted table.
I need determine each of this this 3 commands in trigger [UPDATE,DELETE,INSERT].For last 2 I do next:
IF EXISTS (SELECT * FROM inserted)
BEGIN
END
ELSE IF EXISTS (SELECT * FROM deleted)
BEGIN
END
How can I get updating rows?
Thanks.
Not exactly sure what you're trying to accomplish, but you can test if it's an UPDATE if both inserted (values after update) and deleted (values before update) exist. From the documentation:
The deleted table stores copies of the affected rows during DELETE
and UPDATE statements. During the execution of a DELETE or UPDATE
statement, rows are deleted from the trigger table and transferred to
the deleted table. The deleted table and the trigger table ordinarily
have no rows in common.
The inserted table stores copies of the affected rows during
INSERT and UPDATE statements. During an insert or update
transaction, new rows are added to both the inserted table and the
trigger table. The rows in the inserted table are copies of the new
rows in the trigger table.
Thus, if inserted exists but not deleted, it's an INSERT; if deleted exists but not inserted, it's a DELETE; if they both exist, it's an UPDATE.