Lab Activity: SQL Commands

Comprehensive Laboratory Work Guide

Grade XII • Computer Science ⏱️ ~25 min
Setup Environment Installation & Connection

Set up XAMPP and access MySQL/MariaDB:

  1. Install XAMPP: Download from Apache Friends and run the installer with default settings.
  2. Launch Services: Open XAMPP Control Panel. Click Start for both Apache and MySQL. Wait for them to turn green.
  3. Open Terminal: Launch CMD (Windows) or Terminal (Mac/Linux) and navigate to MySQL bin:
    cd C:\xampp\mysql\bin
  4. Login to MySQL: Connect as root user (no password):
    mysql -u root
    You should see MariaDB [(none)]> or mysql> prompt.
Objectives Learning Goals & Outcomes

By the end of this lab activity, you will be able to:

Real-World Application: These skills are essential for building dynamic websites, managing business data, and working with any application that requires persistent data storage.

Concept Overview Understanding SQL Fundamentals

What is SQL?

SQL (Structured Query Language) is a standard language for managing relational databases. It allows you to create, modify, and query data stored in database systems like MySQL, PostgreSQL, and SQL Server.

Key SQL Command Categories:

Common Mistakes to Avoid:

Best Practices:

Part 1 Database and Table Setup

Step 1.1: Create database named LibraryDB.

CREATE DATABASE LibraryDB;

Step 1.2: Verify and select database.

SHOW DATABASES;
USE LibraryDB;

Step 1.3: Create Books table with columns for book information.

CREATE TABLE Books (
    BookID int,
    Title varchar(255),
    Author varchar(255),
    PublishedYear int
);
Part 2 Data Manipulation (CRUD Operations)

Step 2.1: Insert data into the Books table:

INSERT INTO Books (BookID, Title, Author, PublishedYear)
VALUES (1, 'The Great Gatsby', 'F. Scott Fitzgerald', 1925);

Step 2.2: Read data from the table:

Query all data:

SELECT * FROM Books;

Query specific columns:

SELECT Title, Author FROM Books;

Step 2.3: Update data using WHERE clause:

UPDATE Books SET PublishedYear = 1926 WHERE BookID = 1;

Step 2.4: Delete data using WHERE clause:

DELETE FROM Books WHERE BookID = 1;
Part 3 Advanced Structure & Cleanup Operations

Step 3.1: Modify table structure by adding a Genre column:

ALTER TABLE Books ADD Genre varchar(100);

Step 3.2: Create index to speed up title searches:

CREATE INDEX idx_title ON Books(Title);

Step 3.3: Clean up by removing objects:

Drop index:

DROP INDEX idx_title ON Books;

Drop table:

DROP TABLE Books;

Drop database:

DROP DATABASE LibraryDB;
Challenge Practical Lab Tasks: CRUD Operations

Complete these challenge tasks in your MySQL terminal. Each requires all four CRUD operations.

Challenge Set 1: Student Enrollment

Table: Students with columns StudentID (int), FullName (varchar), Grade (int), Section (varchar).

  1. Insert: Add student 'Anil Sharma', Grade 11, Section 'A', StudentID 101.
  2. Select: Display FullName and Section for Grade 11 students.
  3. Update: Change Anil Sharma's section from 'A' to 'B'.
  4. Delete: Remove student with StudentID 101.

Challenge Set 2: Inventory Control

Table: Inventory with columns ItemID (int), ItemName (varchar), Price (int), StockQuantity (int).

  • Insert: Add 'Wireless Mouse', ItemID 5, Price 1200, Stock 15.
  • Select: List items with StockQuantity less than 20.
  • Update: Increase stock of ItemID 5 to 25.
  • Delete: Remove item with ItemID 5.
View Challenge Solutions

Solutions:

Set 1:

-- Insert
INSERT INTO Students (StudentID, FullName, Grade, Section) VALUES (101, 'Anil Sharma', 11, 'A');
-- Select
SELECT FullName, Section FROM Students WHERE Grade = 11;
-- Update
UPDATE Students SET Section = 'B' WHERE StudentID = 101;
-- Delete
DELETE FROM Students WHERE StudentID = 101;

Set 2:

-- Insert
INSERT INTO Inventory (ItemID, ItemName, Price, StockQuantity) VALUES (5, 'Wireless Mouse', 1200, 15);
-- Select
SELECT * FROM Inventory WHERE StockQuantity < 20;
-- Update
UPDATE Inventory SET StockQuantity = 25 WHERE ItemID = 5;
-- Delete
DELETE FROM Inventory WHERE ItemID = 5;
Assessment Prep Ready for the Interactive Assessment

What to Expect in the Assessment:

Key Points to Remember:

Practice Recommendations:

Quiz Challenge Quiz: Test Your SQL Skills

1. Which clause is mandatory in an UPDATE statement to prevent changing all rows?

  • A) SET
  • B) FROM
  • C) WHERE
  • D) GROUP BY

2. What happens if you run DELETE FROM Books; without a WHERE clause?

  • A) It deletes the table structure.
  • B) It deletes all records in the table.
  • C) It deletes only the first record.
  • D) The command fails.

3. Which statement is used to remove an entire table and all its data?

  • A) DELETE TABLE
  • B) REMOVE TABLE
  • C) DROP TABLE
  • D) TRUNCATE
Check Quiz Answers

Answers:

1-C, 2-B, 3-C.