سؤال

I am new to SQL and I have a question regarding table relationships. 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. 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. 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 مع الإسناد
لا تنتمي إلى StackOverflow
scroll top