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
Share on other sites

For start, there is no END to pair with BEGIN. Also before defining a trigger you most likely will have to change delimiter from ; to something else (this is covered in MySQL's manual AFAIR, but reminding you just in case)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.