Lab 2: Data Manipulation Language (DML)
Welcome to Lab 2!
In this lab, we will focus on performing various DML operations – INSERT, UPDATE, DELETE, and SELECT on the customers, movies, and ticket_purchases tables.
Learning Objectives:
Understand the basics of Data Manipulation Language (DML). Perform INSERT, UPDATE, DELETE, and SELECT operations on database tables. Pre-requisites:
Completed Lab 1 or have the MovieTicketDB database and associated tables (customers, movies, ticket_purchases) set up. Have some sample data in the tables to perform operations on. Exercise 1: INSERT Statement
In this exercise, you will learn to insert records into the database tables.
Task 1.1: Insert a New Customer
Insert a new record into the customers table.
INSERT INTO customers (first_name, last_name, email)
VALUES ('Emily', 'Clark', 'emilyclark@email.com');
Task 1.2: Insert a New Movie
Insert a new record into the movies table.
sqlCopy code
INSERT INTO movies (title, genre, rating, release_date)
VALUES ('Avatar', 'Sci-Fi', 7.8, '2009-12-18');
Task 1.3: Insert a New Ticket Purchase
Insert a new record into the ticket_purchases table.
sqlCopy code
INSERT INTO ticket_purchases (customer_id, movie_id, purchase_date, show_time)
VALUES (4, 4, '2023-09-27', '21:00:00');
Exercise 2: UPDATE Statement
In this exercise, you will learn to update existing records in the database tables.
Task 2.1: Update a Customer's Email
Update the email address of a customer in the customers table.
sqlCopy code
UPDATE customers
SET email = 'newemail@email.com'
WHERE customer_id = 4;
Task 2.2: Update a Movie's Rating
Update the rating of a movie in the movies table.
sqlCopy code
UPDATE movies
SET rating = 8.5
WHERE movie_id = 4;
Exercise 3: DELETE Statement
In this exercise, you will learn to delete records from the database tables.
Task 3.1: Delete a Ticket Purchase
Delete a record from the ticket_purchases table.
sqlCopy code
DELETE FROM ticket_purchases
WHERE purchase_id = 3;
Exercise 4: SELECT Statement
In this exercise, you will learn to retrieve records from the database tables.
Task 4.1: Retrieve All Customers
Retrieve all records from the customers table.
sqlCopy code
SELECT * FROM customers;
Task 4.2: Retrieve Movies Released After 2000
Retrieve all movies released after the year 2000 from the movies table.
sqlCopy code
SELECT * FROM movies
WHERE release_date > '2000-01-01';
Task 4.3: Retrieve Ticket Purchases for a Specific Movie
Retrieve all ticket purchases for a specific movie from the ticket_purchases table.
sqlCopy code
SELECT * FROM ticket_purchases
WHERE movie_id = 4;
Lab Review
In this lab, you learned to insert, update, delete, and select records in a SQL database. Make sure to practice these commands further to solidify your understanding.
Takeaway:
Manipulating data is at the core of any database system. Understanding how to effectively insert, update, delete, and select data is crucial for working with databases. Continue practicing these operations, and consider exploring more complex queries and operations as you progress.