Relational Database Operations
Relational Database Concepts
Payroll systems, inventory trackers and online stores keep records in relational databases. The relational model stores data in tables linked by shared values and answers business questions through operations on those tables. Recognizing tables, keys and operations helps you predict query results and spot data errors.
Tables, rows and columns
A relational database organizes data about employees, departments, products or orders into tables. Formally, a table is called a relation. Each column holds one property, and each row describes one instance. Each cell holds a single value, such as one hire date rather than a list of dates.
Formal term | Everyday description | Meaning |
|---|---|---|
Relation | Table | A set of rows with the same columns |
Tuple | Row or record | One instance, such as one employee |
Attribute | Column or field | One property, such as a hire date |
Domain | Permitted set of values | The values an attribute is allowed to hold |
A domain can be narrower than its underlying data type. For example, a rating column may use integers, but its domain may allow only integers from 1 to 5.
Row order carries no meaning in the model. To obtain sorted rows, request an order in the query rather than relying on entry order.
Keys connect the tables
A primary key is a column, or combination of columns, whose value identifies exactly one row. No two rows may share a primary-key value, and no primary-key column may be null. A composite key uses multiple columns: for example, (StudentID, CourseCode, Term) could identify each course enrolment.
A foreign key is a column, or combination of columns, that references a primary key or another eligible unique key in the same or another table. Non-null foreign-key values must match the referenced key values; nulls may be permitted by the design. Foreign keys connect related records without copying all their details.
Here, EMPLOYEE.DeptID refers to DEPARTMENT.DeptID. Each department's name and location are stored once; employee rows store the department code. Renaming Marketing means changing one department cell.
The database management system enforces referential integrity: it rejects a non-null foreign-key value without a matching referenced key. If the design permits nulls, an employee awaiting assignment can have a null DeptID. The system also blocks, or handles by a stated rule, deletion of a department that employees still reference.
Three core operations
These operations take tables as input and produce a table, so the output of one can feed the next.
Operation | What it keeps | Example question |
|---|---|---|
Select (also called restrict) | Rows meeting a condition | Which employees are in department D2? |
Project | Chosen columns only | What are all the employees' names? |
Join | Combined rows satisfying a matching condition | What city does each employee work in? |
To find which employees work in Montreal, join EMPLOYEE to DEPARTMENT on DeptID, select rows where Location is Montreal, and project Name. The names returned belong to Ana Silva (E101) and Lee Wong (E103); the query returns only their names. In SQL, the language most databases use:
SELECT e.Name
FROM Employee AS e
JOIN Department AS d ON e.DeptID = d.DeptID
WHERE d.Location = 'Montreal';An inner join keeps only rows that find a match. In this DeptID join, an employee with a null DeptID and a department with no employees would be absent from the result. In formal relational algebra, projection removes duplicate rows; SQL keeps duplicates unless you write SELECT DISTINCT.
What to remember
- Tables contain rows describing instances and columns describing properties; a domain specifies permitted values.
- A primary key uniquely identifies each row, and its columns cannot be null.
- A foreign key uses one or more columns to reference a primary or other eligible unique key in the same or another table. Non-null values must match; nulls are allowed only when the design permits them.
- Select keeps qualifying rows, project keeps chosen columns, and join combines related rows. An inner join keeps only matches.
Example: Linking customers and orders
Gearbox Supply, a small online bicycle-parts wholesaler, keeps these two tables in its relational database. Totals are in Canadian dollars.
CUSTOMER
CustID | Name | Province |
|---|---|---|
C201 | Northside Cycles | ON |
C202 | Prairie Wheels | MB |
C203 | Lakeshore Bikes | ON |
C204 | Rideau Sports | ON |
ORDERS
OrderID | OrderDate | CustID | Total (CAD) |
|---|---|---|---|
5001 | 2026-03-02 | C202 | 240.00 |
5002 | 2026-03-02 | C201 | 85.50 |
5003 | 2026-03-05 | C204 | 410.00 |
5004 | 2026-03-07 | C202 | 62.00 |
5005 | 2026-03-09 | C201 | 175.25 |
a) Propose a suitable primary key for each table and a foreign-key constraint that links them. Explain why the link should use CustID rather than the customer's name.
A suitable design declares CustID as the primary key of CUSTOMER and OrderID as the primary key of ORDERS. Each primary key must be unique and non-null across all records. The sample values are consistent with these choices, but uniqueness in a sample alone does not establish a declared primary-key constraint.
Declare ORDERS.CustID as a foreign key referencing CUSTOMER.CustID. This constraint requires every non-null ORDERS.CustID value to match an existing CUSTOMER.CustID. Foreign-key values may repeat because one customer can place several orders; repetition itself does not establish a foreign key.
The link uses CustID because it can be assigned as a unique, stable identifier. Two businesses can share a trading name, and a customer that renames itself would otherwise force an edit to every past order. With the ID as the link, the name is stored once, in CUSTOMER.
b) The sales manager wants the order number and total of every order over 150 CAD. Name the operations needed, in order, and give the result.
Everything the manager asked for is in ORDERS, so no join is needed.
- Select the rows where Total > 150. Checking each total: ________ yes, ________ no, 410.00 yes, 62.00 no, 175.25 yes. Orders 5001, 5003 and 5005 remain.
- Project the OrderID and Total columns, dropping OrderDate and CustID.
The result has 3 rows and 2 columns, OrderID and Total (CAD). Its rows are:
- OrderID 5001; Total 240.00 CAD.
- OrderID 5003; Total 410.00 CAD.
- OrderID 5005; Total 175.25 CAD.
c) The manager now asks which Ontario customers have placed orders, with their order numbers. Name the operations and give the result. Then explain why Lakeshore Bikes does not appear even though it is an Ontario customer.
Province is stored in CUSTOMER and order numbers in ORDERS, so the two tables must be joined.
- Inner join CUSTOMER and ORDERS on CUSTOMER.CustID = ORDERS.CustID. Each order finds its one customer, giving ________ rows: 5001 Prairie Wheels MB, 5002 Northside Cycles ON, 5003 Rideau Sports ON, 5004 Prairie Wheels MB, 5005 Northside Cycles ON.
- Select the rows where Province = 'ON'. Orders 5002, 5003 and 5005 remain.
- Project Name and OrderID.
The result has 3 rows and 2 columns, Name and OrderID. Its rows are:
- Name Northside Cycles; OrderID 5002.
- Name Rideau Sports; OrderID 5003.
- Name Northside Cycles; OrderID 5005.
Northside Cycles appears twice because it placed two orders; the join produces one row per matching pair.
Lakeshore Bikes (C203) has no row in ORDERS with CustID C203, so the inner join finds no match for it and excludes it before the selection is applied. If the manager wanted every Ontario customer listed, including those with no orders, an outer join that keeps unmatched customers would be needed instead.
d) A clerk tries to enter order 5006 with CustID C209. No customer C209 exists. What does a database that enforces the foreign-key constraint proposed in part a) do, and how should the clerk fix the problem?
The foreign-key constraint requires every non-null CustID in ORDERS to match an existing CustID in CUSTOMER. C209 is non-null and matches nothing, so the database rejects the new row.
This protects the business. If the row were accepted, order 5006 would be an orphaned order: it would refer to a customer record that does not exist. An inner join to CUSTOMER would find no matching name or province, so the order would disappear from a report built using that inner join.
The clerk should first check whether the ID was mistyped and correct it to the intended existing customer's ID. If C209 is a genuinely new customer, the clerk adds that customer to CUSTOMER first and then enters the order.
A hardware retailer's PRODUCT table has the columns ProductID, Name, Category, UnitPrice and StockQty, and it holds 400 products. Each product has a unique Name value. A buyer asks for a list that shows only each product's name and stock quantity, for every product in the table.
Which relational operation produces this list?
A training centre maintains enrolment records in relational tables. Match each SQL statement to the action it performs when staff maintain these records.
Remove the row for a withdrawn enrolment.
Change the phone number in an existing student's row.
Add a row for a newly enrolled student.
UPDATE
DELETE
INSERT