简体   繁体   English

关系数据库设计

[英]Relational Database Design

I am making an online gathering system and I am trying to design a database. 我正在制作一个在线收集系统,并且正在尝试设计一个数据库。 I currently have four tables (Household, Household Forms, Household Members, Household Members form). 我目前有四个表(“家庭”,“家庭表格”,“家庭成员”,“家庭成员”表格)。 Basically what I need the database to do is capture the information from the end client that fills out a form, update the information about that household, and store the previous forms. 基本上,我需要数据库要做的是从填写表单的最终客户端捕获信息,更新有关该家庭的信息,并存储以前的表单。 The issue I am having a difficult time wrapping my head around is regarding to the household members and its respective forms. 我难以解决的问题是关于家庭成员及其各自形式的问题。 Since I do not have control over how many individual household members will be added, I am at a lost as how to create a database that updates and archives the individual household member forms. 由于我无法控制要添加多少个单个家庭成员,因此,我对如何创建一个更新和存档单个家庭成员表格的数据库一无所知。

Here what I am considering: 这是我正在考虑的:

HOUSEHOLD table 家用

  • UserID ( Primary Key ) 用户ID(主键)
  • Household ID 家庭ID
  • Head of Household 户主
  • Number of household members 家庭成员人数
  • Phone Number 电话号码

HOUSEHOLD FORM table 家用表格

  • UID ( Auto Incremented) UID(自动递增)
  • Household ID (Should be same as household table) 住户编号(必须与住户表格相同)
  • Head of Household 户主
  • Number of household members 家庭成员人数
  • Phone Number 电话号码

Right now, I have a UserID that is unique to the household. 现在,我有一个家庭唯一的UserID。 Whenever this UserID is used I know this is either the first time this household is inputting information or they are updating their information. 每当使用此UserID时,我都知道这是该家庭第一次输入信息,或者他们正在更新其信息。 The Household ID is what I use to form the relationship between the other tables.Both Household and household form should have the same household ID so I know that the forms are coming from that particularly household. 住户编号是我用来形成其他表格之间关系的方式。住户表格和住户表格都应具有相同的住户编号,因此我知道表格来自该特定住户。

Additionally I need information regarding each household member. 另外,我需要有关每个家庭成员的信息。

Household Members 家庭成员

  • UID (Auto Increments) UID(自动增量)
  • Household ID (Same as Household table) 家庭ID(与家庭表相同)
  • Household Member ID 家庭成员编号
  • First Name 名字
  • Last Name
  • Date of Birth 出生日期
  • Relationship to Head of Household 与户主的关系

Household Members Forms 家庭成员表格

  • UID (Auto Increments) UID(自动增量)
  • Household Members ID ( Same as household members) 家庭成员编号(与家庭成员相同)
  • First Name 名字
  • Last Name
  • Date of Birth 出生日期
  • Relationship to Head of Household 与户主的关系

What I trying to do here is make the database store the individual household member information as entities in the household members table and whenever a new form is added, the household member will be updated and the previous form will saved in the household members forms. 我在这里要做的是使数据库将各个家庭成员信息作为实体存储在家庭成员表中,并且每当添加新表单时,家庭成员都会被更新,而先前的表单将保存在家庭成员表单中。 The household ID in Household members essentially tells me which household each member belongs to. 本质上,家庭成员中的家庭ID告诉我每个成员属于哪个家庭。 The household member ID essentially lets me observe all the forms filled out from that particular individual. 从本质上讲,家庭成员ID使我可以观察到从该特定个人填写的所有表格。

Again, still really new to this and any information will be greatly appreciated! 再次,这仍然是真正的新事物,任何信息将不胜感激!

It looks like your only unique information in your Household Form table is the phone number and the number of members in the household. 看来您的住户表格中唯一的唯一信息是电话号码和住户成员数。 If that is true, I would consider getting rid of that table completely. 如果是这样,我将考虑完全摆脱该表。

For the phone number in the Household Form table, you should put it in the Household table because it appears to be information that belongs only to the household: it looks like there is only one per household and the number appears to be more associated with the household than any individual in the household. 对于“住户表格”表中的电话号码,应将其放在“住户表格”中,因为它似乎是仅属于该住户的信息:看起来每个住户只有一个,并且该号码似乎与比家庭中的任何个人都要多。

I would not have a number of members in the household field at all. 我根本不会在家庭领域有很多成员。 This number is what is known as a "derived" or "computed" value, meaning you get it from other information that is already in the database. 该数字就是所谓的“派生”或“计算”值,这意味着您可以从数据库中已经存在的其他信息中获取它。 You would use a SELECT COUNT statement along with a JOIN statement between the Household table and the Household members table. 您将在住户表和住户成员表之间使用SELECT COUNT语句和JOIN语句。 You can find lots of information on these SQL statements for whichever database you use with a Google search. 对于您与Google搜索一起使用的任何数据库,您都可以在这些SQL语句中找到很多信息。

I would also delete the Household Members Forms table. 我还将删除“家庭成员表格”表。 It appears to be just a subset of the Household Members table. 它似乎只是家庭成员表的一个子集。

I think what you were trying to accomplish with the Household Members Forms table and the Household Form table was to relate the Household and the Household Member table. 我认为您要使用“家庭成员表格”表和“家庭表格”表完成的工作是将“家庭”和“家庭成员表”关联起来。 When you use a table to do this, it is called a join table, but you usually only use join tables for "many-to-many" relationships. 使用表执行此操作时,该表称为联接表,但通常仅将联接表用于“多对多”关系。 In your case, a many-to-many relationship would be where a house can have many members in it AND where an individual can belong to several households. 在您的情况下,多对多关系就是一间房子可以有很多成员,而一个人可以属于几个家庭。

It looks like you are trying to model a "has many/belongs to" relationship. 似乎您正在尝试建立“有很多/属于”关系的模型。 In your case, that would be where a single household can have several members in it but any individual only ever belongs to one household. 在您的情况下,单个家庭可以有多个成员,但任何个人都只能属于一个家庭。 To link or relate the data in this relationship, all you need is to place a foreign key in the belongs to table. 要以这种关系链接或关联数据,您所需要做的就是将一个外键放置在belongs表中。 In your case, the belongs to table is the Household Members table and the foreign key would be the Household ID. 在您的情况下,“所属”表是“家庭成员”表,外键是“家庭ID”。

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

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