w7 57-010126-2009-8

39
Week 7 57-01012-62009-8 Thanaphat Kalaya

Upload: ohmresistor

Post on 19-Mar-2017

60 views

Category:

Education


0 download

TRANSCRIPT

Page 1: W7 57-010126-2009-8

Week 757-01012-62009-8 Thanaphat Kalaya

Page 2: W7 57-010126-2009-8

Task 1Implement last week relations to database using selected DBMS

Page 3: W7 57-010126-2009-8

Solution

RelationsCinemas

Theaters

show

Movies

c_id

t_id seat_cap c_id

t_id m_id time_slot price num_adult num_child

m_id

Page 4: W7 57-010126-2009-8

Solution

RelationsCinemas

Theaters

show

Movies

c_id

t_id seat_cap c_id

t_id m_id time_slot price num_adult num_child

m_id

Page 5: W7 57-010126-2009-8

Solution

RelationsCinemas

Theaters

show

Movies

c_id

t_id seat_cap c_id

t_id m_id time_slot price num_adult num_child

m_id

Page 6: W7 57-010126-2009-8

Note : foreign keys

Page 7: W7 57-010126-2009-8

Solution

RelationsCinemas

Theaters

show

Movies

c_id

t_id seat_cap c_id

t_id m_id time_slot price num_adult num_child

m_id

Page 8: W7 57-010126-2009-8

Solution

code : cinemasCREATE TABLE `cinemas` (

`c_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

`c_name` CHAR(50) NOT NULL DEFAULT '0',

PRIMARY KEY (`c_id`)

);

Page 9: W7 57-010126-2009-8

Solution

code : theatersCREATE TABLE `theaters` (

`t_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

`t_num` INT(10) NOT NULL,

`seat_cap` INT(10) UNSIGNED NOT NULL,

`c_id` INT(10) UNSIGNED NOT NULL,

PRIMARY KEY (`t_id`),

INDEX `c_id` (`c_id`),

CONSTRAINT `c_id` FOREIGN KEY (`c_id`) REFERENCES `cinemas` (`c_id`)

);

Page 10: W7 57-010126-2009-8

Solution

code : theatersCREATE TABLE `movies` (

`m_id` INT(10) UNSIGNED NOT NULL AUTO_INCREMENT,

`m_name` CHAR(50) NULL DEFAULT '0',

`m_release` DATE NOT NULL,

PRIMARY KEY (`m_id`)

);

Page 11: W7 57-010126-2009-8

Solution

code : theatersCREATE TABLE `shows` (

`t_id` INT(10) UNSIGNED NOT NULL,

`m_id` INT(10) UNSIGNED NOT NULL,

`time_slot` DATETIME NOT NULL,

`price` INT(11) UNSIGNED NOT NULL,

`num_adult` INT(11) UNSIGNED NOT NULL,

`num_child` INT(11) UNSIGNED NOT NULL,

INDEX `t_id` (`t_id`),

INDEX `m_id` (`m_id`),

CONSTRAINT `m_id` FOREIGN KEY (`m_id`) REFERENCES `movies` (`m_id`),

CONSTRAINT `t_id` FOREIGN KEY (`t_id`) REFERENCES `theaters` (`t_id`)

);

Page 12: W7 57-010126-2009-8

Task 2Insert random data into your designed database using selected DBMS

Page 13: W7 57-010126-2009-8

Solution

random_to_theaterDB.py

Page 14: W7 57-010126-2009-8

Solution

random_to_theaterDB.py

Page 15: W7 57-010126-2009-8

run the code

Page 16: W7 57-010126-2009-8

Page 17: W7 57-010126-2009-8

… (1 hrs. later)

Page 18: W7 57-010126-2009-8

Page 19: W7 57-010126-2009-8

… (6 hrs. later)

Page 20: W7 57-010126-2009-8

Page 21: W7 57-010126-2009-8

