0

我有以下表格;它将保存有关各种类型文章的信息。我需要一些帮助来为此提出适当的架构。

表格是:

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”触发器。

    • 问候
4

1 回答 1

1

您的数据库设计在这里引起了您的大部分问题。你的三个文章表,maths,news 和 other 都应该是同一个表,带有一个 type 列来区分不同的类型。然后直接设置一个引用表,其中包含文章表的两个外键,一个用于源文章,一个用于参考文章。

我通常在应用程序本身而不是在数据库层中管理引用完整性,以便您的所有业务逻辑都在一个地方。因此,如果您删除一篇文章,则应用程序本身应删除任何参考条目。

希望有帮助!

于 2011-01-20T00:11:55.510 回答