简体   繁体   English

这是此数据库设计的正确方法

[英]which is the correct way for this database design

I have four tables, namely 我有四个表,即

countries,states,cities,areas

which will be the best feasible solution for my database table 这将是我的数据库表的最佳可行解决方案

Method A : 方法A:

CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `states` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
`state_id` int(11) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `areas` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
`zipcode` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

or Method B : 方法B:

CREATE TABLE IF NOT EXISTS `countries` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `states` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `cities` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
`state_id` int(11) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `areas` (
`id` int(11) auto_increment NOT NULL,
`name` varchar(50) NOT NULL,
`zipcode` int(11) NOT NULL,
`city_id` int(11) NOT NULL,
`state_id` int(11) NOT NULL,
`country_id` int(11) NOT NULL,
PRIMARY KEY  (`id`),
UNIQUE(`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

Thank you.. 谢谢..

The first is less likely to have problems with synchronization. 第一种不太可能出现同步问题。

The second will offer better performance through denormalization. 第二种将通过非规范化提供更好的性能。

Possible related thread: What is a good way to denormalize a mysql database? 可能的相关线程: 什么是使mysql数据库非规范化的好方法?

The second version will lead to major headaches if mismatched data is entered. 如果输入的数据不匹配,则第二个版本将导致严重的麻烦。 Take the following sample data: 取得以下样本数据:

countries: Canada, USA
states: Saskatchewan, Michigan
cities: Saskatoon, Detroit
zipcode: 90210 (california)

insert into  area (...) ('Canada', 'Michigan', 'Saskatoon', 90210)

all individually valid, but the entire record is utterly wrong. 全部单独有效,但整个记录完全错误。 Yet, by your design, it's supposed to be valid. 但是,根据您的设计,它应该是有效的。

It probably depends on what queries you are going to run on those tables. 这可能取决于要在这些表上运行的查询。 In general, A is normalized whereas B is not (A will use less space). 通常,将A标准化,而将B标准化(A将使用较少的空间)。

I would start with Method A, but if it turns out performance requires the additional columns further down the chain, I'd add them only as needed. 我将从方法A开始,但是如果事实证明性能需要在链的更下游添加其他列,则仅根据需要添加它们。

Just be sure to make your _id columns indexes. 只要确保使_id列索引即可。

I prefer Method A at first glance, but without knowing specifics about what you want the relationships and constraints to be, it's impossible to say categorically that one is "better" than the other. 我乍看之下更喜欢方法A,但是在不了解您想要的关系和约束条件的具体细节的情况下,不可能一概而论地说一个比另一个更好。 Follow your application's functional requirements. 遵循应用程序的功能要求。

Congratulations on looking towards a normalised approach: it's nice to see! 恭喜您寻求标准化方法:很高兴看到!

I personally would choose the first one [Method A]. 我个人会选择第一个[方法A]。 If you know, for example, the city ID of an area, then you automatically know the state ID and the country ID. 例如,如果您知道某个地区的城市ID,那么您将自动知道州ID和国家ID。 While the second one may be a bit more convenient, you may run into issues down the line if say, a city moved to a different state. 虽然第二个可能更方便,但是如果说某个城市搬到另一个州,您可能会遇到很多问题。

It's always best to start with the normalized form. 最好总是从规范化形式开始。 I would only suggest Method B if you had your RDBMS automatically managing cached column updates. 如果您让RDBMS自动管理缓存的列更新,我只会建议方法B。 For example, if you mistakenly placed Los Angeles in Michigan, you would need to update multiple locations (unless you had triggers that would update cascading pieces of information in denormalized tables). 例如,如果您错误地将洛杉矶放置在密歇根州,则需要更新多个位置(除非您有触发器可以更新非规范化表中的级联信息)。 But without triggers, Method A is without a doubt the best form. 但是,如果没有触发器,方法A无疑是最佳形式。

This is assuming of course that your constraints match the ones implicitly dictated by common interpretation when viewing Method A's definition. 当然,这是假定您的约束与查看方法A的定义时由常见解释隐式规定的约束匹配。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM