Link to home
Start Free TrialLog in
Avatar of James Murrell
James MurrellFlag for United Kingdom of Great Britain and Northern Ireland

asked on

possible to record changes (trigger I think) msql

Hello i have a mysql database  see below and when some changes Driver1 or Driver2 or TeamName i need system to record in new database the old values - with SubmissionID being the link:  can anyone help me

CREATE TABLE `f1users` (
  `submissionID` bigint(20) NOT NULL AUTO_INCREMENT,
  `Email` varchar(255) DEFAULT NULL,
  `FullName` varchar(255) DEFAULT NULL,
  `Driver1` varchar(255) DEFAULT NULL,
  `Driver2` varchar(255) DEFAULT NULL,
  `Team1` varchar(255) DEFAULT NULL,
  `LewisEndofyearPosition` varchar(255) DEFAULT NULL,
  `Team2` varchar(255) DEFAULT NULL,
  `Total` varchar(255) DEFAULT NULL,
  `TeamName` varchar(255) DEFAULT NULL,
  `Password` varchar(255) DEFAULT NULL,
  `MyRaceId` varchar(255) NOT NULL,
  `League_owner` varchar(45) DEFAULT NULL,
  `time_updated` timestamp NOT NULL DEFAULT '2017-03-23 00:00:00',
  `updated_dt` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`submissionID`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=utf8;

Open in new window

Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

yes, that is perfectly possible, and you should start reading this page of the reference manual (you many need to switch to your version of mysql as needed):
https://dev.mysql.com/doc/refman/5.7/en/trigger-syntax.html
Avatar of James Murrell

ASKER

Thanks angelIII, i am restricted as using raspberry pi: but will start reading now,,,
ok i thought i followed it correctly but guess i did not as it did not work

Table
CREATE TABLE `F1database`.`f1userhistory` (
  `idf1userhistory` INT ,
  `f1TeamName` VARCHAR(145) NULL,
  `f1submissionID` VARCHAR(45) NULL,
  `f1driverName1` VARCHAR(145) NULL,
  `f1driverName2` VARCHAR(145) NULL,
  `DateStamp` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`idf1userhistory`));
USE `F1database`;

Open in new window


Trigger
DELIMITER $$
CREATE
    TRIGGER updatehistory AFTER Update
    ON f1users  
   FOR EACH ROW BEGIN
	
		IF updated THEN
			SET @f1TeamName =  TeamName;
			SET @f1submissionID =  submissionID;
			SET @f1driverName1 =  driver1;
			SET @f1driverName2 =  driver2;	
			SET @DateStamp =  DateStamp;
		END IF;
    
		INSERT INTO updatehistory (idf1userhistory, f1TeamName,f1submissionID,f1driverName1,f1driverName2,DateStamp) VALUES (idf1userhistory, @f1TeamName,$f1submissionID,$f1driverName1,$f1driverName2,CURDATE());
		
    END$$

Open in new window





when i did a update nothing was stored in table
Hi!

This should work.

ELIMITER $$
CREATE
    TRIGGER updatehistory AFTER Update
    ON f1users  
   FOR EACH ROW BEGIN
	
			SET @f1TeamName =  TeamName;
			SET @f1submissionID =  submissionID;
			SET @f1driverName1 =  driver1;
			SET @f1driverName2 =  driver2;	
			SET @DateStamp =  DateStamp;
    
		INSERT INTO updatehistory (f1TeamName,f1submissionID,f1driverName1,f1driverName2,DateStamp) VALUES ( @f1TeamName,$f1submissionID,$f1driverName1,$f1driverName2,CURDATE());
		
    END$$

Open in new window


The if statement is causing the values not to be set.
Also make sure that the column idf1userhistory is set to AUTO_INCREMENT
 `idf1userhistory` INT AUTO_INCREMENT 

Open in new window

then you can skip that column in the insert statement in the trigger. Othervice you would need to put a unique value in the variable.

Regards,
    Tomas Helgi
sorry had to travel with work...

bug thanks for code and explanation

i now get
Error Code: 1235. This version of MySQL doesn't yet support 'multiple triggers with the same action time and event for one table'
Hi!

You need to drop the existing trigger on your table before creating the new one.

Regards,
     Tomas Helgi
cool thanks but 99% they will not change team name: so should i remove it?
Hi!

The update trigger is only triggered on an update on your table and the history table gets a new record.
So select on the history table shows all changes done on the main table.
And only update on the main table changes the data not this trigger.

Regards,
   Tomas Helgi
When I update main table I get nothing in history table...
ASKER CERTIFIED SOLUTION
Avatar of Tomas Helgi Johannsson
Tomas Helgi Johannsson
Flag of Iceland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks for all your help: and explaining... helps me learn