[CC4057] Lab work - Week 4
December 1, 2023•530 words
1. Create a database named movie_rental.
create database movie_rental;
use movie_rental;
2. Create the following tables in the database

genres
create table genres (
genreID int primary key auto_increment,
name varchar(20) not null
);movies
create table movies (
movieID int primary key auto_increment,
name varchar(30) not null,
releaseDate date not null,
genre int not null,
pricePerDay int not null,
foreign key (genre) references genres(genreID)
);clients
create table clients(
clientID int primary key auto_increment,
firstName varchar(20) not null,
lastName varchar(20),
dob date,
phone varchar(20) not null
);rents
create table rents(
rentID int primary key,
movie int not null,
client int not null,
startDate date not null,
endDate date not null,
foreign key(movie) references movies(movieID),
foreign key(client) references clients(clientID)
);
3. Insert the following data into the tables


genres
insert into genres
(name)
values
("Action"), ("Comedy"), ("Horror"), ("Animation"), ("Musical");clients
insert into clients
(firstName, lastName, dob, phone)
values
("Mark", "Guiliana", "1989-09-12", "122-555-888"),
("Benny", "Greb", "1985-04-04", "545-862-745"),
("Mark", "Twain", "1990-07-22", "541-965-785"),
("Anna", "Ivanovna", "1994-09-12", "654-785-022"),
("Kate", "Bengio", "2000-12-01", "854-885-446"),
("Siraj", "Rawal", "1991-04-18", "854-889-886"),
("James", "Arthur", "1989-06-24", "654-852-962");movies
insert into movies
(name, releaseDate, genre, pricePerDay)
values
("Logan", "2017-12-05", 1 , 200),
("Star Wars", "1990-01-07", 1 , 150),
("Conjuring", "2015-06-04", 3 , 175),
("Grudge", "2012-11-03", 3 , 140),
("Mr Bean", "1994-05-12", 2 , 200),
("Kung Fu Panda", "2009-12-21", 2 , 160),
("Lion King", "1994-06-22", 4 , 250),
("August Rush", "2009-08-14", 5 , 160),
("School Of Rock", "1997-06-22", 5 , 300),
("Transformers", "2014-06-12", 1 , 275);rents
insert into rents
(1, 1, 3, "2017-02-12", "2017-02-17"),
(2, 2, 3, "2017-02-12", "2017-02-17"),
(3, 10, 1, "2017-02-19", "2017-02-22"),
(4, 4, 1, "2017-02-22", "2017-02-27"),
(5, 2, 2, "2017-02-26", "2017-03-01"),
(6, 3, 4, "2017-03-01", "2017-03-06"),
(7, 8, 5, "2017-03-12", "2017-03-15"),
(8, 4, 7, "2017-03-19", "2017-03-22"),
(9, 5, 6, "2017-03-21", "2017-03-25"),
(10, 9, 6, "2017-03-22", "2017-03-26");
4. Write SQL statements for doing the following:
a. Show all the details of the clients whose names begin with “m”.
select * from clients where firstName like "m%";
b. Show the lastName and dob of the clients whose last names end in “a” or ”o”.
select lastName, dob from clients where lastName like "%a" or lastName like "%o";
c. Show all the details of the movies which were released in the 90s.
select * from movies where year(releaseDate) between 1990 and 1999;
d. Show all the details of the clients who were born in September or April.
select * from clients where month(dob) in (9,4);
e. Show all the details of the Action and Horror movies.
select * from movies where genre in (1,3);
f. Show the average pricePerDay of the movies.
select avg(pricePerDay) from movies;