简体   繁体   English

SQL表设计问题

[英]SQL Table Design Issue

So I am building out a set of tables in an existing database at the moment, and have run into a weird problem. 因此,目前我正在现有数据库中构建一组表,并且遇到了一个奇怪的问题。

First things first, the tables in question are called Organizations , Applications , and PostOrganizationsApplicants . 首先,有问题的表称为OrganizationsApplicationsPostOrganizationsApplicants

Organizations is a pre-existing table that is already populated with lots of data in regards to an organization's information which has been filled out in another form on another portal. Organizations是一个预先存在的表,其中已经填充了有关组织信息的大量数据,这些数据已在另一个门户网站上以另一种形式填写。 EDIT: I cannot edit this table. 编辑:我不能编辑此表。
Applications is a table that records all information that a user inputs in the application form of the website. 应用程序是记录用户以网站的应用程序形式输入的所有信息的表。 It is a new table. 这是一张新桌子。
PostOrganizationsApplicants is basically a copy of Organizations. PostOrganizationsApplicants基本上是组织的副本。 This is also a new table. 这也是一个新表。

The process goes: 流程如下:
1. Go to website and choose between two different web forms, Form A pertains to companies who are in the Organizations table, and Form B pertains to companies who are not in that table. 1.转到网站并在两个不同的Web表单之间进行选择, 表单A适用于“ 组织”表中的公司, 表单B适用于不在该表中的公司。
2a. 2a。 If Form A is chosen, a lot of the fields in the application will be auto-populated because of their previous submission. 如果选择了表格A,则应用程序中的许多字段将由于先前提交而被自动填充。
2b. 2b。 If Form B is chosen, the company has to start from scratch and fill out the entire application from scratch. 如果选择表格B,则公司必须从头开始,并从头开始填写整个应用程序。
3. Any Form B applicants must go into the PostOrganizationsApplicants table. 3.任何B表格申请人都必须进入PostOrganizationsApplicants表。

Now I am extremely new to SQL and Database Management so I may sound pretty stupid, but when I am linking the Organizations and PostOrganizationsApplicants tables to the Applications table, FK's for the OrganizationsID column and PostOrganizationsApplicantsID columns will have lots of empty spaces. 现在,我对SQL和数据库管理非常陌生 ,因此听起来有些愚蠢,但是当我将OrganizationsPostOrganizationsApplicants表链接到Applications表时,OrganizationsID列和PostOrganizationsApplicantsID列的FK将有很多空白。
Is this good practice? 这是好习惯吗? Is there a better way to structure my tables? 有没有更好的方法来构造我的表? I've been racking my brain over this and just can't figure out a better way. 我一直在为此绞尽脑汁,只是想不出更好的方法。

No, it's not necessarily bad practice to allow NULL values for foreign key columns. 不,允许外键列使用NULL值不一定是坏习惯。

If an instance of an entity doesn't have a relationship to an instance of another entity, then storing a NULL in the foreign key column is the normative practice. 如果一个实体的实例与另一个实体的实例没有关系,则将规范存储在外键列中为NULL。

From your description of the use case, a "Form A" Applications won't be associated with a row in Organizations or a row in PostOrganizationsApplicants . 根据对用例的描述,“ Form A” Applications不会与Organizations的行或PostOrganizationsApplicants的行PostOrganizationsApplicants


Getting the cardinality right is what is important. 重要的是获得正确的基数。 How many Organizations can a given Applications be related to? 给定的Applications可以与多少个Organizations相关? Zero? 零? One? 一? More than One? 超过一个? And vice versa. 反之亦然。

If the relationship is many-to-many, then the usual pattern is to introduce a third relationship table. 如果关系是多对多的,那么通常的模式是引入第三个关系表。

Less frequently, we will also implement a relationship table for very sparse relationships, when a relationship to another entity is an exception, rather than the rule. 当与另一个实体的关系是例外而不是规则时,我们也将为非常稀疏的关系实现关系表,这种情况很少出现。


I'm assuming that the OrganizationsID column you are referring to is in the PostOrganizationsApplicants table (which would mean that a PostOrganizationsApplicants can be associated with (at most) one Organizations . 我假设您要引用的OrganizationsID列位于PostOrganizationsApplicants表中(这意味着PostOrganizationsApplicants可以与(最多)一个Organizations相关联。

I'm also assuming that PostOrganizationsApplicantsID column is in the Applications table, which means an instance of Applications can be associated with at most one PostOrganizationsApplicants . 我还假设在Applications表中有PostOrganizationsApplicantsID列,这意味着Applications一个实例最多可以与一个PostOrganizationsApplicants

Bottomline, have a "zero-or-one to many" relationship is valid, as long as that supports a suitable representation of the business model. 底线是,具有“零或一对多”关系是有效的,只要该关系支持业务模型的适当表示即可。

Why not just add a column to the Organizations table that indicates that the Organization is a "Post" type of organization and set it for the Form B type of applicants? 为什么不只是在“组织”表中添加一列来表明该组织是“职位”类型的组织,并为B型申请者设置它呢? - then, all your orgs are in one table - with a single property to indicate where they came from. -然后,您的所有组织都在一个表中-具有一个属性来指示它们来自何处。

If you can add a new record to Organizations (I hope you can) just create FK from Organizations as PK of PostOrganizationsApplicants . 如果可以向组织添加新记录(希望您可以),则可以从组织创建FK作为PostOrganizationsApplicants的 PK。 So if Organizations has corresponding record in PostOrganizationsApplicants - it's "Post"! 因此,如果组织PostOrganizationsApplicants中具有相应的记录-这就是“发布”!

Thanks everybody, I think I found the most efficient way to do it inspired by all of your answers. 谢谢大家,我想我受您所有答案的启发找到了最有效的方法。

My solution below, in case anyone else has a similar problem... 我的解决方案是在其他人遇到类似问题的情况下...

Firstly I will make the PK of PostOrganizationsApplicants the FK of Organizations by making a "link" table. 首先,我将通过创建“链接”表将PostOrganizationsApplicants的PK设置为组织的FK。 Then I am going to add a column in PostOrganizationsApplicants which will take in a true/false value on whether they completed the form from the other portal or not. 然后,我将在PostOrganizationsApplicants中添加一列,以获取有关他们是否从其他门户网站完成表单的真/假值。 Then I will ask a question in the form whether they have already done the other version of the form or not. 然后,我将在表单中询问一个问题,他们是否已经完成了该表单的其他版本。 If the boolean value is true, then I will point those rows to the Organizations table to auto-populate the forms. 如果布尔值是true,则将这些行指向Organizations表以自动填充表单。

Thanks again! 再次感谢!

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

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