update another table when updated mysql trigger - mysql

I have 2 tables, students and Math. I want to UPDATE existing data on Math.Last_name when I update the students.Last_Name using the ID I am updating in students.
Students table
CREATE TABLE `STUDENTS` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL AUTO_INCREMENT,
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
PRIMARY KEY (`LRN`)
)COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
AUTO_INCREMENT=123456789112;
MATH TABLE
CREATE TABLE `Math` (
`Date_Modified` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`LRN` BIGINT(12) NOT NULL,
`Last_Name` VARCHAR(50) NOT NULL,
`First_Name` VARCHAR(50) NOT NULL,
`Level` VARCHAR(3) NOT NULL,
`UT1` VARCHAR(3) NOT NULL,
`Q1` VARCHAR(50) NULL DEFAULT NULL,
`UT2` VARCHAR(50) NULL DEFAULT NULL,
`Q2` VARCHAR(50) NULL DEFAULT NULL,
`UT3` VARCHAR(50) NULL DEFAULT NULL,
`Q3` VARCHAR(50) NULL DEFAULT NULL,
`UT4` VARCHAR(50) NULL DEFAULT NULL,
`Q4` VARCHAR(50) NULL DEFAULT NULL,
`FINAL GRADE` VARCHAR(50) NULL DEFAULT NULL,
PRIMARY KEY (`LRN`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
ROW_FORMAT=COMPACT;
MY TRIGGER
CREATE TRIGGER `STUDENTS_after_update` AFTER UPDATE ON `STUDENTS` FOR EACH ROW BEGIN
UPDATE Math
SET Last_Name = NEW.Last_Name
WHERE LRN IN (SELECT LRN FROM Math where LRN = NEW.LRN);
END

You do not need a sub select in the update command, since for each row it will pick up the new LRN value and will update all of them in the MATH table where the new.LRN matches in the MATH table. Here how the trigger would look like
delimiter //
create trigger `STUDENTS_after_update` after update on `STUDENTS`
for each row
begin
update Math
set Last_Name = NEW.Last_Name
where LRN = NEW.LRN ;
end;//
delimiter ;
Note that I have added delimiter which is needed when you run them in the Mysql CLI, some other user interface like PHPMyadmin you need to select the delimiter from the user interface.

Related

MySQL - Trigger for updating same table after insert comparing the text

Need Help with MYSQL Trigger with IF TEXT contains condition
Here's what I'm trying to do on MYSQL:
When there's an update on the table (i,e) when the "status" is changed from 'open' to "closed", i want to update date_closed to current date.
Trigger with if condition {could not get it to work}
create TRIGGER update_close_date BEFORE UPDATE ON issues
FOR EACH ROW
BEGIN
if LOCATE('closed', NEW.status) !=0 THEN
SET new.date_closed = CURRENT_DATE;
end if;
END;
Table Structure
CREATE TABLE `issues` (
`id` int(10) NOT NULL,
`project_type` varchar(100) NOT NULL,
`assessment_name` varchar(150) NOT NULL,
`issue_title` varchar(200) NOT NULL,
`description` varchar(1000) DEFAULT NULL,
`severity` varchar(10) DEFAULT NULL,
`status` varchar(25) DEFAULT NULL,
`found_on` date DEFAULT NULL,
`target_date` date DEFAULT NULL,
`app_support_contact` varchar(100) DEFAULT NULL,
`manager` varchar(100) DEFAULT NULL,
`date_created` datetime DEFAULT NULL,
`date_updated` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
`date_closed` date DEFAULT NULL,
`reminder_count` int(5) DEFAULT 0,
`escalation_count` int(5) DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Update
Thanks for the support, figured out whats wrong, had to add Delimiter word :)
DELIMITER //
create TRIGGER update_close_date BEFORE UPDATE ON issues
FOR EACH ROW
BEGIN
if LOCATE('closed', NEW.status) !=0 THEN
SET new.date_closed = CURRENT_DATE;
end if;
END;//
DELIMITER ;

MYSQL Trigger Copy Entire Row while insert,update

I have two Table namely admin_user,admin_user_bak
the structure of the table admin_user is
CREATE TABLE IF NOT EXISTS `admin_user` (
`user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
and structure of table admin_user_bak have all the fields of admin_user and additionally a field bak_user_id . its auto increment id..
CREATE TABLE IF NOT EXISTS `admin_user_bak` (
`bak_user_id` int(11) NOT NULL AUTO_INCREMENT,
`user_id` int(11) NOT NULL,
`user_name` varchar(150) NOT NULL,
`name` varchar(150) NOT NULL,
`emailid` varchar(150) NOT NULL,
`password` varchar(150) NOT NULL,
`roll` varchar(50) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
`last_login` datetime NOT NULL,
`status` enum('active','inactive') NOT NULL,
PRIMARY KEY (`bak_user_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
here my trigger is
CREATE TRIGGER ins_admin_user BEFORE UPDATE ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( NEW.user_id, NEW.user_name, NEW.emailid, new.password, NEW.roll, NEW.created, NEW.modified, NEW.last_login, NEW.status);
END
my purpose is i want to back up all events. insert update delete of a particular record. not all record. i write for insertion. its not working
any idea.. thanks
i want a insertion trigger but you had written update on in your trigger so it's not work.
You use INSERT ON
CREATE TRIGGER ins_admin_user BEFORE INSERT ON admin_user
FOR EACH ROW
BEGIN
INSERT INTO admin_user_bak (user_id,user_name,name,emailid,password,roll,created,modified,last_login,status) VALUES ( old.user_id, old.user_name, old.emailid, old.password, old.roll, old.created, old.modified, old.last_login, old.status);
END

error in trigger creation in phpmyadmin

I created the very simple triger and I think syntex is also correct:
CREATE TRIGGER trig1 after INSERT ON urlcontent for each row
BEGIN
insert into userpost(userid,url,hash) values (userid,url,hash);
END;
gives error:
#1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 3
Structure of both table:
CREATE TABLE urlcontent (
userid text NOT NULL,
url varchar(255) NOT NULL,
`desc` varchar(2048) NOT NULL,
preview varchar(255) NOT NULL,
img_url varchar(128) NOT NULL,
title varchar(128) NOT NULL,
`hash` varchar(128) NOT NULL,
rate varchar(20) DEFAULT NULL,
`time` varchar(64) DEFAULT NULL,
sentiment varchar(32) DEFAULT NULL,
`subject` varchar(64) DEFAULT NULL,
PRIMARY KEY (`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
-- --------------------------------------------------------
--
-- Table structure for table 'userpost'
--
CREATE TABLE userpost (
userid varchar(40) NOT NULL DEFAULT '',
url varchar(255) DEFAULT NULL,
`desc` varchar(2048) DEFAULT NULL,
preview varchar(255) DEFAULT NULL,
img_url varchar(128) DEFAULT NULL,
title varchar(128) DEFAULT NULL,
`hash` varchar(128) NOT NULL DEFAULT '',
rate varchar(16) DEFAULT NULL,
`time` varchar(64) DEFAULT NULL,
pcount varchar(16) DEFAULT NULL,
ncount varchar(16) DEFAULT NULL,
isset varchar(16) DEFAULT NULL,
sentiment varchar(32) DEFAULT NULL,
`subject` varchar(64) DEFAULT NULL,
PRIMARY KEY (userid,`hash`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
hash and url are key in userpost table/
Structure of both table
Your specific error is likely due to the delimiter being defined as ;.
If you change the delimiter to | (in the box below the query editor) like in the following image it will work:
This helps the trigger to be created without stoping the query at the first ;.
This would work, but to insert the values from urlcontent into userpost you have to add the keywords NEW before the values. This tells the trigger that the values you want to insert in userpost are the ones that were just inserted into urlcontent:
CREATE TRIGGER trig1 AFTER INSERT ON urlcontent
FOR EACH ROW
BEGIN
insert into userpost(userid,url,hash) values (NEW.userid,NEW.url,NEW.hash);
END;
|

mysql TRIGGER update field after update

i have 2 table today_plan and kiln_master.
after pattern and itemno inserted to today_plan then need to select matching yeild from kiln_master table and update this value to yeild field in today_plan.
i have created a trigger
CREATE TRIGGER update_yeild AFTER INSERT ON today_plan
FOR EACH ROW UPDATE today_plan
SET yeild= (SELECT kiln_master.yeild from kiln_master,today_plan WHERE today_plan.itemno = kiln_master.item AND today_plan.pattern = kiln_master.pattern ) WHERE itemno=new.itemno AND pattern=new.pattern
what part is wrong with my code
today_plan
DROP TABLE IF EXISTS decsys.today_plan;
CREATE TABLE `today_plan` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`belt` varchar(25) NOT NULL,
`distant` varchar(25) NOT NULL,
`pjtno` varchar(15) DEFAULT NULL,
`pattern` varchar(25) NOT NULL,
`itemno` varchar(25) NOT NULL,
`pro_qty` varchar(25) NOT NULL,
`act_qty` varchar(25) NOT NULL,
`yeild` varchar(25) NOT NULL,
`remark` varchar(100) NOT NULL,
`shipment` varchar(15) NOT NULL,
`temp` varchar(15) NOT NULL,
`fire_date` date NOT NULL,
`kiln` varchar(10) DEFAULT NULL,
`kiln_plan` varchar(15) NOT NULL,
`kiln_act` varchar(15) NOT NULL,
`ins_date` date NOT NULL,
`ins_act` varchar(15) NOT NULL,
`plandate` date NOT NULL,
`ship_date` date NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=31 DEFAULT CHARSET=utf8;
kiln_master
DROP TABLE IF EXISTS decsys.kiln_master;
CREATE TABLE `kiln_master` (
`kid` int(7) NOT NULL,
`pattern` varchar(30) NOT NULL,
`item` varchar(30) NOT NULL,
`yeild` double NOT NULL,
`temp` int(6) NOT NULL,
`kiln` varchar(10) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
error
#1442 - Can't update table 'today_plan' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
thank you very much
You're trying to update the row after it has been inserted, isn't it better to do before so that it gets inserted with the correct values? That'd be something like;
CREATE TRIGGER update_yeild BEFORE INSERT ON today_plan
FOR EACH ROW
SET NEW.yeild = COALESCE((SELECT kiln_master.yeild
FROM kiln_master
WHERE NEW.itemno = kiln_master.item
AND NEW.pattern = kiln_master.pattern
LIMIT 1), 0);

Writing a trigger to update a value with a value from its own row

I have the following table..
CREATE TABLE `community_data_1` (
`id` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
`type` VARCHAR(1) NOT NULL,
`reply_to_id` BIGINT(20) UNSIGNED NULL DEFAULT NULL,
`subject` TEXT NOT NULL,
`post` MEDIUMTEXT NOT NULL,
`html` VARCHAR(1) NOT NULL DEFAULT '0',
`time_stamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`author_id` BIGINT(20) UNSIGNED NOT NULL,
`d_id` BIGINT(20) UNSIGNED NOT NULL,
PRIMARY KEY (`id`)
)
ENGINE=MyISAM
ROW_FORMAT=DEFAULT
Now whenever an insert is done, after the insert I need to check if the d_id value is set to zero, if it is then I need to update it to the same value as that of the id of that row. How do I do this?
delimiter |
CREATE TRIGGER fixValue BEFORE INSERT ON community_data_1
FOR EACH ROW BEGIN
IF NEW.d_id = 0 THEN
SET #NewId= (SELECT MAX(id)+1 FROM community_data_1);
SET NEW.d_id = NewId;
END IF;
END;
|
delimiter ;