我有以下表格;它将保存有关各种类型文章的信息。我需要一些帮助来为此提出适当的架构。
表格是:
CREATE TABLE IF NOT EXISTS `math_articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` char(250) NOT NULL,
`body` text,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
)
CREATE TABLE IF NOT EXISTS `news_articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` char(250) NOT NULL,
`body` text,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
)
CREATE TABLE IF NOT EXISTS `other_articles` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` char(250) NOT NULL,
`body` text,
PRIMARY KEY (`id`),
UNIQUE KEY `title` (`title`)
)
CREATE TABLE IF NOT EXISTS `references` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`article_from_table_name` text NOT NULL,
`from_id` int(11) NOT NULL,
`article_to_table_name` text NOT NULL,
`to_id` int(11) NOT NULL,
PRIMARY KEY (`id`)
)
插入测试数据:
INSERT INTO `TEST`.`math_articles` (
`id` ,
`title` ,
`body`
)
VALUES (
NULL , 'fibonacci sequences', 'fib sequences are: 0,1,1,2,3,5...also see article Leonardo of Pisa'
);
由于这个 math_articles.title = 'fibonacci sequences' 提到了文章 'Leonardo of Pisa' 我的程序将在 other_articles 表中插入以下数据:
INSERT INTO `TEST`.`other_articles` (
`id` ,
`title` ,
`body`
)
VALUES (
NULL , 'Leonardo of Pisa', 'Leonardo of Pisa also known as Leonardo of Pisa, Leonardo Pisano, Leonardo Bonacci, Leonardo Fibonacci, or, most commonly, simply Fibonacci, was.....'
);
关于表引用的架构问题
由于表 other_articles.title = 'Leonardo of Pisa' 在表 math_articles.title = 'fibonacci sequences' 中被引用,我将在引用表中保存此引用,如下所示:
不确定/问题插入参考表
INSERT INTO `TEST`.`references`
(`id`, `article_from_table_name`, `from_id`, `article_to_table_name`, `to_id`)
VALUES
(NULL, 'math_articles', '1', 'other_articles', '1');
保存这些参考的最佳方法是什么?
我对引用表架构的问题!
- article_from_table_name 和 article_to_table_name 两列的数据类型是文本,但它们是我数据库中的实际表。
- from_id 和 to_id 应该是它们各自表的外键 from_id = article_from_table_name.id 和 to_id = article_to_table_name.id 我不知道如何在模式中定义它。
如果我删除文章 math_articles.title = 'fibonacci sequences' 那么引用表也将被更新,我知道我应该使用某种“ON DELETE CASCADE”触发器。
- 问候