Foundations › SQL › Foundations: Databases
️ Foundations: DatabasesThe Relational Model: Tables, Rows, Columns and Keys
In a relational database data is split across linked tables instead of one giant table. Each table represents one thing: customers, orders, products.
Parts of a table
| Term | Meaning | Example |
|---|---|---|
| Table | A set of rows of the same kind | customers |
| Row / Record | One customer with all details | id=1, Ahmed Ali, Cairo |
| Column / Field | One property for every row | name or city |
| Data type | The kind of values a column allows | number, text, date |
Here is the products table: each row is a product, each column a property. Open in SQL Lab
Primary key
A column (or more) whose value is unique, never repeated and never empty, identifying a row exactly. In customers it is id, like a person's national ID.
Foreign key
A column in one table pointing to the primary key of another. In orders, customer_id points to customers.id, so we know which customer each order belongs to.
Notice customer_id: a number pointing to a customer in the customers table. Open in SQL Lab
Why split the data?
If we wrote the customer's name and city in every order we would repeat the same data thousands of times, and if the city changed we would edit thousands of rows and might miss some. Splitting stores each fact once.
For advanced readers Schema and constraints
The schema is the database design: tables, columns, types and constraints (PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK) that protect data from errors before they get in.
❓ In the orders table, customer_id is a…
It is a foreign key because it points to id in customers.
❓ Can a primary key value repeat in a table?
A primary key is unique by definition because it identifies the row.
- A table represents one thing, a row is a record, a column a property.
- A primary key identifies a row; a foreign key links to another table.
- Splitting prevents repetition and makes edits easier.
Want an expert to do it for you?
Gomaa Intelligence turns your data into clear reports and decisions. Tell us your problem and we will reply.