Monday, March 10, 2025

#8.2 SqLite CRUD - Advanced Topics

 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

  1. Create a teachers table.
  2. 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

  1. Create an index for the students' names.
  2. 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

  1. Begin a transaction to update multiple students.
  2. 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

  1. Create a new table for enrollments and use a JOIN to show which student is in which course.
  2. Use transactions to ensure enrollment updates succeed.
  3. 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

#11 @Output

  Understanding @Output() in Angular 19 The @Output() decorator in Angular 19 is used to send data from a child component to a parent co...