Jump to content

MYSQL Trigger help


Canuckfan21

Recommended Posts

Hey guys just had a question about MySQL triggers.. I'm a newbie at MySQL and I'm trying to create a trigger that would remove an entry from one table, after data has been changed in another. If that makes sense..

 

 

Table 1:

 

CREATE TABLE IF NOT EXISTS `patient` (

  `p_person_id` int(11) NOT NULL auto_increment,

  `contact_date` date NOT NULL,

  `patient_type` char(1) NOT NULL,

  PRIMARY KEY  (`p_person_id`),

  KEY `patient_type` (`patient_type`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

 

 

Table 2:

 

 

 

CREATE TABLE IF NOT EXISTS `resident` (

  `rp_person_id` int(11) NOT NULL auto_increment,

  `bed_number` int(11) NOT NULL,

  `room_number` int(11) NOT NULL,

  `care_center_id` int(11) NOT NULL,

  PRIMARY KEY  (`rp_person_id`),

  KEY `bed_number` (`bed_number`,`room_number`,`care_center_id`)

) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=10 ;

 

 

So patient type is either R or O (Resident or Outpatient).

 

Whenever a Resident is changed to an outpatient, I would want the entry

of Resident to be deleted from the Resident table.

 

 

 

Here is my trigger:

 

CREATE TRIGGER `patient_trig` AFTER UPDATE

on `patient`

FOR EACH ROW BEGIN

DELETE FROM `resident` WHERE rp_person_id = NEW.p_person_id;

 

 

That won't work, thanks in advance for any help or attempts to solve the problem!

Link to comment
https://forums.phpfreaks.com/topic/153229-mysql-trigger-help/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.