okey I think I should stop by now.(8 hrs. later)

Page 22: W7 57-010126-2009-8

Result

Page 23: W7 57-010126-2009-8

Result

Page 24: W7 57-010126-2009-8

Result

Page 25: W7 57-010126-2009-8

Result

Page 26: W7 57-010126-2009-8

Result

Page 27: W7 57-010126-2009-8

Result

The program generated

- 19 rows of data for table “cinemas”

- 530 rows of data for table “movies”

- 541,457 rows of data for table “shows”

- 208 rows of data for table “theaters”

Page 28: W7 57-010126-2009-8

Task 3Write 5 meaningful/practical SQL queries

Page 29: W7 57-010126-2009-8

#1 Query

select

cinemas.c_name,

movies.m_name,

theaters.t_num,

shows.time_slot,

shows.price

from shows

inner join movies on shows.m_id = movies.m_id

inner join theaters on theaters.t_id = shows.t_id

inner join cinemas on cinemas.c_id = theaters.c_id

order by c_name,m_name,time_slot,t_num,price;

Page 30: W7 57-010126-2009-8

#1 Query Result (2.360 sec.)

Page 31: W7 57-010126-2009-8

#2 Query

select

cinemas.c_name,

movies.m_name,

theaters.t_num,

shows.time_slot,

round((shows.num_adult / (shows.num_adult + shows.num_child)) * 100) as percent_adult,

round((shows.num_child / (shows.num_adult + shows.num_child)) * 100) as percent_child,

shows.price*(shows.num_adult + shows.num_child) as income

from shows

inner join movies on shows.m_id = movies.m_id

inner join theaters on theaters.t_id = shows.t_id

inner join cinemas on cinemas.c_id = theaters.c_id

order by c_name,m_name,time_slot,t_num,price;

Page 32: W7 57-010126-2009-8

#2 Query Result (2.766 sec.)

Page 33: W7 57-010126-2009-8

#3 Query

select

cinemas.c_name,

movies.m_name,

theaters.t_num,

shows.time_slot,

shows.price,

round((shows.num_adult / (shows.num_adult + shows.num_child)) * 100) as percent_adult,

round((shows.num_child / (shows.num_adult + shows.num_child)) * 100) as percent_child,

shows.price*(shows.num_adult + shows.num_child) as income

from shows

inner join movies on shows.m_id = movies.m_id

inner join theaters on theaters.t_id = shows.t_id

inner join cinemas on cinemas.c_id = theaters.c_id

order by c_name,m_name,time_slot,t_num,price;

Page 34: W7 57-010126-2009-8

#3 Query Result (2.782 sec.)

Page 35: W7 57-010126-2009-8

#4 Query

select

cinemas.c_name,

movies.m_name,

theaters.t_num,

shows.time_slot,

shows.price,

shows.num_adult,

shows.num_child,

shows.num_adult + shows.num_child as num_customers,

theaters.seat_cap,

round((shows.num_adult / (shows.num_adult + shows.num_child)) * 100) as percent_adult,

round((shows.num_child / (shows.num_adult + shows.num_child)) * 100) as percent_child,

shows.price*(shows.num_adult + shows.num_child) as income

from shows

inner join movies on shows.m_id = movies.m_id

inner join theaters on theaters.t_id = shows.t_id

inner join cinemas on cinemas.c_id = theaters.c_id

order by c_name,m_name,time_slot,t_num,price;

Page 36: W7 57-010126-2009-8

#4 Query Result (3.063 sec.)

Page 37: W7 57-010126-2009-8

#5 Query

select distinct

movies.m_name,

sum(shows.price*(shows.num_adult + shows.num_child)) as total_income

from shows

inner join movies on shows.m_id = movies.m_id

group by movies.m_id

order by total_income desc;

Page 38: W7 57-010126-2009-8

#5 Query Result (0.406 sec.)

Page 39: W7 57-010126-2009-8

End of week 7