|
| 1 | +-- ======================================== |
| 2 | +-- CREATE TABLES |
| 3 | +-- ======================================== |
| 4 | + |
| 5 | +CREATE TABLE students ( |
| 6 | + student_id SERIAL PRIMARY KEY, |
| 7 | + first_name VARCHAR(50), |
| 8 | + last_name VARCHAR(50), |
| 9 | + birth_date DATE |
| 10 | +); |
| 11 | + |
| 12 | +CREATE TABLE classes ( |
| 13 | + class_id SERIAL PRIMARY KEY, |
| 14 | + name VARCHAR(100), |
| 15 | + teacher VARCHAR(100) |
| 16 | +); |
| 17 | + |
| 18 | +CREATE TABLE enrollments ( |
| 19 | + enrollment_id SERIAL PRIMARY KEY, |
| 20 | + student_id INTEGER REFERENCES students(student_id), |
| 21 | + class_id INTEGER REFERENCES classes(class_id), |
| 22 | + enrollment_date DATE DEFAULT CURRENT_DATE |
| 23 | +); |
| 24 | + |
| 25 | +-- ======================================== |
| 26 | +-- INSERT MOCK DATA |
| 27 | +-- ======================================== |
| 28 | + |
| 29 | +INSERT INTO students (first_name, last_name, birth_date) VALUES |
| 30 | +('Alice', 'Smith', '2005-04-10'), |
| 31 | +('Bob', 'Johnson', '2006-08-15'), |
| 32 | +('Charlie', 'Brown', '2004-12-01'); |
| 33 | + |
| 34 | +INSERT INTO classes (name, teacher) VALUES |
| 35 | +('Mathematics', 'Mrs. Taylor'), |
| 36 | +('History', 'Mr. Anderson'), |
| 37 | +('Science', 'Dr. Reynolds'); |
| 38 | + |
| 39 | +INSERT INTO enrollments (student_id, class_id) VALUES |
| 40 | +(1, 1), |
| 41 | +(1, 2), |
| 42 | +(2, 2), |
| 43 | +(3, 1), |
| 44 | +(3, 3); |
| 45 | + |
| 46 | +-- ======================================== |
| 47 | +-- SIMULATE DEAD TUPLES |
| 48 | +-- ======================================== |
| 49 | + |
| 50 | + |
| 51 | +INSERT INTO students (first_name, last_name, birth_date) |
| 52 | +SELECT 'John', 'Doe', CURRENT_DATE - (random() * 5000)::int |
| 53 | +FROM generate_series(1, 100000); |
| 54 | + |
| 55 | +-- These updates and deletes will create dead tuples |
| 56 | + |
| 57 | +-- Update records (old versions become dead) |
| 58 | +UPDATE students |
| 59 | +SET last_name = last_name || '_updated' |
| 60 | +WHERE student_id IN (1, 2); |
| 61 | + |
| 62 | +-- Delete records (deleted rows become dead) |
| 63 | +DELETE FROM enrollments |
| 64 | +WHERE enrollment_id IN (SELECT enrollment_id FROM enrollments LIMIT 2); |
| 65 | + |
| 66 | +-- Disable autovacuum temporarily (for demo) |
| 67 | +ALTER TABLE students SET (autovacuum_enabled = false); |
| 68 | +ALTER TABLE enrollments SET (autovacuum_enabled = false); |
| 69 | + |
| 70 | +-- ======================================== |
| 71 | +-- SELECT QUERIES |
| 72 | +-- ======================================== |
| 73 | + |
| 74 | +-- Get all students |
| 75 | +SELECT * FROM students; |
| 76 | + |
| 77 | +-- Get all students enrolled in Mathematics |
| 78 | +SELECT s.first_name, s.last_name |
| 79 | +FROM students s |
| 80 | +JOIN enrollments e ON s.student_id = e.student_id |
| 81 | +JOIN classes c ON e.class_id = c.class_id |
| 82 | +WHERE c.name = 'Mathematics'; |
| 83 | + |
| 84 | +-- Count students per class |
| 85 | +SELECT c.name, COUNT(e.student_id) AS student_count |
| 86 | +FROM classes c |
| 87 | +LEFT JOIN enrollments e ON c.class_id = e.class_id |
| 88 | +GROUP BY c.name; |
| 89 | + |
| 90 | +-- ======================================== |
| 91 | +-- UPDATE QUERIES |
| 92 | +-- ======================================== |
| 93 | + |
| 94 | +-- Change Bob's last name |
| 95 | +UPDATE students |
| 96 | +SET last_name = 'Williams' |
| 97 | +WHERE first_name = 'Bob' AND last_name = 'Johnson'; |
| 98 | + |
| 99 | +-- Update the teacher for the History class |
| 100 | +UPDATE classes |
| 101 | +SET teacher = 'Ms. Carter' |
| 102 | +WHERE name = 'History'; |
| 103 | + |
| 104 | +-- ======================================== |
| 105 | +-- DELETE QUERIES |
| 106 | +-- ======================================== |
| 107 | + |
| 108 | +-- Remove Charlie from Science class |
| 109 | +DELETE FROM enrollments |
| 110 | +WHERE student_id = (SELECT student_id FROM students WHERE first_name = 'Charlie') |
| 111 | + AND class_id = (SELECT class_id FROM classes WHERE name = 'Science'); |
| 112 | + |
| 113 | +-- Delete a student completely |
| 114 | +DELETE FROM students |
| 115 | +WHERE first_name = 'Alice' AND last_name = 'Smith'; |
0 commit comments