Comprehensive Laboratory Work Guide
Grade XII • Computer Science ⏱️ ~25 minSet up XAMPP and access MySQL/MariaDB:
cd C:\xampp\mysql\bin
mysql -u rootYou should see
MariaDB [(none)]> or mysql> prompt.
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.
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:
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
);
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;
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;
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).
FullName and Section for Grade 11 students.Challenge Set 2: Inventory Control
Table: Inventory with columns ItemID (int), ItemName (varchar), Price (int), StockQuantity (int).
StockQuantity less than 20.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;
What to Expect in the Assessment:
Key Points to Remember:
Practice Recommendations:
1. Which clause is mandatory in an UPDATE statement to prevent changing all rows?
2. What happens if you run DELETE FROM Books; without a WHERE clause?
3. Which statement is used to remove an entire table and all its data?
Answers:
1-C, 2-B, 3-C.