Tables & Views

Understanding the fundamental building blocks of relational databases is essential for mastering SQL. Tables and views are the core structures that organize and present your data in meaningful ways.

In this lesson, we'll explore how tables store data, how keys maintain relationships, and how views provide flexible ways to access information from multiple tables.

🗃️ What Are Tables?

Tables are the main structures in a relational database. Think of them as organized spreadsheets where each table represents a single, specific subject.

Key characteristics of tables:

  • Each table represents one specific subject (like customers, products, or orders)
  • The logical order of rows and columns is not important
  • Every table contains at least one primary key column
  • Data can exist independently of how it's physically stored

Tables can represent two types of subjects:

  • Objects - Tangible things like people, places, or products
  • Events - Actions or occurrences like sales, appointments, or transactions

📊 Table Components

🔢 Columns (Fields)

Columns define the type of data stored and represent specific characteristics of the table's subject.

Column characteristics:

  • Each column holds one specific type of data
  • Column names should clearly indicate what data they contain
  • Well-designed columns contain only one value per cell
  • Examples: FirstName, LastName, Email, OrderDate

📝 Rows (Records)

Rows represent individual entries or records in the table. Each row contains all the information about one specific instance of the table's subject.

Example: Books Table

ID  | Title                | Author              | Year
----|----------------------|---------------------|------
1   | The Great Gatsby     | F. Scott Fitzgerald | 1925
2   | 1984                 | George Orwell       | 1949
3   | To Kill a Mockingbird| Harper Lee          | 1960

In this example:

  • Each row represents a different book
  • Each column stores specific information about that book
  • The ID column serves as the primary key

🔑 Understanding Keys

Keys are special columns that play crucial roles in maintaining data integrity and relationships between tables.

🏷️ Primary Key

A primary key consists of one or more columns that uniquely identify each row in a table.

Primary key rules:

  • No two rows can have the same primary key value
  • Cannot be null - every row must have a primary key value
  • Should not change once assigned to a row

Example: Agents Table

AgentID | AgentFirstName | AgentLastName | DateHired  | AgentHomePhone
--------|----------------|---------------|------------|---------------
1       | William        | Thompson      | 15-May-01  | 555-2681
2       | Scott          | Bishop        | 10-Feb-03  | 555-2666
3       | Carol          | Viescas       | 09-Sep-00  | 555-2571

💡 Did You Know? When a primary key is made up of multiple columns, it's called a composite primary key. This is useful when no single column can uniquely identify each row.


🔗 Foreign Key

A foreign key is a column that refers to the primary key of another table, establishing relationships between tables.

Example: Entertainers Table

EntertainerID | AgentID (FK) | EntertainerName    | EntertainerPhone
--------------|--------------|--------------------|------------------
1001          | 1            | Carol Peacock Trio | 555-2691
1002          | 3            | Topazz             | 555-2591
1003          | 3            | JV & the Deep Six  | 555-2511

Why foreign keys matter:

  • Maintain relationships between tables
  • Ensure data integrity by preventing orphaned records
  • Enable complex queries across multiple tables
  • Enforce referential integrity in the database

👁️ What Are Views?

A view is like a virtual table that presents data from one or more tables in a specific way. Unlike regular tables, views don't store data themselves—they're saved queries that pull data from existing tables.

Benefits of views:

  • Simplify complex queries by presenting data in an easy-to-understand format
  • Security - Show only specific columns or rows to certain users
  • Reusability - Save commonly used queries for repeated access
  • Data consistency - Always show current data from underlying tables

🔨 Creating Views

Views are created using SQL queries that define which data to display and how to present it.

Example: Customer Engagements View

CREATE VIEW Customer_Engagements AS
SELECT EngagementNumber, CustFirstName, CustLastName, StartDate, EndDate
FROM Customers
JOIN Engagements ON Customers.CustomerID = Engagements.CustomerID;

This view would display:

EngagementNumber | CustFirstName | CustLastName | StartDate  | EndDate
-----------------|---------------|--------------|------------|------------
3                | Doris         | Hartwig      | 2016-09-10 | 2016-09-15
13               | Peter         | Brehm        | 2016-09-17 | 2016-09-20
14               | Doris         | Hartwig      | 2016-09-29 | 2016-09-29

💡 Key Point: Views automatically update when the underlying table data changes, so you always see current information.


✅ Key Takeaways

  • Tables are the foundation of relational databases, storing data in rows and columns
  • Primary keys uniquely identify each row and maintain table integrity
  • Foreign keys establish relationships between tables and ensure data consistency
  • Columns should be well-designed with clear names and single values
  • Views are virtual tables that present data from one or more tables without storing it
  • Views simplify complex queries and provide security by controlling data access
  • Understanding keys and views is crucial for effective database design and querying

🚀 What's Next?

Now that you understand the fundamental structures of databases, you're ready to start writing SQL queries to interact with tables and create views. In the next lessons, we'll dive into the SELECT statement and learn how to retrieve data from these structures effectively.

Ready to practice what you learned?

Test your SQL skills with our interactive coding challenges and exercises.