简体   繁体   English

SQL-表关系,主要是(PK / FK)

[英]SQL - Table Relationships, Primarily (PK/FK)

I am new to SQL and I have a question regarding table relationships. 我是SQL的新手,我对表关系有疑问。 Currently, I have the following tables in my database: 当前,我的数据库中有以下表格:

Person: 人:

Person_ID (PK)
Name
Company_ID(FK)
Phone
Email

Company: 公司:

Company_ID (PK)
Name
Address
City
State
Invoice_ID (FK)

Invoice: 发票:

Invoice_ID (PK)
Summary_ID (FK)
Detailed_ID (FK)

Summary_Section: 摘要部分:

Summary_ID (PK)
InvoiceNumber
Date
DueDate

Detailed_Section: 详细部分:

Detailed_ID (PK)
Person_ID (FK)
Amount
Info

Basically, each company has multiple people and receives one invoice. 基本上,每个公司都有多人,并且会收到一张发票​​。

Each invoice is broken into two sections: 每张发票分为两部分:

  • Summary 摘要
  • Detailed 详细

The summary section shows the company name, company address, invoice number, invoice date, & due date. 摘要部分显示公司名称,公司地址,发票编号,发票日期和到期日。

The detailed section shows the person's name, amount, & additional info. 详细部分显示此人的姓名,金额和其他信息。

Am I going about this all wrong? 我要解决所有这些错误吗? What would be the best way to go about this? 最好的方法是什么? Thank you! 谢谢!

EDIT: 编辑:

Concern: Am I assigning the foreign/primary keys correctly in this scenario? 关注:在这种情况下,我是否正确分配了外键/主键?

The PK/FK assignment seems to be ok. PK / FK分配似乎还可以。 Although I would suggest the following: 尽管我建议以下内容:

  1. If you are using any kind of framework, try to be consistent with its conventions. 如果您使用任何类型的框架,请尝试与其惯例保持一致。 For example, Ruby on Rails usually calls the primary key (if it is just one column) as id for all tables. 例如,Ruby on Rails通常将主键(如果只是一列)作为所有表的ID。 Note: column names across tables don't have to be unique. 注意:跨表的列名不必唯一。
  2. Do you really need to separate the summary and detailed sections in the DB? 您是否真的需要在数据库中将摘要部分和详细部分分开? You could combine the Invoice, Summary, and Detailed in one table, and then just retrieve/display the columns you need. 您可以将“发票”,“摘要”和“详细”合并在一个表中,然后仅检索/显示所需的列。 This is both a design and engineering issue. 这既是设计问题,也是工程问题。
    • Design: Can two invoices ever share a section? 设计:两个发票可以共享一个部分吗?
    • Engineering: How many times do you think the sections might be edited/retrieved? 工程:您认为这些节可以被编辑/检索多少次?

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

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