

0 / 2 embers
0 / 3000 xp
click for more info
Complete a lesson to start your streak
click for more info
Difficulty: 5
click for more info
Not enough gems
Cost: 6 gems
1: Table Relationships
incomplete
2: One to Many
incomplete
3: Many to Many
incomplete
4: Database Normalization
incomplete
5: Normal Forms
incomplete
6: Primary Keys
incomplete
7: Functional Dependencies
incomplete
8: First Normal Form (1NF)
incomplete
9: Second Normal Form (2NF)
incomplete
10: Third Normal Form (3NF)
incomplete
11: Boyce-Codd Normal Form (BCNF)
incomplete
12: Normalization Review
incomplete
13: Query Practice – Users & Banks
incomplete
Back
ctrl+,
Next
ctrl+.
This lesson's interactive features are locked, please to keep using them
When talking about the relationships between tables, a one-to-many relationship is probably the most commonly used relationship.
A one-to-many relationship occurs when a single record in one table is related to potentially many records in another table.
The one → many relation only goes one way; a record in the second table cannot be related to multiple records in the first table!
A customers table and an orders table. Each customer has 0, 1, or many orders that they've placed.
A users table and a transactions table. Each user has 0, 1, or many transactions that they've taken part in.
CREATE TABLE customers (id INTEGER PRIMARY KEY, name TEXT NOT NULL);
CREATE TABLE orders (
id INTEGER PRIMARY KEY,
amount REAL NOT NULL,
customer_id INTEGER,
CONSTRAINT fk_customers FOREIGN KEY (customer_id) REFERENCES customers (id)
);It's important that we track which devices our users are using to log into CashPal for security purposes. A user can log in from potentially many devices (e.g., a phone, a tablet, and a laptop), but each specific device is owned by a single user.
Let's create a one-to-many relationship between users and their devices.
id: an integer primary key
mac_address: TEXT
type: TEXT
user_id: an integer foreign key to the id field of the users table