Objective
Create a relational structure for organizing photos into albums. This pattern enables hierarchical organization with albums containing multiple photos.
Step 1: Create Albums Table
Create a table to store album information.
CREATE TABLE albums (
album_id INTEGER PRIMARY KEY,
album_name VARCHAR(255) NOT NULL,
description TEXT,
cover_photo_id INTEGER,
is_public BOOLEAN DEFAULT FALSE,
created_by VARCHAR(100),
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
Step 2: Create Photos Table
Create a table for photos with album relationship.
CREATE TABLE photos (
photo_id INTEGER PRIMARY KEY,
album_id INTEGER,
title VARCHAR(255),
photo IMAGE(JPEG),
description TEXT,
taken_date DATE,
sort_order INTEGER DEFAULT 0,
is_favorite BOOLEAN DEFAULT FALSE,
view_count INTEGER DEFAULT 0,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (album_id) REFERENCES albums(album_id)
);
Step 3: Insert Sample Albums
Create sample albums for organization.
INSERT INTO albums (album_id, album_name, description, is_public, created_by) VALUES
(1, 'Summer Vacation 2024', 'Family trip to the beach', TRUE, 'john_doe'),
(2, 'Wedding Photography', 'Sarah and Mike wedding ceremony', FALSE, 'jane_photographer'),
(3, 'Nature Collection', 'Landscape and wildlife photography', TRUE, 'nature_lover'),
(4, 'City Architecture', 'Urban photography from around the world', TRUE, 'urban_explorer'),
(5, 'Personal Memories', 'Private family moments', FALSE, 'john_doe');
Step 4: Insert Photos into Albums
Add photos to the albums.
INSERT INTO photos (photo_id, album_id, title, description, taken_date, sort_order, is_favorite) VALUES
-- Summer Vacation album
(1, 1, 'Beach Sunset', 'Golden hour at the beach', '2024-07-15', 1, TRUE),
(2, 1, 'Family Picnic', 'Lunch by the shore', '2024-07-15', 2, FALSE),
(3, 1, 'Kids Playing', 'Building sandcastles', '2024-07-16', 3, TRUE),
(4, 1, 'Ocean View', 'Morning view from hotel', '2024-07-17', 4, FALSE),
-- Wedding Photography album
(5, 2, 'First Dance', 'Bride and groom first dance', '2024-06-20', 1, TRUE),
(6, 2, 'Ceremony', 'Exchange of vows', '2024-06-20', 2, TRUE),
(7, 2, 'Group Photo', 'All guests together', '2024-06-20', 3, FALSE),
-- Nature Collection album
(8, 3, 'Mountain Peak', 'Summit view at sunrise', '2024-03-10', 1, TRUE),
(9, 3, 'Deer in Forest', 'Wildlife encounter', '2024-04-05', 2, TRUE),
(10, 3, 'Waterfall', 'Cascading falls', '2024-05-12', 3, FALSE);
Step 5: Update Album Cover Photos
Set cover photos for albums.
UPDATE albums SET cover_photo_id = 1 WHERE album_id = 1;
UPDATE albums SET cover_photo_id = 5 WHERE album_id = 2;
UPDATE albums SET cover_photo_id = 8 WHERE album_id = 3;