Mysql在两个表上多次触发

时间:2014-05-22 21:26:00

标签: mysql triggers delete-row error-code

我知道使用相同的触发时间和事件在两个触发器上创建触发器是不可能的,但是它有一个解决方法吗?我需要的是以下内容:

DELIMITER //
CREATE TRIGGER delete_char_ AFTER DELETE ON character_
FOR EACH ROW begin
DELETE FROM learned_skills
 WHERE learned_skills.Character_Name = old.Name_;
DELETE FROM learned_talents
 WHERE learned_talents.Character_Name = old.Name_;
END;
//
DELIMITER ;

来自表格:

CREATE TABLE character_ ( 
  Name_             varchar (30) NOT NULL,
  Class             varchar (30),
  World_Type        varchar (15),
  Str               integer     ,
  WS                integer     ,               
  BS                integer     ,
  Fel               integer     ,
  Per               integer     ,
  Int_              integer     ,
  Agi               integer     ,
  WP                integer     ,
  Tough             integer     ,  
  PRIMARY KEY (Name_)           ,
  FOREIGN KEY (Class) REFERENCES Class(Class_name),
  FOREIGN KEY (World_Type) REFERENCES World_Type(Name_)     );

  CREATE TABLE Skills (
  SkillName         varchar (30) NOT NULL,
  Type_             varchar (30),
  Characteristic    varchar (30),
  Descriptor        varchar (30),
  PRIMARY KEY (SkillName)   );

  CREATE TABLE Learned_Skills ( 
  Character_Name    varchar (30) NOT NULL,
  Skill_Name        varchar (40) NOT NULL,
  PRIMARY KEY (Character_Name,Skill_Name),
  FOREIGN KEY (Character_Name) REFERENCES character_(Name_),
  FOREIGN KEY (Skill_Name) REFERENCES Talents(TalentName)   );

CREATE TABLE Talents (
  TalentName        varchar (40) NOT NULL,
  Description       varchar (90),
  R_Str             integer     ,
  R_WS              integer     ,
  R_BS              integer     ,
  R_Fel             integer     ,
  R_Per             integer     ,
  R_Int             integer     ,
  R_Agi             integer     ,
  R_WP              integer     ,
  R_Tough           integer     ,
  Talent_required   varchar(40) ,
  Skill_required    varchar(40) ,
  Class_required    varchar(40) ,
  PRIMARY KEY (TalentName)      ,
  FOREIGN KEY (Talent_required) REFERENCES Talents(TalentName),
  FOREIGN KEY (Skill_required)  REFERENCES Skills(SkillName), 
  FOREIGN KEY (Class_required)  REFERENCES Class(Class_name)  );



 CREATE TABLE Learned_Talents ( 
  Character_Name    varchar (30) NOT NULL,
  Talent_Name       varchar (40) NOT NULL,
  PRIMARY KEY (Character_Name,Talent_Name),
  FOREIGN KEY (Character_Name) REFERENCES character_(Name_),
  FOREIGN KEY (Talent_Name) REFERENCES Talents(TalentName)  );

因此,当删除某个字符时,learned_skillslearned_talents都会被删除吗?

当我尝试做一个简单的事情时:

DELETE FROM character_ WHERE Name_='xxx';

我收到错误代码1451,我知道这是因为外键使用了character_的PK,所以我找到了解决方法:

SET foreign_key_checks = 0; 
DELETE FROM character_ WHERE Name_='xxx';
SET foreign_key_checks = 1;

但是还有一种方法,所以我不需要将键设置为0并返回1?还是完全可以接受?

0 个答案:

没有答案