简体   繁体   English

按条件从一列到不同表的两列的外键

[英]foreign key from one column to two columns of different table by condition

I have tree tables MySQL: 我有树表MySQL:

articles table contains: 文章表包含:

id int
title varchar(255)
...

news table contains: 新闻表包含:

id int
title varchar(255)
...

comments table contains: 评论表包含:

id int
content text
type tinyint(1)  //this column holds 0 if this comment for news and holds 1 for article
fid  int         // this column holds id of article or news 

how can I make foreign key from comments table to articles and news. 如何从评论表到文章和新闻使用外键。 I mean how can implement this in MySQL query: 我的意思是如何在MySQL查询中实现这一点:

if type=0 then
 FOREIGN KEY (fid) REFERENCES news(id)

if type=1 then
 FOREIGN KEY (fid) REFERENCES articles(id)

To have proper PK-FK relationships I'd suggest to have a superset table (lets call it posts ). 为了拥有适当的PK-FK关系,我建议使用一个超集表(我们称其为posts )。 In that case your schema might look like 在这种情况下,您的架构可能看起来像

CREATE TABLE posts 
(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  type TINYINT(1)
);

CREATE TABLE articles
(
  id INT NOT NULL,
  title VARCHAR (255),
  article_property VARCHAR(128),
  -- other article specific attributes
  CONSTRAINT FOREIGN KEY (id) REFERENCES posts (id)
);

CREATE TABLE news
(
  id INT NOT NULL,
  title VARCHAR (255),
  reporter VARCHAR(128),
  -- other news specific attributes
  CONSTRAINT FOREIGN KEY (id) REFERENCES posts (id)
);

CREATE TABLE comments
(
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  post_id INT NOT NULL,
  content TEXT,
  CONSTRAINT FOREIGN KEY (post_id) REFERENCES posts (id)  
);

To populate id s when inserting new articles and news you can make use of triggers 要在插入新文章和新闻时填充id ,可以使用触发器

DELIMITER $$
CREATE TRIGGER tg_article_insert 
BEFORE INSERT ON articles
FOR EACH ROW
BEGIN
  INSERT INTO posts (type) VALUES (1);
  SET NEW.id = LAST_INSERT_ID();
END$$

CREATE TRIGGER tg_news_insert 
BEFORE INSERT ON news
FOR EACH ROW
BEGIN
  INSERT INTO posts (type) VALUES (0);
  SET NEW.id = LAST_INSERT_ID();
END$$
DELIMITER ;

Here is SQLFiddle demo. 这是SQLFiddle演示。

You can't. 你不能 You can read about foreign key constraints here , and you will note that only one table is allowed. 您可以在此处阅读有关外键约束的信息 ,并且您会注意到只允许一个表。

One work-around is to have separate columns for the separate ids: 一种解决方法是为单独的ID提供单独的列:

id int
content text
type tinyint(1)   //this column holds 0 if this comment for news and holds 1 for article
articleid  int
newsid int
. . . 
foreign key (articleid) references articles(id)
foreign key (newsid) references news(id)

In fact, you can dispense with the type and add in a constraint (implemented by a trigger) that only one id can be populated at any given time. 实际上,您可以省去该type并添加一个约束(由触发器实现),该约束在任何给定时间只能填充一个ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 一个表中的两列是否可以具有另一表中同一列的外键? - Can two columns from one table have a foreign key to the same column in another table? 两个不同外键列的同一个表的外键关系 - Foreign key relationships with the same table for two different foreign key columns Mysql将表1中的同一列作为外键引用到表2中的两列 - Mysql referencing the same column from table 1 as foreign key to two columns in table two mysql-来自不同表的两个不同列的求和和求和没有外键 - mysql - summing and differencing two different columns from different table without foreign key MySQL:是否可以对两个不同的表列创建外键? - MySQL: Is it possible to make a foreign key to two different table-columns? 一个表中两列之间的外键关系 - Foreign Key relationship between two columns in one table 是否可以通过使用外键使用连接子句将两个表中的其他列名显示到一个表中 - Is it possible to display other column names from two table to one table by using join clause by using foreign key 来自同一表的MySql外键(两列与一个键相关) - MySql foreign key from the same table (2 columns are relevant to one key) JPA-表列引用作为两个不同表的外键 - JPA -Table column references as foreign key to two different tables 如何在一个表中使用两次列,以显示另一个表中两个外键列的关联值? - How do I use a column in one table twice, to show the associated values of two foreign key columns in another table?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM