Skip to main content

Tables

Updated Aug 23, 2019 ·

Overview

Databases use tables to hold related data about specific subjects. Tables have rows (records) and columns (fields). While the fields are fixed when the database is created, the number of rows can increase indefinitely.

  • A record is a row in a table, containing data for an individual entry.
  • A field is a column in a table; holds one type of information for all entries.

Naming Practices

Best practices for naming tables:

  • Use lowercase letters and underscores instead of spaces
  • Prefer collective group names (e.g., "inventory")
  • Plural names are also acceptable (e.g., "products")

Best practices for naming fields:

  • Use lowercase and singular names, avoiding spaces

  • Ensure each field has a unique name and does not share its name with the table

  • Example: Use "card_num" and "name" instead of "card_nums" and "names"

    card_numnamemember_yeartotal_fine
    123456John202115.75
    234567Jane20205.00
    345678Alice20190.00
    456789Bob20227.50
    567890Charlie202112.30

Unique Identifiers

A unique identifier, or "key," is a unique value that distinguishes each record in a table. Often a number, this key is crucial for identifying records. In the patrons table below, the card_num field serves as the unique identifier, not the name field, since multiple patrons might share the same name.

card_numnamemember_yeartotal_fine
123456John Doe202115.75
234567Jane Smith20205.00
345678Alice Johnson20190.00
456789Bob Brown20227.50
567890Charlie Davis202112.30

Separating Data into Tables

Having more tables that is focused on a specific subject is better than a table combining multiple subjects. This approach helps maintain clarity and prevents confusion.

  • More tables with distinct subjects are preferable
  • Combining tables can cause duplicate information and non-unique values
  • Conencted tables can answer questions while keeping table topics separate

For instance, consider a patrons table like this:

card_numnamemember_yeartotal_fine
123456John Doe202115.75
234567Jane Smith20205.00
345678Alice Johnson20190.00
456789Bob Brown20227.50
567890Charlie Davis202112.30

A checkouts table:

idstart_datedue_datecard_numbook_id
100101-07-202115-07-2021123456AB1234Z
100205-07-202119-07-2021234567CD5678X
100310-07-202124-07-2021345678EF9101Y
100415-07-202129-07-2021456789GH2345W
100520-07-202103-08-2021567890IJ6789V

Since both tables are related, we can see information for each record clearly in both tables. As an example, we can find the year that Bob Brown started in the patrons table. If we want to see if he borrowed a book and when it is due, we can use his card number and check it on the checkouts table.

Now if we combine both tables into one, the data becomes less clear. Each row now includes information about both the patron and their checkouts, leading to duplicated patron information and non-unique card_num values. This combination can make it harder to maintain and query the data effectively.

card_numnamemember_yeartotal_fineidstart_datedue_datebook_id
123456John Doe202115.75100101-07-202115-07-2021AB1234Z
234567Jane Smith20205.00100205-07-202119-07-2021CD5678X
345678Alice Johnson20190.00100310-07-202124-07-2021EF9101Y
456789Bob Brown20227.50100415-07-202129-07-2021GH2345W
567890Charlie Davis202112.30100520-07-202103-08-2021IJ6789V
123456John Doe202115.75100601-08-202115-08-2021KL1122M
234567Jane Smith20205.00100705-08-202119-08-2021MN2233N