【问题标题】:single or split tables to hold this records in database?单个或拆分表来保存数据库中的这些记录?
【发布时间】:2012-02-28 17:53:34
【问题描述】:

我一直在为如何将这些信息存储在表格中的设计决策而苦苦挣扎。

我有一个名为 property 的表,它保存着房地产的记录。

CREATE TABLE `property` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `addDate` datetime NOT NULL,
  `serial` varchar(30) NOT NULL,
  `title` varchar(100) NOT NULL,
  `description` text,
  `user_id` int(11) NOT NULL,
  `area_id` int(11) NOT NULL,
  `category_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE  KEY `serial` (`serial`),
  FOREIGN KEY (`user_id`) REFERENCES `user`(`id`),
  FOREIGN KEY (`area_id`) REFERENCES `area`(`id`),
  FOREIGN KEY (`category_id`) REFERENCES `category`(`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=utf8;

我必须在公司索引页面中显示网站所有者提供的 5 个特色属性和 5 个 hotDeal 属性。我无法决定是将记录保存在一个表中还是两个不同的表中。

例如。

CREATE TABLE `property_featured` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `addDate` datetime NOT NULL,
  `description` text DEFAULT NULL,
  `position` tinyint(1) DEFAULT '0',
  `property_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `position` (`position`,`property_id`),
  FOREIGN KEY (`property_id`) REFERENCES `property`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE `property_hotdeal` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `addDate` datetime NOT NULL,
  `description` text DEFAULT NULL,
  `position` tinyint(1) DEFAULT '0',
  `property_id` int(11) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `position` (`position`,`property_id`),
  FOREIGN KEY (`property_id`) REFERENCES `property`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

我应该采用哪种方法,单一的还是拆分的?

【问题讨论】:

    标签: mysql database database-design rdbms


    【解决方案1】:

    所有信息都完全相同,因此您应该使用带有附加列的单个表,随意调用它,如果只有两个可能的值,则将其设为 TINYINT(0 或 1),或者或许VARCHAR 将来会有更多的价值。

    【讨论】:

    • 事实上,两个不同的表可能会在保持id 列在两个表中的唯一性方面产生额外的麻烦(我假设你会想要这样做)。
    猜你喜欢
    • 2016-01-26
    • 2017-08-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多