Docsity

Introduction to Database Design with Examples - Study Guide | CMSC 424, Study notes of Principles of Database Management

Toggle sidebar
bg1
--9. C reate View A sian R ivers
create view Asian_Rivers as
(select distinct name, length from encompasses, river, geo_River
where continent = 'Asia'
and encompasses.country = geo_River.country
and geo_River.river = river.name);
-- 11. Adding population density column to the country table
alter table country add pop_density real;
-- 12. Addin g popu lation densi ty dat a
update country set pop_density = population / area;
-- 13. Count ries w hose p opulat ion de snsity diffe rs
-- by a fact or of 10,000 or mo re
select T.name, S.name from country T, country S
where T.pop_density / S.pop_density > 10000;
--14. 5th la rgest econom y in t he wor ld
select name, gdp, t.rank_seq
from ( select country, gdp, rank() over (order by gdp desc) rank_seq from economy where gdp >0) t, country
where country.code = t.country and t.rank_seq = 5;
-- 15. Count ries t hat sp an mor e than one c ontine nt
select name, T.continent, count(S.continent)
from c ountry , enco mpasse s T, e ncompa sses S
where code = T.country and code = S.country and T.percentage < 100
group by name, T.continent;
-- 16. Trigg ering additi on of host c ountry to
-- mem bershi p of n ew org anizat ions
create or replace trigger New_organization_trigger
after insert
on Organization
for each row
begin
insert into Is_member
(C ountry , Orga nizati on, Type)
values
(: new.Co untry, :new. Abbrev iation , 'host');
end ;
/
;
-- 17. Proje cted p opulat ion of Austr alia i n 2010
select c.Population* power(1+ p.Population_growth/100, 3)
from C ountry c, Po pulati on p
where p.Country = c.Code and c.Name = 'Australia';
-- 18. Euro_ percen tage t able
create table Euro_Percentage as
with Europe_Population as
(select sum(c.Population) E_pop
from Country c, Encompasses e
where c.Code = e.Country and e.Continent = 'Europe'
group by e.Continent)
select c.Name as Name,
c.P opulat ion / Europe _Popul ation. E_pop* 100 a s Percentage
fro m Country c, Encompasses e, Europe_Population
where c.Code = e.Country and e.Continent = 'Europe';
-- 19. Count ries 3 hop a way fr om Tur key
create table hop1 (c1 varchar(32), c2 varchar(32));
pf2

Partial preview of the text

Download Introduction to Database Design with Examples - Study Guide | CMSC 424 and more Study notes Principles of Database Management in PDF only on Docsity!

--9. Create View Asian Rivers create view Asian_Rivers as (select distinct name, length from encompasses, river, geo_River where continent = 'Asia' and encompasses.country = geo_River.country and geo_River.river = river.name); -- 11. Adding population density column to the country table alter table country add pop_density real; -- 12. Adding population density data update country set pop_density = population / area; -- 13. Countries whose population desnsity differs -- by a factor of 10,000 or more select T.name, S.name from country T, country S where T.pop_density / S.pop_density > 10000; --14. 5th largest economy in the world select name, gdp, t.rank_seq from (select country, gdp, rank() over (order by gdp desc) rank_seq from economy where gdp >0) t, country where country.code = t.country and t.rank_seq = 5; -- 15. Countries that span more than one continent select name, T.continent, count(S.continent) from country, encompasses T, encompasses S where code = T.country and code = S.country and T.percentage < 100 group by name, T.continent; -- 16. Triggering addition of host country to -- membership of new organizations create or replace trigger New_organization_trigger after insert on Organization for each row begin insert into Is_member (Country, Organization, Type) values (:new.Country, :new.Abbreviation, 'host'); end ; / ; -- 17. Projected population of Australia in 2010 select c.Population* power(1+ p.Population_growth/100, 3) from Country c, Population p where p.Country = c.Code and c.Name = 'Australia'; -- 18. Euro_percentage table create table Euro_Percentage as with Europe_Population as (select sum(c.Population) E_pop from Country c, Encompasses e where c.Code = e.Country and e.Continent = 'Europe' group by e.Continent) select c.Name as Name, c.Population / Europe_Population.E_pop*100 as Percentage from Country c, Encompasses e, Europe_Population where c.Code = e.Country and e.Continent = 'Europe'; -- 19. Countries 3 hop away from Turkey create table hop1 (c1 varchar(32), c2 varchar(32));

create table hop2 (c1 varchar(32), c2 varchar(32)); create table hop3 (c1 varchar(32), c2 varchar(32)); insert into hop1 (select country1, country2 from borders, country where name = 'Turkey' and code = country1) union (select country2, country1 from borders, country where name = 'Turkey' and code = country2); insert into hop2 (select a.c1, b.country2 from hop1 a, borders b where a.c2 = b.country1 union (select a.c1, b.country1 from hop1 a, borders b where a.c2 = b.country2)); insert into hop3 (select hop2.c1, borders.country2 from hop2, borders where hop2.c2 = borders.country union (select hop2.c1, borders.country1 from hop2, borders where hop2.c2 = borders.country2)); select name from country, (select distinct c2 result from hop3 minus (select distinct c2 from hop2 union (select distinct c2 from hop1 union (select distinct c1 from hop1)))) where result = code; --20. Total population of Asia select sum(c.Population* e.Percentage/100) from Country c, Encompasses e where c.Code = e.Country and e.Continent = 'Asia' group by e.Continent;