开发者

mysql how to update a foreign key automatically

开发者 https://www.devze.com 2023-04-11 06:36 出处:网络
this is my tables CREATE TABLE IF NOT EXISTS `carslibrary` ( `CarID` int(10) unsigned NOT NULL AUTO_INCREMENT,

this is my tables

 CREATE TABLE IF NOT EXISTS `carslibrary` (
      `CarID` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `CarName` varchar(255) NOT NULL,
      PRIMARY KEY (`CarID`)
    ) 开发者_运维问答ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=4 ;

    CREATE TABLE IF NOT EXISTS `colorslibrary` (
      `ColorID` int(11) unsigned NOT NULL AUTO_INCREMENT,
      `ColorName` varchar(255) NOT NULL,
      PRIMARY KEY (`ColorID`)
    ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;


    CREATE TABLE IF NOT EXISTS `facerecord` (
      `carslibrary_ID` int(10) unsigned NOT NULL,
      `colorslibrary_ID` int(11) unsigned NOT NULL,
      KEY `carslibrary_ID` (`carslibrary_ID`),
      KEY `colorslibrary_ID` (`colorslibrary_ID`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1;

i noticed carslibrary_ID attribute inside facerecord table is not automatically updated when i add a car record inside carslibrary table, what should i do to be able to?


Firstly, you'll need to have a default value specified for the facerecord.colorslibrary_ID since you will not 'know' what it is when inserting into the carslibrary table. That said you could alter your DDL for the facerecord table to be:

CREATE TABLE `facerecord` (
`carslibrary_ID` int(10) unsigned NOT NULL,
`colorslibrary_ID` int(10) unsigned NOT NULL DEFAULT '0',
KEY `carslibrary_ID` (`carslibrary_ID`),
KEY `colorslibrary_ID` (`colorslibrary_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

I've also changed the datatype of the colorslibrary_ID column to match that of the colorslibrary.ColorID column in case you ever feel like setting up a foreign key between facerecord.colorslibrary_ID and colorslibrary.ColorID ;). For the sake of completeness you should insert a row into the colorslibrary table with a ColorID = 0. Hence:

insert into `colorslibrary` (ColorName) values ('unknown color');

update `colorslibrary` set ColorID = 0 where ColorName = 'unknown color';

Then you can go ahead and define your trigger to insert into the facerecord table:

delimiter $$

CREATE TRIGGER carslibrary_trigger
    AFTER insert ON carslibrary
    FOR EACH ROW
    BEGIN
      insert into facerecord (carslibrary_ID) values (new.CarID);
END$$

delimiter;

All new rows inserted into the facerecord table will then be inserted with a colorslibrary_ID that relates to the 'unknown color' colorslibrary.ColorName.You can then manually update the facerecord.colorslibrary_ID as and when you know it.

Good luck!

PS If you need to remove any existing AFTER insert triggers from the carslibrary table you can do so by firstly finding the existing triggers:

select trigger_name
from information_schema.triggers
where event_object_table = 'carslibrary'
and action_timing = 'AFTER'
and event_manipulation= 'INSERT';

Then take the name of the trigger returned by the above statement (lets say the string 'carslibrary_trigger' is returned) and run:

drop trigger carslibrary_trigger;

Then re-run the CREATE TRIGGER script.

Once a trigger is set up it will automatically perform the action you have specified when the trigger action you have specified occurs. In this case we are telling the database "after an insert happens into the carslibrary table automatically insert a row into the facerecord table using the CarID of the new carslibrary row to populate the facerecord.carslibrary_ID column". As with most things the best way is to try it! Once you have created the trigger manually insert a new row into the 'carslibrarytable. Now look at the data in thefacerecord` table - you should see a new row that has been inserted by the trigger firing.

It sounds like you would benefit from learning about triggers. I recommend the docs on the MySQL site because this answer is way longer than I first intended it to be!


You will need to use triggers. See http://dev.mysql.com/doc/refman/5.0/en/triggers.html

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号