简体   繁体   English

此Mysql数据库规范化表格和结构设计是否正确?

[英]Is This Mysql Database Normalization Form and Structure Design Correct?

I am trying to figure out what kind of normalization and structure to use for a database I am making. 我正在尝试找出要用于数据库的哪种规范化和结构。 It is going to be a list of properties(building number street addresses, street names, cities, states, zipcodes, unit numbers). 这将是一个属性列表(建筑物编号街道地址,街道名称,城市,州,邮政编码,单元号)。

From there, I was going to make a table with various info. 从那里,我将要制作一张包含各种信息的表格。 Then I was going to have a intermediate table to join all the information and make the record. 然后,我将有一个中间表来连接所有信息并进行记录。 As far as I can tell, just about every column will by multi valued except unit number. 据我所知,除单位编号外,几乎每一列都将具有多值。 So, I see the need for complete normalization: 因此,我认为需要完全规范化:

Table building_number
---------------------
building_number_id int primary key auto index not null
buildind_namber tinyint

Table city
--------------------
city_id building_number_id int primary key auto index not null
city_name varchar(30)

Table state
--------------------
state_id building_number_id int primary key auto index not null
state_name varchar(30)

Table zip
---------------------
zip_id building_number_id int primary key auto index not null
zip_name varchar(30)

Table building_name
---------------------
building_name_id int primary key auto index not null
building_name varchar(50)

Table owner
---------------------
owner_id int primary key auto index not null
owner_name varchar(30)


Table info
----------------------
info_id int primary key auto index not null
rent tinyint
condition varchar(10)
comment varchar(1000)

Intermediate table
--------------------------
building_number_id int 
street_id int 
city_id int 
state_id int 
building_name_id 
owner_id 
info_id 
(all these keys are foreign keys referencing their respected tables/primary keys)

I will be creating a html search text box which will take dynamic input and pull up queries based on whatever is give...complete exact address, street name, or building number street name city, etc. I haven't developed my search mysql algorithm yet. 我将创建一个html搜索文本框,它将接受动态输入并根据给出的内容进行查询...完整的确切地址,街道名称或建筑物编号街道名称城市等。我尚未开发搜索mysql算法呢。 I'm just at the beginning stage of creating my database. 我才刚刚开始创建数据库。

I will be using innodb engine and b-tree indexing. 我将使用innodb引擎和b树索引。 I will index every column except comment since I will be doing these dynamic input searches(like google). 我将为除注释之外的所有列编制索引,因为我将进行这些动态输入搜索(例如google)。

I am doing this for myself as a hobbyist. 我是作为业余爱好者为自己做的。 Because of this, I prefer to do this by hand from scratch rather than using some framework or plug ins. 因此,我更喜欢从头开始,而不是使用某些框架或插件。

For what I am doing, is this database design and normalization correct? 对于我正在做什么,此数据库设计和规范化是否正确?

When you're creating tables, you should be thinking first in terms of entities , and in general terms, an entity is a tangible thing. 创建表时,应该首先考虑实体 ,总的来说,实体是有形的。

Examples of tangible things are: buildings, owners, contacts, cities, countries, time zones. 有形事物的示例是:建筑物,所有者,联系人,城市,国家/地区,时区。

On the other hand, there are things that are not entities, but instead descriptors of entities. 另一方面,有些东西不是实体,而是实体的描述符。

Examples of descriptors are: height, weight, door number, and price. 描述符的示例包括:高度,重量,门号和价格。

Descriptors are generally attributes of entities. 描述符通常实体的属性 If it is not possible to enumerate all possible descriptors in advance, these should probably not be in a table. 如果不可能事先枚举所有可能的描述符,则这些描述符可能不在表中。

Cases where you would want a look-up table for a descriptor are generally where you're constrained in the types of values you can accept. 通常,您希望在描述符的查找表中遇到的情况是,您只能接受可以接受的值的类型。 For example, "shoe size" might seem open-ended, but maybe you only manufacture certain sizes, so a free-form input field is not practical. 例如,“鞋子尺码”似乎是开放式的,但也许您只制造某些尺码,所以自由格式的输入字段不切实际。 On the other hand, "height" is better stored as a value with a pre-defined set of units instead of having a look-up table of all possible heights. 另一方面,最好将“高度”存储为具有一组预定义单位的值,而不要使用所有可能高度的查找表。

In your case, you need an "address" entity with a number of fields that describe it. 在您的情况下,您需要一个“地址”实体,其中包含许多描述它的字段。 Things like "building number" should be a free-form input field. 诸如“建筑物编号”之类的内容应为自由格式的输入字段。 "Building A", "82 1/2", "107B", "3.7", "4/9" and "44-290" are all valid building numbers. “ A楼”,“ 82 1/2”,“ 107B”,“ 3.7”,“ 4/9”和“ 44-290”都是有效的楼号。 You should just accept a string. 您应该只接受一个字符串。

Likewise, street names are hardly a thing you can qualify. 同样,街道名称几乎不是您可以限定的东西。 Is "Green Way Street" the same as "Green Way St." “绿道街”与“绿道街”相同吗? or "Greenway St."? 或“格林威街”? Does it matter? 有关系吗? Probably not, as it's just a descriptor. 可能不是,因为它只是一个描述符。 You have no way of verifying these, and linking them together is almost impossible, there's way too much massaging required to get it to work on a large scale. 您无法验证这些方法,将它们链接在一起几乎是不可能的,要使其大规模工作需要太多的按摩。

Also keep in mind that some places need two, three, four, or even five lines of address information to identify a location. 还请记住,某些地方需要两,三,四甚至五行地址信息才能识别位置。 The United Kingdom is one of the worst offenders here, where a formal address will include all sorts of information. 英国是这里最严重的违法行为之一,正式地址将包括各种信息。

What you should probably do is design a table like "addresses" with fields: address1 , address2 , address3 , address4 , address5 , city , region , country , postal_code . 您可能应该做的是设计一个像“地址”这样的表,其中包含以下字段: address1address2address3address4address5cityregioncountrypostal_code With that you can cover most anything they'll throw at you. 这样一来,您可以覆盖他们将要扔给您的几乎所有东西。 Look at the kind of data Google Maps returns for examples. 以Google Maps返回的数据为例。

You seem to be hinting at some kind of one-to-many structure in your question, where an address could have multiple building names or numbers. 您似乎暗示了问题中的某种一对多结构,其中一个地址可以具有多个建筑物名称或编号。 Without some kind of sequence indicator, you'll have no way of knowing which of these associated records is first. 如果没有某种顺序指示器,您将无法知道这些关联记录中的哪一个是第一个。 That complicates things significantly. 这使事情变得非常复杂。

When worrying about normalization, start with the simplest thing that works, and fix any obvious mistakes. 在担心标准化时,请从最简单的方法开始,然后修复所有明显的错误。 Unless you have massive amounts of data to deal with, you can usually adjust your schema fairly easily if you haven't over-done it with normalization. 除非您有大量数据要处理,否则如果没有通过规范化过度完成架构,通常可以相当轻松地调整架构。

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

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