Appendix D. Solutions to Assignments

Table of Contents

Assignments From Chapter 1
Solutions
Assignments From Chapter 2
Solutions
Assignments From Chapter 3
Solutions
Solutions
Assignments From Chapter 4
Solutions
Assignments From Chapter 5
Solutions
Assignments From Chapter 6
Solutions

for localhost only


  1. Make the (slight) changes to the above diagram that are sufficient to do the two selects shown in #3, and #4. Submit the diagram as a png file (b.png).
  2. Create and populate a database with testdata sufficient to do the two selects shown below. Submit the sql declarations as an sql text file (b.sql).
  3. Retrieve a list of the colors that exists in both paper and plastic. Add the necessary declaration(s) to b.sql.
  4. A list of the colors that only exists in paper. Add the necessary declaration(s) to b.sql.

Example D.1. Declarations b.sql

create database ex10d3;
use ex10d3
create table color(
    colorcode char(6) not null primary key
);
create table brick(
    type int not null primary key,
    colorcode char(6) not null,
    foreign key (colorcode) references color(colorcode)
);
create table coloredpaper(
    type int not null primary key,
    colorcode char(6) not null,
    foreign key (colorcode) references color(colorcode)
);
insert into color values('000000');
insert into color values('333333');
insert into color values('666666');
insert into color values('999999');
insert into color values('cccccc');
insert into color values('ffffff');
insert into coloredpaper values(1,'000000');
insert into coloredpaper values(2,'333333');
insert into coloredpaper values(5,'999999');
insert into coloredpaper values(9,'cccccc');
insert into brick values(1,'000000');
insert into brick values(2,'333333');
insert into brick values(3,'666666');

-- solutions #3

-- ideal, refer to diagram, not feasible in mysql
select colorcode from coloredpaper
intersect
select colorcode from brick;

-- or
select brick.colorcode 
from brick, coloredpaper 
where brick.colorcode = coloredpaper.colorcode;

-- or
select colorcode 
from coloredpaper 
where colorcode in (select colorcode from brick);

-- or
select coloredpaper.colorcode 
from coloredpaper 
inner join brick 
on brick.colorcode=coloredpaper.colorcode;

-- solutions #4

-- ideal, refer to diagram, not feasible in mysql
select colorcode from coloredpaper
except    
select colorcode from brick;

-- or
select colorcode 
from coloredpaper
where colorcode not in (select colorcode from brick);

-- or
select colorcode from coloredpaper 
left join brick using (colorcode) 
where brick.colorcode is null;