Session Overview

  • WHERE, ORDER BY and LIMIT
  • Relationships and Foreign Keys
  • Joins (Introduction)
  • Transactions
  • Stored Procedures
  • Blog Database Design

WHERE, ORDER BY and LIMIT

These clauses help us filter, sort and restrict records returned by a query.

WHERE Clause
SELECT *
FROM Student
WHERE City = 'Mumbai';
ORDER BY
SELECT *
FROM Student
ORDER BY Name;
ORDER BY DESC
SELECT *
FROM Student
ORDER BY Name DESC;
LIMIT
SELECT *
FROM Student
LIMIT 2;
Note: LIMIT is commonly used in pagination and dashboard reports.

Relationships and Foreign Keys

Relationships allow tables to be connected with each other.

Example

One Category can contain many Blog Posts.

CategoryId Name
1 Technology
2 Travel
PostId Title CategoryId
1 Django Basics 1
2 Goa Trip 2
Create Category Table
CREATE TABLE Category
(
    Id INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(100)
);
Create Post Table
CREATE TABLE Post
(
    Id INT PRIMARY KEY AUTO_INCREMENT,
    Title VARCHAR(200),
    CategoryId INT,
    FOREIGN KEY(CategoryId)
    REFERENCES Category(Id)
);
Important: Foreign Key creates a relationship between two tables.

Joins (Introduction)

Joins are used to retrieve data from multiple related tables.

Category Table
Id Name
1 Technology
2 Travel
Post Table
Id Title CategoryId
1 Django Basics 1
2 Goa Trip 2
INNER JOIN Example
SELECT
    Post.Title,
    Category.Name
FROM Post
INNER JOIN Category
ON Post.CategoryId = Category.Id;
Result
+---------------+------------+
| Title         | Name       |
+---------------+------------+
| Django Basics | Technology |
| Goa Trip      | Travel     |
+---------------+------------+
Note: INNER JOIN returns records that have matching values in both tables.

Transactions

A Transaction is a group of SQL statements executed as a single unit of work.

Transactions help maintain data consistency and prevent partial updates.

COMMIT

COMMIT permanently saves changes made during a transaction.

START TRANSACTION;

UPDATE Student
SET City = 'Pune'
WHERE Id = 1;

COMMIT;

ROLLBACK

ROLLBACK cancels all changes made during the current transaction.

START TRANSACTION;

UPDATE Student
SET City = 'Mumbai'
WHERE Id = 1;

ROLLBACK;

Bank Transfer Example
START TRANSACTION;

UPDATE Accounts
SET Balance = Balance - 1000
WHERE Id = 1;

UPDATE Accounts
SET Balance = Balance + 1000
WHERE Id = 2;

COMMIT;
Real World Usage: Transactions are commonly used in Banking, E-Commerce Orders, Payments and Inventory Systems.

Stored Procedures (Basics)

A Stored Procedure is a group of SQL statements stored inside the database and executed whenever required.

Advantages
  • Reduces code duplication.
  • Improves maintainability.
  • Improves performance for frequently executed queries.
  • Centralizes business logic inside the database.
Example
DELIMITER //

CREATE PROCEDURE GetStudents()
BEGIN
    SELECT * FROM Student;
END //

DELIMITER ;
Execute Stored Procedure
CALL GetStudents();
Result
All records from Student table will be displayed.
Note: In real applications, Stored Procedures are often used for reports, dashboards and complex business operations.

Blog Database Design

Before starting our Django Blog Project, let's understand the database design.

Tables Required
Table Purpose
Category Store Blog Categories
Post Store Blog Posts
Category Table
Category
--------------------------------
Id
Name
Post Table
Post
--------------------------------
Id
Title
Description
Image
CreatedDate
CategoryId
Relationship
One Category
      |
      |
      |
Many Posts
Table Creation Example
CREATE TABLE Category
(
    Id INT PRIMARY KEY AUTO_INCREMENT,
    Name VARCHAR(100) NOT NULL
);

CREATE TABLE Post
(
    Id INT PRIMARY KEY AUTO_INCREMENT,
    Title VARCHAR(200) NOT NULL,
    Description TEXT,
    Image VARCHAR(255),
    CreatedDate DATETIME DEFAULT CURRENT_TIMESTAMP,
    CategoryId INT,

    FOREIGN KEY(CategoryId)
    REFERENCES Category(Id)
);
Note: User Registration, Login and Comments will be implemented later using Django's built-in User model and application features.

Quick Revision

Filtering Data
  • WHERE
  • ORDER BY
  • LIMIT
Relationships
  • Connect Tables
  • Foreign Key
  • Data Integrity
Joins
  • Combine Tables
  • INNER JOIN
  • Related Data
Transactions
  • START TRANSACTION
  • COMMIT
  • ROLLBACK
Stored Procedures
  • Reusable SQL
  • Stored in Database
  • Executed using CALL
Blog Design
  • Category Table
  • Post Table
  • One-To-Many Relation

Assignment

Complete the following tasks in MySQL Workbench:
  1. Create a Category table and a Product table with a Foreign Key relationship.
  2. Insert at least 3 categories and 5 products into the database.
  3. Display products belonging to a specific category using the WHERE clause.
  4. Write an INNER JOIN query to display Product Name and Category Name together.
  5. Create a Stored Procedure named GetProducts that displays all records from the Product table.