我有树表MySQL:
文章表包含:
id int
title varchar(255)
...
新闻表包含:
id int
title varchar(255)
...
评论表包含:
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
如何从评论表到文章和新闻制作外键。 我的意思是如何在MySQL查询中实现这一点:
if type=0 then
FOREIGN KEY (fid) REFERENCES news(id)
if type=1 then
FOREIGN KEY (fid) REFERENCES articles(id)
答案 0 :(得分:1)
为了拥有正确的PK-FK关系我建议有一个超集表(让我们称之为posts
)。在这种情况下,您的架构可能看起来像
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)
);
要在插入新文章和新闻时填充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 ;
这是 SQLFiddle 演示。
答案 1 :(得分:1)
你做不到。您可以阅读外键约束here,您会注意到只允许一个表。
一种解决方法是为单独的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)
实际上,您可以省去type
并添加一个约束(由触发器实现),在任何给定时间只能填充一个id。