I am trying to bring together two separate designs here so I understand the overall approach may not be ideal. Basically through user input in PHP table1 and part of table2 is populated and then in turn I need the rest of table2 and table3 to be populated automatically.
I have the following db
with this trigger
DELIMITER |
CREATE TRIGGER new_trigger AFTER INSERT on table2
FOR EACH ROW BEGIN
INSERT INTO table3(att1, DateCreated, DateUpdated)
VALUES('PG', now(), now());
UPDATE table2 SET table3Id = table3.LAST_INSERT_ID();
END;
|
DELIMITER ;
although MySQL accepts the trigger as written without any errors I get this error when the app runs:
General error: 1442 Can't update table 'table2' in stored function/trigger
because it is already used by statement which invoked this stored function/trigger
I believe this comes from MySQL triggers can't manipulate the table they are assigned to. So if this is the reason for the error how else can I achieve the same results?
EDIT: (ANSWER)
Thanks to the help from mootinator here and in chat. Here is his solution that works as I need it to.
CREATE TRIGGER new_trigger BEFORE INSERT on table2
FOR EACH ROW BEGIN
INSERT INTO table3(att1, DateCreated, DateUpdated)
VALUES('PG', now(), now());
SET NEW.table3Id = LAST_INSERT_ID();
END;
You can't use an AFTER trigger because the new change you make would (potentially) cause the AFTER trigger to be run again in an infinite loop. You have to use a BEFORE trigger to edit the row before it gets written.
Try eg:
CREATE TRIGGER new_trigger BEFORE INSERT on table2
FOR EACH ROW BEGIN
INSERT INTO table3(att1, DateCreated, DateUpdated)
VALUES('PG', now(), now());
SET NEW.table3Id = LAST_INSERT_ID();
END;
Related
I have one table called number_list which have columns like:
id, name, number, server, status, last_act, user_id, created_at, disable, notify,fcm
I want update last_act when there any changes in status column only. Currently its updating last_act whenever any changes in any column.
Let me know if its possible with MySQL. Thanks
Setting up a trigger like the following should accomplish what you have asked.
DELIMITER $$
CREATE TRIGGER number_list_update_trigger
BEFORE UPDATE
ON number_list
FOR EACH ROW
BEGIN
if (NEW.status = OLD.status) THEN
set NEW.last_act = OLD.last_act;
END IF;
END $$
DELIMITER ;
I have these tables in a MYSQL database:
tBook (idBook,BookName)
tAuthor (idAuthor,AuthorName)
tBookAuthor (FK_Book,FK_Author)
I want to insert in one stored procedure a new book. In the same procedure I want to insert into the table tBookAuthor.
INSERT INTO `tBook`
(`idBook`, `BookName`)
VALUES
(23, 'myBookName')
INSERT INTO `tBookAuthor`
(`FK_Book`, `FK_Author`)
VALUES
(LAST_INSERT_ID(), 526)
The problem was that I tried to modify a storec procedure which already exists.
I did this in PHPmyAdmin.
If you want to create a new stored procedure you do it like this:
DELIMITER //
CREATE PROCEDURE new_person(
first CHAR(35), last CHAR(35), email CHAR(255), tool_id INT)
BEGIN
START TRANSACTION;
INSERT INTO `tBook`
(`idBook`,
`BookName`)
VALUES
(23
'myBookName')
INSERT INTO `tBookAuthor`
(`FK_Book`,
`FK_Author`)
VALUES
(LAST_INSERT_ID(),
526)
COMMIT;
END//
DELIMITER;
If you want to ALTER a stored procedure in phpmyadmin you do it like this:
BEGIN
START TRANSACTION;
INSERT INTO `tBook`
(`idBook`,
`BookName`)
VALUES
(23
'myBookName')
INSERT INTO `tBookAuthor`
(`FK_Book`,
`FK_Author`)
VALUES
(LAST_INSERT_ID(),
526)
COMMIT;
END
Instead of a stored procedure, since you are using PHP, I suggest doing 2 inserts, since the second insert is foreign keys you'll have to select the author, get the last insert ID of the book and do a simple insert there.
It's a lot less trouble.
I also have some trouble understanding your dB structure, can one book be written by more than one author? If so, my bad, otherwise, save yourself the trouble of the helper table and just put the author id on the book.
Can anyone explain me what below code actually do, I have this on my mysqlDB.sql exported file
DROP TRIGGER IF EXISTS `alertupdateevent`;
DELIMITER $$
CREATE TRIGGER `alertupdateevent` AFTER INSERT ON `RECOG`
FOR EACH ROW BEGIN
INSERT INTO `mydb`.`ALERTUPDATES` (`FIRSTNAME`, `LASTNAME`, `CAMNAME`, `TIMESTAMP`, `RESERVE1`, `ALERTTYPE`, `IMGURL`) VALUES (NEW.FIRSTNAME, NEW.LASTNAME, NEW.DOB, NEW.TIMESTAMP, NEW.FACEID, 'RECOG' , NEW.FACEURL);
END
$$
DELIMITER ;
Currently I dealing a project which is not developed by me, and I am not well familiarized with mysql.
The problem I am facing is I have two table in DB like RECOG and ALERTUPDATES and I need to insert data to both of these table(same data), and I can see only one php which insert data to the table `RECOG'.
So my question does the above piece of code insert data automatically to table ALERTUPDATES when data insert on RECOG table by php.
Yes, you are correct.
Trigger are used to INSERT UPDATE on some TABLE based on action perform on some TABLE actions like insert, update or delete.
Refer MySQL triggers
Try this..
CREATE TRIGGER trigger_name
BEFORE INSERT
ON table_name FOR EACH ROW
BEGIN
-- variable declarations
-- trigger code
END;
Parameters or Arguments
trigger_name
The name of the trigger to create.
BEFORE INSERT
It indicates that the trigger will fire before the INSERT operation is executed.
table_name
The name of the table that the trigger is created on.
RESTRICTIONS
You can not create a BEFORE trigger on a view.
You can update the NEW values.
You can not update the OLD values.
Example:
DELIMITER //
CREATE TRIGGER contacts_before_insert
BEFORE INSERT
ON contacts FOR EACH ROW
BEGIN
DECLARE vUser varchar(50);
-- Find username of person performing INSERT into table
SELECT USER() INTO vUser;
-- Update create_date field to current system date
SET NEW.created_date = SYSDATE();
-- Update created_by field to the username of the person performing the INSERT
SET NEW.created_by = vUser;
END; //
DELIMITER ;
Ref:http://www.techonthenet.com/mysql/triggers/before_insert.php
I know this is possible using transaction but I want to do it using single mysql query.
this is which common format transaction
START TRANSACTION;
SELECT #A:=SUM(salary) FROM table1 WHERE type=1;
UPDATE table2 SET summary=#A WHERE type=1;
COMMIT;
but I need to know a single mysql query is possible?
Normally, you can not do this in MySQL. But you can do this using concat query.
INSERT INTO your_table
(value1,value2,value3)
VALUES
(a,b,c),
(d,e,f),
(i,j,k);
But this is not your question answer. so Your question answer is NO. MYSQL is not support it still now.
No, it can't be done in single statement like
insert into table1,table2
either you do separately like
insert into table1 ...
insert into table2 ...
(OR)
Wrap the insert statements in stored procedure and call that procedure like
create procedure sp_insert_multiple
as
begin
insert into table1 ...
insert into table2 ...
end
Call the SP
exec sp_insert_multiple
You can't do this. However, you can use a transaction and have both of them be contained within one transaction.
START TRANSACTION;
INSERT INTO table_1 VALUES ('1','2','3');
INSERT INTO table_2 VALUES ('one','two','three');
COMMIT;
See the following rule
Normally it is not possible to insert multiple table in single query. you can insert multiple row in a single table . like as
INSERT INTO tbl_test
(a1,a2,a3)
VALUES
(1,2,3),
(4,5,6),
(7,8,9);
you can do this in Oracle
by using procedure you can insert
create procedure insert_query
as
begin
insert into tbl_test1(a1,a2,a3) VALUES (1,2,3)
insert into tbl_test2 (b1,b2,b3) VALUES (1,2,3)
end
you can do that like coz
MySQL doesn't support multi-table insertion in a single INSERT statement.
INSERT INTO NAMES VALUES(...)
INSERT INTO PHONES VALUES(...)
here is a link of detail answer..
sql - insert into multiple tables in one query
I supposed to make an sql trigger for update that will only activate when the affected rows are greater than 0. Yet, Even though the affected row is 0 (or the new record is identical to the old record) it still fires the trigger. I want to check the number of affected rows, before i execute the insert query below. Thanks in advance! Sorry for my grammar!
DELIMITER $$
DROP TRIGGER /*!50032 IF EXISTS */ `dbase`.`table_update_trigger`$$
CREATE
/*!50017 DEFINER = 'root'#'localhost' */
TRIGGER `table_update_trigger` AFTER UPDATE ON `table`
FOR EACH ROW BEGIN
IF (##ROWCOUNT > 0) THEN
INSERT INTO
table_audit_trail
(column1,
column2,
column3,
column4,
column5,
column100)
VALUES
(old.column1,
old.column2,
old.column3,
old.column4,
old.column5,
old.column100);
END IF;
END;
There is no ##ROWCOUNT in MySQL.
You can read this post to find ount how to replace it.
But you dont need that. Your trigger is for each row so it will fire for every updated row. (But that doesnt mean that your rows have changed. Just that they were updated by some statement.)