Table Design
Key points
- Table design is the work of deciding the shape of the tables that store your data.
- "Columns" define the data's fields, a "primary key" uniquely identifies each row, and "foreign keys" connect tables to one another.
- Poor design leads to duplicated or inconsistent data and slower searches.
What is a table?
A table is data organized into rows and columns. A single table holds one type of data together — for example, "customers" or "orders."
table
customer data
layout
Teacher Pochi's hintThink of a company's "customer ledger." One ledger is one table, and the format of the page you fill in corresponds to the column design.
Columns and data types
A column represents one field of data stored in a table. For each column, you decide a "data type" that specifies what kind of data it holds.
id: integerA numeric columnname: stringA string columncreated_at: datetimeA datetime columnFixing the data type in advance prevents bad data from slipping in — like text ending up in a column that should only hold numbers.
Teacher Pochi's hintThink of the blanks on a survey form. The "age" field only accepts numbers, and the "name" field only accepts letters — each blank has a fixed idea of what can be written in it.
Primary keys: uniquely identifying a record
A primary key is a column used to pinpoint a single row (record) within a table without any duplication. It guarantees that no two rows share the same value.
John Smith
Jane Doe
duplicated
Teacher Pochi's hintThis resembles a student ID number. Even if two students share the same name, checking the student ID number lets you identify exactly who's who.
Foreign keys: connecting tables together
A foreign key is a mechanism that links two tables together by storing one table's primary key value as a column in another table.
primary key: id
foreign key: customer_id
This lets the data correctly express "which customer this order belongs to," without having to copy the customer's name and address into the orders table every time.
Teacher Pochi's hintThis is similar to the "member number" printed on a courier's delivery slip. The slip itself doesn't need to spell out the full address — looking up the member number in the membership database gives you the correct delivery destination.
Relationships between tables: one-to-many
A relationship that comes up constantly in table design is the "one-to-many" relationship. Let's consider the example of one customer placing multiple orders.
orders
This relationship is expressed by giving the "many" side table (orders) a foreign key that holds the "one" side table's (customer) primary key. There are more complex relationships too, such as "many-to-many," but it's worth getting comfortable with one-to-many first.
Practical pitfalls to watch for
Summary
Table design is the work of using columns to define a table's fields, a primary key to uniquely identify each row, and foreign keys to connect tables to one another. If this foundation isn't solid, duplication and inconsistencies tend to creep in later.
Related topics: