2.3. Tables and relationships

In a relational database, data is stored in tables. Each table contains data about a particular type of entity (either physical or conceptual).

For instance, our sample database is the inventory and sales system for Acme Widget Co. It has tables containing data for the following entities:

Table 2-1. Acme Widget Co Tables

TableDescription
stock_itemInventory items
customerCustomer account details
salepersonSales people working for Acme Widget Co.
salesSales events which occur

Tables in a database contain fields and records. Each record describes one entity. Each field describes a single item of data for that entity. You can think of it like a spreadsheet, with the rows being the records and the columns being the fields, thus:

Table 2-2. Sample table

ID numberDescriptionPriceQuantity in stock
1widget$9.9512
2gadget$3.2720

Every table must have a \emph{primary key}, which is a field which uniquely identifies the record. In the example above, the Stock ID number is the primary key.

The following figures show the tables used in our database, along with their field names and primary keys (in bold type).

Table 2-3. the stock_item table

stock_item
id
description
price
quantity

Table 2-4. the customer table

customer
id
name
address
suburb
state
postcode

Table 2-5. the salesperson table

salesperson
id
name

Table 2-6. the sales table

sales
id
sale_date
salesperson_id
customer_id
stock_item_id
quantity
price