Sub topic 4
Tables, Rows, Columns, and the Schema
Assume you already know how a spreadsheet stores values in cells and can read short SQL examples. This chapter uses a relational database model, where data is organized into tables and connections between tables are defined explicitly.
A table is a structured collection of data
A table represents one kind of thing, such as a customer or an order. A table has:
- Columns, which describe the properties each row may have.
- Rows, which represent individual records.
- A schema, which defines the tables, columns, data types, and rules in a database.
For example, a customers table might look like this:
| customer_id | name | |
|---|---|---|
| 1 | Ada Lovelace | [email protected] |
| 2 | Grace Hopper | [email protected] |
The first row is a header that names the columns. The remaining rows contain customer records. Every row follows the same column structure.
In relational database terminology, a table is sometimes called a relation, a row is called a tuple, and a column is called an attribute. These terms describe the same structures at a more formal level.
The schema defines the structure
A schema is the database's structural contract. It says what data a table accepts and how that data is organized. The schema can require an identifier, limit a value's type, or prevent a required value from being missing.
This SQL statement creates a table:
CREATE TABLE customers (
customer_id INTEGER PRIMARY KEY,
name VARCHAR(100) NOT NULL,
email VARCHAR(255) NOT NULL UNIQUE
);Here:
customer_idis an integer column used to identify each customer.PRIMARY KEYrequires each identifier to be unique and present.nameis text with a maximum length of 100 characters.NOT NULLmeans a value is required.UNIQUEprevents two rows from using the same email address.
The exact type rules vary between database systems. PostgreSQL, MySQL, SQLite, and SQL Server may interpret some types differently, but the central idea remains the same: the schema gives data a predictable shape.
Rows are records, not independent documents
A row is one record within a table. It should contain values about one instance of the table's subject. A row in customers describes one customer, while a row in orders describes one order.
This rule gives columns a consistent meaning. If a column sometimes contains a customer's name and sometimes contains several names separated by commas, the table is harder to query and validate. Prefer one value with one meaning in each column.
A value of NULL needs special care. NULL means that a value is unknown, unavailable, or not applicable. It does not mean zero, an empty string, or the text "NULL". Comparisons with NULL use IS NULL or IS NOT NULL, rather than = NULL.
Choosing columns
When designing a table, ask:
- What real-world thing does one row represent?
- What facts belong to that thing?
- Which values are required?
- Which values must be unique?
- Should a fact be stored in another table instead?
For example, storing several phone numbers in one phone_numbers column makes it difficult to search for one number or enforce a format. A separate table may be a better design when a customer can have many phone numbers.
Exercise
In the customers table, what is the difference between a row and the email column?
Show answer
A row represents one complete customer record. The email column represents one property of that customer and holds that property's value.