Copy row with many fields by trigger - mysql

I've a table with 48 columns. So it's almost impossible (or let it be) to write a trigger like
CREATE TRIGGER tbl_trigger_update
AFTER update ON tbl1
FOR EACH ROW
update tbl2 set v1=NEW.v1, v2=NEW.v2, ... v48=NEW.v48 where id=old.id
but I dont want to write something like above.
I want to fire a trigger on update/insert on tbl1 to copy/update rows to tbl2
How to solve this issue ?

You can do something with the insert ... select syntax. This is the docs of the statement.

Related

mysql insert ... returning [duplicate]

I am a complete newbie to MySql so please be gentle.
Is there an equivalent of the RETURNING clause in Oracle or the Inserted'/'Deleted tables in SQL Server in MySQL? What I want to do is the following:
Delete a set of rows from table A
Insert the deleted set of rows into table B.
Please help!
Thanks
Unfortunately, you can't do both insertion and deletion in one query, but you can do it all in one transaction if you are using a transactional store engine (like InnoDB). Moreover, RETURNING is supported by Oracle and PostgreSQL but not by MySQL and therefore you need to write separate delete and insert statements.
Using a transaction however, will guarantee that only the successfully copied data will be deleted from tableA. Consider the following:
begin transaction;
insert into tableB select * from tableA where 'your_condition_here';
delete from tableA where 'your_condition_here';
commit;
Why not insert the rows to be deleted from table A in table B and then delete the rows from table A? you can achieve that like this:
insert into tableB select * from tableA where condition;
and then
delete from tableA where condition.

Can I get a row in procedure/function in MySQL

I have a view and a table that are the same. Table is used as a cache because the view takes a lot of time to process.
I have a procedure that is called when the data is changed.
I need to find the row in the view and save it to the table.
Is it a simple way without naming all the columns?
Something like:
Declare row FOR SELECT...
INSERT row INTO ...
I think this is what you want:
INSERT INTO tab
SELECT *
FROM view
WHERE ...
This will insert all rows matching your conditions in where into the table.
Or if you use trigger you could write something like that:
INSERT INTO tab (id, ...)
VALUES (new.id, ...);

Mysql Trigger - Compound statements

Looking for some help on mysql triggers and using multiple statements.
What I am trying to achieve is BEFORE an insert I want to copy records from active table ->history table and then delete them, THEN do the insert. The code works perfectly for the copying of the records from active->history. But the Trigger bonks as soon as I put the delete in ie. it will still move the records but will NOT delete the old rows.
Thoughts/ideas?
CREATE
DEFINER = 'root'#'%'
TRIGGER tradesfinder.On_Checkin_active_Insert
BEFORE INSERT
ON tradesfinder.user_checkin_active
FOR EACH ROW
BEGIN
INSERT INTO user_checkin_history
SELECT *
FROM user_checkin_active
WHERE user_id = new.user_id;
DELETE FROM user_checkin_active
WHERE user_id = new.user_id;
END
Thanks in advance.
C
I think you need an update if the id exists not delete and then insert. Try this:
INSERT INTO `user_checkin_active` (user_id, b, c)
VALUES (?, ?, ?)
ON DUPLICATE KEY UPDATE b =new.b, c = new.c;
I think the trigger syntax as shown above will not work as expected. Instead of triggering a select *, it would be better to work with the new row. So instead of "insert into user_checkin_history select *..." you would instead want to have the trigger to have soemthing to the effect of "insert into user_checkin_history (a,b,c,d) values (new.a,new.b,new.c,new.d)" which then inserts ONLY the contents of the new row.
Otherwise, you end up picking up ALL rowsx for the given user EACH time the user inserts something. That may not be what you intended.

Multiple MySQL queries (no PHP)

I am wondering if it is possible to perform a SQL query then update another table with the generated ID and continue through all of the rows?
I have this SQL query that works but what I need to do is after each row is added to cards to then update merged.cars_id with the last generated ID so they are linked. normally I would do this with PHP but ideally I would like to just do it with MySQL if possible.
MAIN QUERY
INSERT INTO cards (first_contact_date, card_type, property_id, user_id)
SELECT first_contact_date, 'P', property_id, user_id FROM merged
THEN I NEED WITH MATCHING ROWS (Roughly)
UPDATE merged SET merged.card_id = LAST_INSERT_ID (FROM ABOVE) into the matching record..
Is something like this possible and how do I do it?
I would recommend using MySQL triggers to do this
http://dev.mysql.com/doc/refman/5.0/en/create-trigger.html
A trigger is a function that will be executed AFTER or BEFORE the INSERT or DELETE or UPDATE is done over any record of your table.
In your case you need to do a AFTER INSERT on cards that just updates the merged table. Make sure its AFTER insert as you wont be able to access the new row's ID otherwise.
The code would look something like this, assuming the id field from the cards table its named "id"
delimiter |
CREATE TRIGGER updating_merged AFTER INSERT ON cards
FOR EACH ROW BEGIN
UPDATE merged SET card_id = NEW.id;
END;
|
delimiter ;
May I suggest Stored Procedures?
http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
--EDIT--
Ah yes, triggers. For this particular situation, Jimmy has the answer. I will leave this post for the sake of the link.
I would set up a trigger to do this. For mysql, read http://dev.mysql.com/doc/refman/5.0/en/triggers.html. This is what triggers are designed to handle.

Save Query in MySql Trigger

I want to save the mysql query used to delete a row in a table:
Example:
CREATE TRIGGER `table_DEL` BEFORE DELETE ON `table`
FOR EACH ROW BEGIN
INSERT INTO db_bk.table
SELECT *,NOW(),QUERY()
FROM db.table
WHERE table_id= OLD.table_id;
END
As you understand, I want to now if exists a query() function or another method to retrieve the query that activate the trigger (the exact delete query)
Thank you very much
As #devart said - such a function doesn't exist. If you are worried about who will delete from your table then restrict the 'delete' permission to one account. Then you can control how records are removed and when.