0% found this document useful (0 votes)
15 views

Week 4

The document shows the creation of a movie rental database with tables for genres, movies, clients, and rental information. Genres, movies, clients, and rental records are inserted into their respective tables. Some queries are run to select data from the tables.

Uploaded by

Shivam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Week 4

The document shows the creation of a movie rental database with tables for genres, movies, clients, and rental information. Genres, movies, clients, and rental records are inserted into their respective tables. Some queries are run to select data from the tables.

Uploaded by

Shivam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

# mysql -u root -h localhost

1.Create database movie_rental;


Use movie_rental
2.
a)
Create Table genres(genreID INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(20) NOT
NULL);

Describe genres;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| genreID | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(20) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+

Insert into genres(name) VALUES


-> ("Action"),
-> ("Comedy"),
-> ("Horror"),
-> ("Animation"),
-> ("Musical");

Select * from genres;


+---------+-----------+
| genreID | name |
+---------+-----------+
| 1 | Action |
| 2 | Comedy |
| 3 | Horror |
| 4 | Animation |
| 5 | Musical |
+---------+-----------+

b).
->Create Table movies(movieID INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(30) NOT
NULL, releaseDate DATE NOT NULL, genre INT, pricePerDay INT NOT NULL, FOREIGN
KEY(genre) REFERENCES genres(genreID));

desc movies;
+-------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+----------------+
| movieID | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(30) | NO | | NULL | |
| releaseDate | date | NO | | NULL | |
| genre | int(11) | YES | MUL | NULL | |
| pricePerDay | int(11) | NO | | NULL | |
+-------------+-------------+------+-----+---------+----------------+

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-06-14",5,160),
-> ("School Of Rock","1997-06-22",5,300),
-> ("Transformers","2014-06-12",1,275);

Select * from movies;


+---------+----------------+-------------+-------+-------------+
| movieID | name | releaseDate | genre | pricePerDay |
+---------+----------------+-------------+-------+-------------+
| 1 | Logan | 2017-12-05 | 1 | 200 |
| 2 | Star Wars | 1990-01-07 | 1 | 150 |
| 3 | Conjuring | 2015-06-04 | 3 | 175 |
| 4 | Grudge | 2012-11-03 | 3 | 140 |
| 5 | Mr Bean | 1994-05-12 | 2 | 200 |
| 6 | Kung Fu Panda | 2009-12-21 | 2 | 160 |
| 7 | Lion king | 1994-06-22 | 4 | 250 |
| 8 | August Rush | 2009-06-14 | 5 | 160 |
| 9 | School Of Rock | 1997-06-22 | 5 | 300 |
| 10 | Transformers | 2014-06-12 | 1 | 275 |
+---------+----------------+-------------+-------+-------------+

c).
Create Table clients(clientID INT PRIMARY KEY AUTO_INCREMENT, firstName
VARCHAR(20) NOT NULL, lastName VARCHAR(20), dob DATE, phone VARCHAR(20) NOT NULL);

desc clients;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| clientID | int(11) | NO | PRI | NULL | auto_increment |
| firstName | varchar(20) | NO | | NULL | |
| lastName | varchar(20) | YES | | NULL | |
| dob | date | YES | | NULL | |
| phone | varchar(20) | NO | | NULL | |
+-----------+-------------+------+-----+---------+----------------+

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","Ivanova","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");

Select * from clients;


+----------+-----------+----------+------------+-------------+
| clientID | firstName | lastName | dob | phone |
+----------+-----------+----------+------------+-------------+
| 1 | Mark | Guiliana | 1989-09-12 | 122-555-888 |
| 2 | Benny | Greb | 1985-04-04 | 545-862-745 |
| 3 | mark | twain | 1990-07-22 | 541-965-785 |
| 4 | Anna | Ivanova | 1994-09-12 | 654-785-022 |
| 5 | Kate | Bengio | 2000-12-01 | 854-885-446 |
| 6 | Siraj | Rawal | 1991-04-18 | 854-889-886 |
| 7 | James | Arthur | 1989-06-24 | 654-852-962 |
+----------+-----------+----------+------------+-------------+

d).
Create Table rents(rentID INT PRIMARY KEY, movie INT NOT NULL,client INT NOT
NULL, starDate DATE NOT NULL, endDate DATE NOT NULL,FOREIGN KEY(movie)
REFERENCES movies (movieID),FOREIGN KEY(client) REFERENCES clients(clientID));

desc rents;
+----------+---------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| rentID | int(11) | NO | PRI | NULL | |
| movie | int(11) | NO | MUL | NULL | |
| client | int(11) | NO | MUL | NULL | |
| starDate | date | NO | | NULL | |
| endDate | date | NO | | NULL | |
+----------+---------+------+-----+---------+-------+

insert into rents values


-> (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");

Select * from rents;


+--------+-------+--------+------------+------------+
| rentID | movie | client | starDate | endDate |
+--------+-------+--------+------------+------------+
| 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)

a).
Select * from clients
-> Where(firstName Like"m%");
+----------+-----------+----------+------------+-------------+
| clientID | firstName | lastName | dob | phone |
+----------+-----------+----------+------------+-------------+
| 1 | Mark | Guiliana | 1989-09-12 | 122-555-888 |
| 3 | mark | twain | 1990-07-22 | 541-965-785 |
+----------+-----------+----------+------------+-------------+

b).
Select lastName,dob from clients
-> Where(lastName like"%a" or "%o");
+----------+------------+
| lastName | dob |
+----------+------------+
| Guiliana | 1989-09-12 |
| Ivanova | 1994-09-12 |
+----------+------------+

c).
Select * from movies
-> Where(year(releaseDate)>=1990 AND year(releaseDate)<2000);
+---------+----------------+-------------+-------+-------------+
| movieID | name | releaseDate | genre | pricePerDay |
+---------+----------------+-------------+-------+-------------+
| 2 | Star Wars | 1990-01-07 | 1 | 150 |
| 5 | Mr Bean | 1994-05-12 | 2 | 200 |
| 7 | Lion king | 1994-06-22 | 4 | 250 |
| 9 | School Of Rock | 1997-06-22 | 5 | 300 |
+---------+----------------+-------------+-------+-------------+

d).
Select * from clients
-> Where(month(dob)=09 or month(dob)=04);
+----------+-----------+----------+------------+-------------+
| clientID | firstName | lastName | dob | phone |
+----------+-----------+----------+------------+-------------+
| 1 | Mark | Guiliana | 1989-09-12 | 122-555-888 |
| 2 | Benny | Greb | 1985-04-04 | 545-862-745 |
| 4 | Anna | Ivanova | 1994-09-12 | 654-785-022 |
| 6 | Siraj | Rawal | 1991-04-18 | 854-889-886 |
+----------+-----------+----------+------------+-------------+

e).
Select * from movies
-> Where(genre=1 or genre=3);
+---------+--------------+-------------+-------+-------------+
| movieID | name | releaseDate | genre | pricePerDay |
+---------+--------------+-------------+-------+-------------+
| 1 | Logan | 2017-12-05 | 1 | 200 |
| 2 | Star Wars | 1990-01-07 | 1 | 150 |
| 3 | Conjuring | 2015-06-04 | 3 | 175 |
| 4 | Grudge | 2012-11-03 | 3 | 140 |
| 10 | Transformers | 2014-06-12 | 1 | 275 |
+---------+--------------+-------------+-------+-------------+

f).

Select avg(pricePerDay) from movies;


+------------------+
| avg(pricePerDay) |
+------------------+
| 201.0000 |
+------------------+

You might also like