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.)
Related
I would like my website's users to submit data with my HTML form. However, I want each user to be limited to 5 submissions total. To do this, I'm using a MySQL DML trigger to limit my column "um_id" to only five rows per id. But, I know my syntax is wrong and I'm asking anyone here to help correct it so that it could actually work.
Right now I'm testing this in PHPMyAdmin and it will eventually be placed in my PHP file. So um_id will have a value and be concatenated with this SQL Trigger.
CREATE TRIGGER ml_trigger
BEFORE INSERT
ON ml_character;
BEGIN
IF ( SELECT . 'um_id' . COUNT(*) > 5 ) THEN ROLLBACK;
END IF;
Something like this:
DELIMITER $$
CREATE TRIGGER ml_trigger_bi
BEFORE INSERT
ON ml_character
FOR EACH ROW
BEGIN
-- get count of existing rows with same um_id
DECLARE li_cnt BIGINT;
SELECT COUNT(*) AS cnt
FROM ml_character t
WHERE t.um_id = NEW.um_id
INTO li_cnt ;
-- if already five rows, throw error
IF li_cnt >= 5 THEN
SIGNAL '45000' SET MESSAGE_TEXT = 'Already five rows in ml_character';
END IF;
END$$
DELIMITER ;
The value of um_id to be checked will come from the row being inserted; we reference that value as NEW.um_id.
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 am trying to use a trigger from MySQL with these conditions. And I am really a new to use MySQL.
My Steps:
1) Update before changes on table A
2) I have 2 fields in these table A: FieldToSum (with 1 or 0 in many rows) and SUM (that have to be updated, querying the sum of FieldToSum data.
3) My code doesn´t work:
DELIMITER $$
CREATE TRIGGER before_sums
before update ON tableA
BEGIN
UPDATE tableA SET SUM=select sum(FieldToSum) from tableA;
END
$$
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;
I want to put a timestamp when a specific column is updated.
For example:
column1: a value
dateColumn1: date column1 was updated
column2 : a value
dateColumn2: date column2 was updated
I use the function getTimestamp(), but it doesn't seem to work.
Can anyone advise me on how to do this in PHP and MYSQL?
Thanks.
If you want to do this only in the database, you could write a trigger that checks your conditions and updates specific timestamps if needed. But I'm assuming you don't want to fiddle around with triggers. Triggers have an advantage though: you can access the old and the new values of a row without having to write any php code.
Anyway, in case you need it here is some example code for a trigger (SQL, beware):
DELIMITER $$
DROP TRIGGER IF EXISTS Table1_UpdateTrigger $$
CREATE TRIGGER Table1_UpdateTrigger BEFORE UPDATE ON Table1
FOR EACH ROW BEGIN
IF OLD.column1 != NEW.column1 THEN
SET NEW.dateColumn1 = NOW();
END IF;
IF OLD.column2 != NEW.column2 THEN
SET NEW.dateColumn2 = NOW();
END IF;
END;
$$
DELIMITER ;
Substite Table1 with your real table names, column1, etc. with real column names.
The other way is to compare the old and the new values in php. E.g. do a query fetching the old data, compare the fields you want to check, and then do one update query per field that has changed to set the new timestamps.
UPDATE table
SET column1='new value', timestampcolumn=NOW()
WHERE ...
is one way. If you don't mind the timestamp changing anytime anything in the record is updated, then use the native "timestamp" field type, which'll update itself to "now" when the record's inserted or changed.
I prefer using the MySQL function NOW(), like so:
UPDATE table1 SET column2 = value, dateColumn2 = NOW() WHERE somethingsomething
Use a conditional statement. For example in the following trigger the 'password changed time' will be updated only when there is change in password column.
CREATE TRIGGER update_password
BEFORE UPDATE ON users
FOR EACH ROW
BEGIN
IF OLD.password <> NEW.password THEN
SET NEW.password_changed_on = NOW();
END IF;
END //