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
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.
I have the following line of query in my stored procedure. The purpose of this stored procedure is if the website link is already in the table, it will update the existing one. If not it will record as new row from the other table' value.
CREATE DEFINER=`root`#`localhost` PROCEDURE `insert_diffmod_v2`(
IN para_diffmod LONGTEXT,
IN para_link LONGTEXT)
BEGIN
IF EXISTS (
SELECT website_link FROM diffmod WHERE website_link=para_link
)THEN
UPDATE diffmod
SET diffmod_content=para_diffmod
WHERE website_link= para_link;
ELSE
INSERT INTO diffmod(website_id,website_link)
SELECT id,link
FROM site_html
Where link=para_link;
UPDATE diffmod
SET diffmod_content= para_diffmod
where website_link = para_link;
END IF;
END
Let's say www.google.com is already recorded. When I call like:
CALL myDB.insert_diffmod_v2('test','www.google.com');
there is no problem and it do update the existing record.
But when I called the link which is not in the record (let's say Yahoo) like
CALL myDB.insert_diffmod_v2('test','www.yahoo.com');
it didn't insert as new record in the table. Can I know why ?
Insert in to on duplicate key update example
"$" are parameters and make website_id,website_link as unique
INSERT INTO diffmod SET
website_id = (SELECT id FROM site_html Where link=#para_link),
website_link= $para_link,
diffmod_content=$para_diffmod
ON DUPLICATE KEY UPDATE
diffmod_content=$para_diffmod
IDEA:
Having a table of item, user, assign now if I assign one item to user which the record will be save on table of assign,
table_item:
ID------INT
NAME----TEXT
COUNT---INT
table_user:
ID-------INT
NAME-----TEXT
table_assing:
ID------INT
USER----INT (user id)
ITEM----INT (item_id)
COUNT---INT (this is for subtractions from the column of COUNT table of item)
Here I want to set trigger on inserting to table (table_assing) the value of column COUNT should subtract from column of COUNT table of table_item
This is possible on PHP that I can set to query on once action but it will take lots of code if it's possible on MySQL that will be much better and fast and effective with accuracy
simple trigger after insert on table table_assign
UPDATE table_item
SET table_item.count = (table_item.count - NEW.table_assign.count)
WHERE table_item.id = table_assign.item
Something like this should work.
DELIMITER $$
USE database_name$$
CREATE TRIGGER trigger_name AFTER INSERT ON table_asign FOR EACH ROW
BEGIN
UPDATE table_item SET count=count+NEW.count WHERE id=NEW.id;
END;$$
The 'NEW.id' refers to the new row in the table 'table_asign'
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;