Database Terminology and Primary & Foreign Keys in SQL
Master Database Relationships and SQL Key Concepts
Don't get confused about the names of primary keys and foreign keys. Focus on understanding the underlying concepts and why these relationships matter for connecting database tables.
Key Database Concepts
Primary Keys
Unique identifiers that never change for each record. Like a permanent ID number that stays with a person regardless of name or email changes.
Foreign Keys
References to primary keys in other tables. They create connections between related data stored in separate tables.
Table Relationships
Connections between tables that allow you to join data together while maintaining efficient storage and avoiding duplication.
Email vs User ID for Unique Identification
| Feature | Email Address | User ID Number |
|---|---|---|
| Can Change | Yes | No |
| Can Be Reassigned | Yes | No |
| Permanent Identifier | No | Yes |
| Unique Forever | No | Yes |
Splitting Data Into Multiple Tables
How Primary and Foreign Keys Connect Tables
Primary Key Assignment
Each user gets a unique, permanent ID number in the users table that serves as the primary key
Foreign Key Reference
When an order is placed, the orders table stores the user's ID number as a foreign key reference
Table Connection
The foreign key in orders table matches the primary key in users table, creating the relationship
Data Retrieval
Queries can join both tables using the matching keys to retrieve complete information
Think of foreign keys like travelers in a foreign land - they refer back to their home country. Foreign keys in one table refer back to primary keys in another table.
Database Relationship Types
One-to-One Relationship
Each record in one table relates to exactly one record in another. Example: One person has one passport, one passport belongs to one person.
One-to-Many Relationship
One record relates to multiple records in another table. Example: One customer can place many orders, but each order belongs to one customer.
One-to-many relationships are the most common in databases because they reflect real-world scenarios like customers placing multiple orders or users creating multiple posts.
Working with ER Diagrams in dBeaver
dBeaver provides visual connections between related tables
Green highlighting shows which fields connect between tables
Small orange keys indicate primary key fields
P key indicates primary keys in the interface
Visual relationships help plan queries and joins
When somebody places an order, we say, hey, go look up the User ID. When somebody places an order in line items, line items are knowing which order they are associated with by the Order ID.
This lesson is a preview from our SQL Course Online (includes software) and SQL Certification Online (includes software & exam). Enroll in a course for detailed lessons, live instructor support, and project-based training.
Key Takeaways