我可以根据另一个表外键更改来更新外键吗?

时间:2018-03-27 19:55:45

标签: mysql foreign-keys

create table `company_building` (
  id INT(11) NOT NULL,
  PRIMARY KEY (id)
);

create table `job` (
  id INT(11) NOT NULL,
  company_building_id INT(11) NOT NULL,
  PRIMARY KEY (id),
  FOREIGN KEY (company_building_id) REFERENCES company_building(id)
);

create table `cost_detail` (
  id INT(11) NOT NULL,
  company_building_id INT(11) DEFAULT NULL,
  PRIMARY KEY (id),
  CONSTRAINT `a_constraint` FOREIGN KEY (company_building_id) REFERENCES 
company_building_id(id) ON UPDATE SET NULL
);

说我有这种设置,我希望它在cost_detail.company_building_id更新时更新job.company_building_id

ON UPDATE CASCADE仅在company_building.id更改而非job.company_building_id时才有效。有没有办法使用触发器或者我不知道的一些外键魔法?

修改

详细说明我们有一个像

这样的结构
group1
   v
group2
   v
group3
   v
cost_detail < other group1 < other group2

并想象成本只是一笔金额。基本上我希望能够看到与group2相关的所有成本或与group1和其他group2相关的other_group1 OR成本相关的成本。

这样做我会写一些类似的东西 select * from cost_detail where group2_id = ? and other_group1_id = ?;而不是加入

0 个答案:

没有答案
相关问题