CSE 340: Web Backend Development

W02 Learning Activity: Database Review

Overview

In this activity, you will prepare for working with databases by reviewing foundational concepts and familiarizing yourself with database management systems (DBMS). This preparation will help you understand how to design, implement, and interact with databases effectively in future lessons.

Preparation Material

Review Database Concepts from ITM 111

This course assumes you are familiar with databases from the earlier course, ITM 111, "Introduction to Databases." If you need to review that material, you can access it here:

Why do we need databases?

In the context of web development, databases solve several critical problems:

Without a database, your Express applications would lose all their data whenever the server restarts. User accounts, application state, and any information entered by users would disappear, severely limiting what your applications can do.

Relational Databases

Relational databases organize data into tables with rows and columns, similar to spreadsheets. Each table represents a specific type of entity (like users, products, or orders), and the rows represent instances of that entity. The columns define the attributes or properties that each instance has.

What makes relational databases powerful is their ability to establish relationships between different tables. This allows you to model complex real-world scenarios where different entities interact with each other.

Key Concepts in Relational Databases

The following are some fundamental concepts that form the foundation of relational databases.

Tables, Rows, and Columns

A relational database stores data in tables (also called relations). Each table has a name and consists of rows and columns:

Here is a simple example of what a "users" table might look like:


| user_id | username  | email               | join_date  |
|---------|-----------|---------------------|------------|
| 1       | johndoe   | john@example.com    | 2023-01-15 |
| 2       | janedoe   | jane@example.com    | 2023-02-20 |
| 3       | bobsmith  | bob@example.com     | 2023-03-05 |
Primary Keys

A primary key is a column (or a combination of columns) that uniquely identifies each row in a table. In the example above, the "user_id" column serves as the primary key, ensuring that each user record can be uniquely identified.

Primary keys are essential because they:

Foreign Keys and Relationships

A foreign key is a column in one table that refers to the primary key of another table. This creates a relationship between the two tables, allowing you to model connections between different types of data. For example, if we have a "posts" table in a blog application, we might include a "user_id" column that references the "user_id" in the "users" table:


| post_id | title           | content                   | user_id |
|---------|-----------------|---------------------------|---------|
| 1       | First Post      | This is my first post!    | 1       |
| 2       | Hello World     | Hello to everyone!        | 2       |
| 3       | Database Intro  | Databases are important...| 1       |

In this example, the "user_id" column in the "posts" table is a foreign key that references the "user_id" primary key in the "users" table. This relationship links each post to the user who created it, allowing you to easily retrieve all posts by a specific user or identify the author of any post. When designing tables, it is common to define the foreign key either immediately after the primary key in the table definition or at the end of the column list. This helps clarify the structure and relationships within your database schema.

Relationships in Action

When modeling data for a web application, you will frequently need to express connections between different entities. For instance, in an e-commerce application, you might have customers who place orders for products. These relationships can be modeled with foreign keys connecting tables for customers, orders, and products.

Introduction to SQL

SQL (Structured Query Language) is the standard language for interacting with relational databases. It provides a set of commands for creating, modifying, querying, and managing data in a relational database system.

SQL was developed in the 1970s by IBM researchers and has since become the standard language for relational database management. It is designed to be both human-readable and powerful enough to handle complex data operations.

Basic SQL Operations

Here are some examples of common SQL commands that demonstrate these types of operations:

Creating a Table

The following SQL command creates a simple "users" table:


CREATE TABLE users (
    user_id INT PRIMARY KEY,
    username VARCHAR(50) NOT NULL,
    email VARCHAR(100) UNIQUE NOT NULL,
    join_date DATE
);

This command defines a table with four columns, specifying the data type for each column and adding constraints like PRIMARY KEY, NOT NULL, and UNIQUE to ensure data integrity. In this case, the "user_id" column is the primary key, which uniquely identifies each user, so there's no need to add a separate UNIQUE constraint. The "username" and "email" columns are marked as NOT NULL, meaning they must have a value when inserting a new record. The "email" column is also marked as UNIQUE, ensuring that no two users can have the same email address.

Inserting Data

To add data to a table, you use the INSERT command:


INSERT INTO users (user_id, username, email, join_date)
VALUES (1, 'johndoe', 'john@example.com', '2023-01-15');
Querying Data

The SELECT command retrieves data from one or more tables:


SELECT username, email
FROM users
WHERE join_date > '2023-01-01'
ORDER BY username;

This query selects the username and email of all users who joined after January 1, 2023, and sorts the results alphabetically by username.

Relationships Between Tables

