Computer Science – MYSQL
MySQL Practice Lab: CREATE DATABASE, SELECT, INSERT,
UPDATE
This lab will help you practice basic MySQL commands including CREATE DATABASE,
SELECT, INSERT, and UPDATE. The exercises are based on a sample 'School' database.
Step 1: Create Database
Command:
CREATE DATABASE School;
Step 2: Create Table
Inside the 'School' database, create a table named 'Students' with the following structure:
student_id INT PRIMARY KEY,
first_name VARCHAR(50),
last_name VARCHAR(50),
grade INT
Step 3: Insert Data
Insert at least 3 student records:
INSERT INTO Students VALUES (1, 'John', 'Doe', 10);
INSERT INTO Students VALUES (2, 'Jane', 'Smith', 11);
INSERT INTO Students VALUES (3, 'Ali', 'Khan', 9);
Step 4: Select Data
Retrieve all student records:
SELECT * FROM Students;
Retrieve students in grade 10:
SELECT * FROM Students WHERE grade = 10;
Step 5: Update Data
Increase the grade of student with ID 1 by 1:
UPDATE Students SET grade = grade + 1 WHERE student_id = 1;
Step 6: Practice Challenge
Add another table called 'Teachers' with fields: teacher_id (INT, PRIMARY KEY), first_name
(VARCHAR(50)), last_name (VARCHAR(50)), and subject (VARCHAR(50)). Insert 2 records
and display all teachers who teach 'Mathematics'.
Additional Practice Questions
6. Retrieve all students whose age is above 20.
7. Update the age of a student with a specific StudentID.
8. Delete a student from the database whose name matches a given value.
9. Add a new column 'Email' to the Students table and insert email addresses for existing
students.
10. Display all students ordered by their name in ascending order.