Hereβs an SQLite tutorial expansion covering Joins, Indexing, and Transactions in four short modules (~30 minutes total). This will help students optimize queries, combine data from multiple tables, and ensure data integrity.
π― Goal of the Tutorial
- Understand Joins to combine multiple tables.
- Learn Indexing for faster queries.
- Use Transactions to maintain database integrity.
π Module 1: Understanding Joins (8 mins)
πΉ Why Use Joins?
Joins help combine data from multiple tables using common keys.
πΉ Creating Example Tables
Students Table
CREATE TABLE students (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
course_id INTEGER
);
Courses Table
CREATE TABLE courses (
id INTEGER PRIMARY KEY AUTOINCREMENT,
course_name TEXT NOT NULL
);
Inserting Data
INSERT INTO courses (course_name) VALUES ('Computer Science'), ('Mathematics');
INSERT INTO students (name, course_id) VALUES ('Alice', 1), ('Bob', 2);
πΉ Performing Joins
INNER JOIN (Most Common)
SELECT students.name, courses.course_name
FROM students
JOIN courses ON students.course_id = courses.id;
βοΈ Shows only students with a matching course.
LEFT JOIN (All Students, Even Without a Course)
SELECT students.name, courses.course_name
FROM students
LEFT JOIN courses ON students.course_id = courses.id;
βοΈ Includes students without a course (NULL values).
RIGHT JOIN (Not Supported in SQLite)
SQLite doesnβt support RIGHT JOIN, but we can swap table positions and use LEFT JOIN.
π Exercise 1
- Create a teachers table.
- Use a JOIN to show which teacher is assigned to which course.
π Module 2: Indexing for Performance (7 mins)
πΉ What is Indexing?
- Speeds up queries by reducing search time.
- Works like a book index.
πΉ Creating an Index
CREATE INDEX idx_students_course ON students(course_id);
βοΈ Speeds up searches involving course_id
.
πΉ Checking Execution Time
EXPLAIN QUERY PLAN
SELECT * FROM students WHERE course_id = 1;
βοΈ Shows whether the query is using an index.
πΉ Deleting an Index
DROP INDEX idx_students_course;
π Exercise 2
- Create an index for the students' names.
- Run a
SELECT
query with and without the index.
π Module 3: Transactions for Data Integrity (8 mins)
πΉ What is a Transaction?
- Ensures multiple operations succeed or fail together.
- Uses BEGIN, COMMIT, and ROLLBACK.
πΉ Example: Safe Bank Transfer
BEGIN TRANSACTION;
UPDATE students SET course_id = 2 WHERE name = 'Alice';
-- Simulate an error before committing
ROLLBACK;
SELECT * FROM students; -- Alice's course remains unchanged
βοΈ ROLLBACK prevents incomplete operations from saving.
πΉ COMMIT: Finalizing Transactions
BEGIN TRANSACTION;
UPDATE students SET course_id = 2 WHERE name = 'Alice';
COMMIT;
SELECT * FROM students; -- Alice's course is now updated
βοΈ COMMIT permanently saves the changes.
π Exercise 3
- Begin a transaction to update multiple students.
- Rollback before committing and check if data changes.
π Module 4: Combining Joins, Indexing, and Transactions (7 mins)
πΉ Complex Query Example
BEGIN TRANSACTION;
SELECT students.name, courses.course_name
FROM students
JOIN courses ON students.course_id = courses.id
WHERE students.name LIKE 'A%';
COMMIT;
βοΈ Finds students whose names start with 'A' and ensures query execution in a transaction.
πΉ Optimizing Queries
CREATE INDEX idx_courses_name ON courses(course_name);
βοΈ Speeds up searching for courses by name.
π Final Exercise
- Create a new table for enrollments and use a JOIN to show which student is in which course.
- Use transactions to ensure enrollment updates succeed.
- Optimize the query using an index.
π― Summary of the Tutorial
βοΈ Module 1: Joins (Combining tables)
βοΈ Module 2: Indexing (Faster queries)
βοΈ Module 3: Transactions (Data integrity)
βοΈ Module 4: Combining them in real-world queries
π Next Steps ( We Will Study later !)
- Try using foreign keys to enforce relationships.
- Experiment with nested joins and multi-table indexes.
- Learn about triggers for automated actions.
Would you like sample SQL scripts for practice? π
No comments:
Post a Comment