Relational databases derive their power from the ability to establish relationships between tables. These relationships are implemented using foreign keys and help model real-world connections between different types of data.

One-to-One (1:1)

In a one-to-one relationship, one record in the first table is related to exactly one record in the second table.


        CREATE TABLE students (
            student_id SERIAL PRIMARY KEY,
            first_name VARCHAR(50) NOT NULL,
            last_name VARCHAR(50) NOT NULL
        );

        CREATE TABLE student_details (
            detail_id SERIAL PRIMARY KEY,
            student_id INTEGER UNIQUE,  -- Ensures one-to-one relationship
            address TEXT,
            phone VARCHAR(20),
            medical_notes TEXT,
            FOREIGN KEY (student_id) REFERENCES students (student_id)
        );
    

The UNIQUE constraint on student_id in the student_details table ensures that each student can have only one detail record.

One-to-Many (1:N)

In a one-to-many relationship, one record in the first table can be related to multiple records in the second table, but each record in the second table is related to only one record in the first table.


CREATE TABLE departments (
    department_id SERIAL PRIMARY KEY,
    department_name VARCHAR(100) NOT NULL
);

CREATE TABLE employees (
    employee_id SERIAL PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL,
    department_id INTEGER,
    FOREIGN KEY (department_id) REFERENCES departments (department_id)
);

In this example, one department can have many employees, but each employee belongs to only one department.

Many-to-Many (N:M)

In a many-to-many relationship, one record in the first table can be related to multiple records in the second table, and vice versa. This requires a junction table (also called a bridge or linking table).


CREATE TABLE students (
    student_id SERIAL PRIMARY KEY,
    first_name VARCHAR(50) NOT NULL,
    last_name VARCHAR(50) NOT NULL
);

CREATE TABLE courses (
    course_id SERIAL PRIMARY KEY,
    course_name VARCHAR(100) NOT NULL,
    credits INTEGER
);

-- Junction table for the many-to-many relationship
CREATE TABLE enrollments (
    student_id INTEGER,
    course_id INTEGER,
    enrollment_date DATE DEFAULT CURRENT_DATE,
    grade CHAR(2),
    PRIMARY KEY (student_id, course_id),  -- Composite primary key
    FOREIGN KEY (student_id) REFERENCES students (student_id),
    FOREIGN KEY (course_id) REFERENCES courses (course_id)
);

In this example, one student can enroll in many courses, one course can have many students, and the enrollments junction table resolves this many-to-many relationship by creating two one-to-many relationships.

PostgreSQL

While SQL is the standard language for relational databases, different database management systems (DBMS) implement SQL in slightly different ways, with varying features and optimizations. In this course, you will use PostgreSQL a popular, free, open source relational database management system.

PostgreSQL is often chosen for applications that require complex queries, data integrity, and scalability.

Activity Instructions

Practice database terminology by answering the following questions.

  1. What is a primary key and why is it important?
    Answer (click to expand)

    A primary key is a column (or set of columns) that uniquely identifies each row in a table. It ensures record uniqueness, enables efficient indexing, and is used to establish relationships with other tables.

  2. What is a foreign key and how does it support relationships?
    Answer (click to expand)

    A foreign key is a column in one table that references the primary key of another table. It enforces referential integrity and lets you join related data across tables (for example, linking posts to their author).

  3. Describe one-to-one, one-to-many, and many-to-many relationships and how to implement each in a relational database.
    Answer (click to expand)

    One-to-one: each row in A maps to one row in B (enforce with UNIQUE on the foreign key). One-to-many: one row in A maps to many rows in B (B has a foreign key to A). Many-to-many: use a junction table with foreign keys to both tables (often a composite primary key).

  4. Why do web applications typically need a database?
    Answer (click to expand)

    Databases provide data persistence across server restarts, enforce integrity rules, allow efficient querying, support concurrent access, and help scale applications as data and users grow.

  5. What is an index and how does it affect query performance?
    Answer (click to expand)

    An index is a data structure (commonly a B-tree) that allows the database to locate rows quickly without scanning the whole table. Indexes speed up reads but add overhead to inserts/updates and consume extra storage, so choose columns to index carefully.

  6. Explain the common types of SQL JOINs and when to use them.
    Answer (click to expand)

    INNER JOIN returns rows with matching keys in both tables. LEFT JOIN returns all rows from the left table and matching rows from the right (NULL when no match). RIGHT JOIN is the opposite. FULL OUTER JOIN returns rows when either side matches. Use the appropriate join to express the required relationship in your query.

Next Step

Complete the other Week 02 Learning Activities

After you have completed all the learning activities for this lesson, return to Canvas to submit a quiz.

Other Links: