w7 57-010126-2009-8

Post on 19-Mar-2017

60 Views

Category:

Education

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Week 757-01012-62009-8 Thanaphat Kalaya

Task 1Implement last week relations to database using selected DBMS

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

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

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

Note : foreign keys

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

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`)

);

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`)

);

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`)

);

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`)

);

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

Solution

random_to_theaterDB.py

Solution

random_to_theaterDB.py

run the code

… (1 hrs. later)

… (6 hrs. later)

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

Result

Result

Result

Result

Result

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”

Task 3Write 5 meaningful/practical SQL queries

#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;

#1 Query Result (2.360 sec.)

#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;

#2 Query Result (2.766 sec.)

#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;

#3 Query Result (2.782 sec.)

#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;

#4 Query Result (3.063 sec.)

#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;

#5 Query Result (0.406 sec.)

End of week 7

top related