简体   繁体   English

SQL表或字段

[英]SQL table OR field

I have a conceptual question. 我有一个概念性的问题。

My DB has a table that stores information about people. 我的数据库有一个表,用于存储有关人员的信息。 One of it's fields is their phone number (8-digit for my country). 其中一个字段是他们的电话号码(我的国家/地区是8位数字)。

The thing is that in some cases, two or more people will have the same phone number. 问题是,在某些情况下,两个或两个以上的人将拥有相同的电话号码。

My question is: will it be a better choise to store the phone numbers on another table and then reference it by a foreign key instead of just storing them as a field?If so, will the result be the same for whatever the size of the DB is? 我的问题是:将电话号码存储在另一个表上,然后通过外键引用而不是仅将其存储为字段,是否会更好?如果是这样,无论大小如何,结果都一样。 DB是?

I don't know if this will make any difference, but the table will have no more than 600.000 - 800.000 records, and I guess the coincident phone numbers will be about 10% of the total records. 我不知道这是否会有所作为,但是表格中的记录不会超过600.000-800.000,而且我猜想碰巧的电话号码大约占总记录的10%。

EDIT: 编辑:

-Each record can have up to 4 phone numbers (two lines and two cells) -每条记录最多可以有4个电话号码(两行和两个单元格)

-Both cases will occur, there will be sometimes where the users will be looking for all the people having a specific number, and times where the user will want to know what are all the phone numbers a person has -两种情况都会发生,有时用户会寻找所有具有特定号码的人,有时用户会想知道一个人拥有的所有电话号码是什么时候

If you have more then 1 phone number per person 如果您每人拥有一个以上的电话号码

There is a good reason to set new table like: id, user_id, phone, type, description 有充分的理由来设置新表,例如: id, user_id, phone, type, description

So type could be list of Home, Work, Office2, Boss, Wife, Fax, Mobile etc... 因此type可以是家庭,工作,Office2,老板,妻子,传真,手机等的列表。

and description like "work hours only", "evening", "24x7", "Emergency only" etc description例如“仅工作时间”,“晚上”,“ 24x7”,“仅紧急情况”等

If you really manage phone book for your application that is good idea to separate phone numbers from original user table. 如果您确实为应用程序管理电话簿,则最好将电话号码与原始用户表分开。

Technically to be normalized, you would have a separate Phone number table and then a PersonPhonenumber table. 从技术上进行标准化,您将有一个单独的电话号码表,然后是一个PersonPhonenumber表。

However, in practice I have rarely seen this structure for phone numbers and addresses. 但是,实际上,我很少看到电话号码和地址的这种结构。 For one, it is easy to make a mistake and update more than one person's addess or phone when you only mean to change one. 对于一个人来说,很容易犯一个错误,并且在您只想更改一个人的addess或电话时,要进行更新。 For another it adds an extra join that seems unnneeded to most people. 另一方面,它增加了大多数人似乎不需要的额外联接。 You aren't really gaining much by going to this level other than a small amount of duplication. 除了进行少量重复操作外,您进入此级别并不会真正获得任何收益。

The real decider is how you are going to use and update the numbers. 真正的决定者是您如何使用和更新号码。 If you want to update all the people with the same number frequently, it is better to go fully normalized. 如果要经常更新具有相同编号的所有人员,最好完全标准化。 If you will usually only want to update one person at a time, it is probably less risky to only have a Person table and a PersonPhone Number table. 如果您通常一次只想更新一个人,则仅拥有一个Person表和一个PersonPhone Number表可能没有那么大的风险。

If you want history, then I would go with a person table and aPersonPhoneNumber history table. 如果您想要历史记录,那么我将使用人员表和aPersonPhoneNumber历史记录表。 It would have the personid, the phone number, the startdate and the end date. 它将包含个人身份,电话号码,开始日期和结束日期。 So when John and Mary get divorced, his phonenumber woudl have an end date but hers woudl not and you could clearly see who had the number when. 因此,当约翰和玛丽离婚时,他的电话号码会有结束日期,而她的电话号码没有结束日期,您可以清楚地看到谁在何时获得了电话号码。

Usually the phone number is just a number, and without a person has no meaning. 通常电话号码只是一个数字,没有人没有任何意义。 So you store it in the Person table. 因此,您将其存储在Person表中。

But it you work for a telephone company for which a phone number has a different meaning and usage (like history, phone lookup, billing) then it should be stored in a separate table. 但是,如果您在一家电话公司工作,那么该电话公司的电话号码具有不同的含义和用途(例如历史记录,电话查询,帐单),则应将其存储在单独的表中。 I hope it helped. 希望对您有所帮助。

If you have two people with the same phone number you will encounter a problem when searching for a specific phone number. 如果两个人使用相同的电话号码,则在搜索特定的电话号码时会遇到问题。 A search for a specific phone number will sometimes return more than one result (10% according to your estimate). 搜索特定的电话号码有时会返回多个结果(根据您的估计为10%)。 If you search by phone number AND by people, you can require all searches for a phone number to include a user identifier (first name, last name, location, etc). 如果您按电话号码和按人员搜索,则可以要求所有对电话号码的搜索都包括用户标识符(名字,姓氏,位置等)。 It depends on what your objective is. 这取决于您的目标是什么。

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

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