Assignment 2 Due Date: March 26, 2014 (in class) Submit your

advertisement
Assignment 2
Due Date: March 26, 2014 (in class)
Submit your answers in hard copy with cover sheet
(Note: SQL statements are for MySQL)
[Q.1] Answer the following questions
(a) Explain the following query in English (based on company database)
SELECT FNAME, LNAME, SSN
FROM EMPLOYEE
WHERE NOT EXISTS (SELECT *
FROM WORKS_ON B
WHERE (B.PNO IN (SELECT PNUMBER
FROM PROJECT
WHERE DNUM = 5)
AND
NOT EXISTS (SELECT *
FROM WORKS_ON C
WHERE C.ESSN = SSN AND C.PNO = B.PNO))
);
Answer: List the first, last name and social security number of employees who work on
all the projects controlled by department 5.
(b) Modify the query to list employees who do not work all the projects controlled by the
department number 5.
Answer:
SELECT FNAME, LNAME, SSN
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM WORKS_ON B
WHERE (B.PNO IN (SELECT PNUMBER
FROM PROJECT
WHERE DNUM = 5)
AND
NOT EXISTS (SELECT *
FROM WORKS_ON C
WHERE C.ESSN=SSN AND C.PNO = B.PNO))
);
(c) Modify the query to list employees who work on any of the projects controlled by
department number 5.
Answer:
SELECT FNAME, LNAME, SSN
FROM EMPLOYEE
WHERE EXISTS (SELECT *
FROM WORKS_ON B
WHERE (B.PNO IN (SELECT PNUMBER
FROM PROJECT
WHERE DNUM = 5)
AND
EXISTS (SELECT *
FROM WORKS_ON C
WHERE C.ESSN=SSN AND C.PNO = B.PNO))
);
(d) Given two relational tables shown below. Show results by (i) left outer join of T1 and T2
(on T1.P = T2.A) (ii) right outer join of T1 and T2 (on T1.Q = T2.B)
Relation table T1
P
10
15
25
Q
a
b
a
R
5
8
6
Relation table T2
A
10
25
10
B
b
c
b
C
6
3
5
T1 left outer join T2 on T1.P = T2.A
a
25
10
10
15
b
a
a
a
b
c
6
5
5
8
p
25
10
10
null
q
c
b
b
null
r
3
5
6
null
p
10
10
25
q
b
b
c
R
5
6
3
T1 right outer join T2 on T1.Q = T2.B
a
15
15
null
(e)
b
b
b
null
C
8
8
Null
Write SQL statements (in MySQL) to get intersection, difference, and union sets of the
following two tables.
SELECT PNAME
FROM
PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME='Smith';
SELECT PNAME
FROM
PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND
ESSN=SSN AND LNAME='Smith';
Intersection:
drop tables if exists pset1, pset2;
create temporary table pset1(pname varchar(25));
create temporary table pset2(pname varchar(25));
insert into pset1(pname)
SELECT DISTINCT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME=’SMITH’;
insert into pset2(pname)
SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=’SMITH’;
select pname from pset2 where pname in (select pname from pset1);
Difference:
drop tables if exists pset1, pset2;
create temporary table pset1(pname varchar(25));
create temporary table pset2(pname varchar(25));
insert into pset1(pname)
SELECT DISTINCT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME=’SMITH’;
insert into pset2(pname)
SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=’SMITH’;
select pname from pset2 where pname not in (select pname from pset1);
Union:
SELECT PNAME
FROM PROJECT, DEPARTMENT, EMPLOYEE, WORKS_ON
WHERE DNUM=DNUMBER AND MGRSSN=SSN AND LNAME=’SMITH’
UNION
SELECT PNAME
FROM PROJECT, WORKS_ON, EMPLOYEE
WHERE PNUMBER=PNO AND ESSN=SSN AND LNAME=’SMITH’
;
(f) Explain why updates on materialized view are often difficult to perform.
Answer: Views defined on multiple tables using joins are generally not updatable, also
views defined with grouping or aggregation are not updatable. Despite these limitations
views should always be displaying up to date data. The materialized views are created in
main memory for efficiently processing frequent retrieval queries without update
purposes. Updating the view may violate the underlying base table data integrity.
(g) CREATE OR REPLACE VIEW RESIDENTS (TOWN, PLAYERNUMBERS) AS
SELECT TOWN, COUNT(*)
FROM PLAYERS
GROUP BY TOWN;
Can we update RESIDENTS view by the following SQL statement?
insert into Residents values ('Houston', 3);
Answer: No, because the view includes an aggregate function group by, thus, the view is
not updatable.
[Q.2] Answer the following questions based on the following simple university database
(a) Write SQL statement to create and populate SECTION table with referential constraint.
You need to show how to insert one tuple into the table.
(b) Write a SQL statement to retrieve the names of all senior students majoring in CS.
(c) Write a SQL statement to retrieve the names of all courses taught by Professor Anderson
in 2004 and 2005.
(d) Retrieve the name and major departments of all students who do not have any grade A in
any of their courses.
Answer: Review the following SQL statements
SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0;
SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS,
FOREIGN_KEY_CHECKS=0;
SET @OLD_SQL_MODE=@@SQL_MODE,
SQL_MODE='TRADITIONAL,ALLOW_INVALID_DATES';
DROP SCHEMA IF EXISTS simpleUniversity ;
CREATE SCHEMA IF NOT EXISTS simpleUniversity ;
USE simpleUniversity ;
drop table if exists Course;
create table Course(
CourseName varchar(30) NOT NULL,
CourseNumber varchar(10) primary key,
NumOfCredits tinyint,
Department varchar(10) not null
) ENGINE = InnoDB;
drop table if exists Section;
create table Section(
SectionId int not null,
CourseNo varchar(10) not null,
Semester char(6) not null,
Year Date not null,
Instructor varchar(20) not null,
primary key (SectionId, CourseNo, Semester, Year),
#constraint pkSection primary key (SectionId, CourseNo, Semester, Year),
constraint fkSection foreign key (CourseNo) references Course(CourseNumber)
on delete cascade on update cascade
)engine = innoDB;
drop table if exists Prerequisite;
create table Prerequisite (
CourseNo varchar(10) not null,
PrerequisiteNo varchar(10) not null,
primary key (CourseNo, PrerequisiteNo),
constraint fkPrerequisite1 foreign key (CourseNo) references Course(CourseNumber)
on delete no action on update cascade,
constraint fkPrerequisite2 foreign key (PrerequisiteNo) references
Course(CourseNumber)
on delete cascade on update cascade
);
drop table if exists Student;
create table Student (
StudentNumber char(9) primary key,
name varchar(20) not null,
Classification tinyint not null,
DepartmentName varchar(10) not null
) ENGINE = InnoDB;
drop table if exists GradeReport;
create table GradeReport(
StudentNo char(9),
SectionId int not null,
Grade ENUM ('A', 'B', 'C', 'D', 'F'),
primary key (StudentNo, SectionId),
constraint fkGradeReport foreign key (StudentNo) references Student(StudentNumber)
on delete cascade on update cascade
);
-- database population -insert into Course values
('Intro to Commputer Science', 'CS1310', 4, 'CS'),
('Data Structures', 'CS3320', 4, 'CS'),
('Discrete Mathematics', 'MATH2410', 4, 'MATH'),
('Database Systems', 'CS3380', 4, 'CS');
insert into Section values
(85, 'MATH2410', 'Fall', '2004-09-01', 'King'),
(92, 'CS1310', 'Fall', '2004-09-01', 'Anderson'),
(102, 'CS3320', 'Spring', '2005-01-20', 'Knuth'),
(112, 'MATH2410', 'Fall', '05-09-01', 'Chang'),
(119, 'CS1310', 'Fall', '2005-09-01', 'Anderson'),
(135, 'CS3380', 'Fall', '2005-09-01', 'Stone');
insert into Prerequisite values
('CS3380', 'CS3320'),
('CS3380', 'MATH2410'),
('CS3320', 'CS1310');
insert into Student values
('17', 'Bill Smith', 1, 'CS'),
('8', 'Bob Brown', 2, 'CS'),
('11', 'Nick Feiner', 4, 'MATH');
insert into GradeReport values
('17', 112, 'B'),
('17', 119, 'C'),
('8', 85, 'A'),
('17', 135, 'A'),
('8', 92, 'A'),
('11', 85, 'B');
SET SQL_MODE=@OLD_SQL_MODE;
SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS;
SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS;
select * from Course;
select * from Section;
select * from Prerequisite;
select * from GradeReport;
select * from Student;
-- (b) Write a SQL statement to retrieve the names of all senior students majoring in CS -select name from student
where classification = 4 and DepartmentName = 'CS';
select name as 'Student Name' from student
where classification = 4 and DepartmentName = 'MATH';
-- (c) Write a SQL statement to retrieve the names of all courses taught by Professor Anderson in
2004 and 2005.
SELECT CourseName
FROM Course, Section
WHERE Course.CourseNumber = Section.CourseNo
AND Instructor = 'ANDERSON'
AND (YEAR(YEAR) = '2004' OR YEAR(YEAR) = '2005');
SELECT CourseName
FROM Course as C inner join Section S
on C.CourseNumber = S.CourseNo
AND Instructor = 'Anderson'
AND (YEAR(YEAR) = '2004');
-- (d) Retrieve the name and major departments of all students who do not have any grade A in
any of their courses.
-- first select student number who has at least one grade 'A'
Select StudentNumber, name as StudentName, DepartmentName as Major
from Student S
where exists
(select StudentNo
from GradeReport AS GR
where
GR.StudentNo = S.StudentNumber
and Grade = 'A');
-- Retrieve the name and major departments of all students who do not have any grade A in any
of their courses.
Select StudentNumber, name as StudentName, DepartmentName as Major
from Student S
where not exists
(Select StudentNo
from GradeReport AS GR
where
GR.StudentNo = S.StudentNumber
and Grade = 'A');
-- Retrieve the name and major departments of all students who do not have all A grade
Select StudentNumber, name as StudentName, DepartmentName as Major
from Student S
where exists
(Select StudentNo
from GradeReport AS GR
where
GR.StudentNo = S.StudentNumber
and NOT(Grade = 'A'));
-- Retrieve the name and major departments of all students who do have all A grade
Select StudentNumber, name as StudentName, DepartmentName as Major
from Student S
where not exists
(Select StudentNo
from GradeReport AS GR
where
GR.StudentNo = S.StudentNumber
and NOT(Grade = 'A'));
[Q.3] Answer the following questions for a simple library application.
The data requirements of the library application are summarized as follows: BOOK entity is
identified by BookId, it has title and multiple author names. PUBLISHER entity consists of Name,
Address, and Phone attributes. Name is the key for the PUBLISHER. LIBRARY_BRANCH
entity has BranchId as a key and Branchname attribute additionally. BORROWER entity has
BrowerId as key and additionally has name, address, phone attributes. Each
LIBRARY_BRANCH has one or more copies of the same book. In such a case, noOfCopies
attribute needs to be maintained by the relationship. A book is published by only one publisher. A
book can be loaned to a borrower at a specific library branch.
(a)
(b)
(c)
(d)
Draw ER diagram for the conceptual schema of the library database application.
Map the conceptual schema to database relations.
Explain at least two referential integrity constraints.
Write SQL queries for the following queries on the library database:
a. How many copies of the book titled “Database Systems” are owned by the
library branch “Lehman”?
b. Retrieve the names of all borrowers who do not have any books checked out.
c. For each library branch, retrieve the name and the total number of books loaned
out from the branch.
d. For each book authored (co-authored) by “Robert Feinerman”, retrieve the title
and the number of copies owned by the library branch “Lehman”.
Answer: Discussed in the class, and left as a make-up assignment for your mid-term examination.
[Q.4] Consider the following ER diagram for a bank conceptual database schema
(a) List the non-weak entity types in the ER diagram.
(b) Identify any weak entity type, and give its name, its partial key, and its identifying
relationship and entity.
(c) Explain rule(s) that are used to map a weak entity type to a relation (table).
(d) List the names of all relationship types, and specify the cardinality constraints. Each
BANK is related to one or more BANK-BRANCHEs. Each BANK_BRANCH has zero
or more LOANs and zero or more ACCOUNTs. Each ACCOUNT is related to exactly
one BANK-BRANCH and to at least one CUSTOMER. Each LOAN is related to exactly
one BANK_BRANCH and to at least to one CUSTOMER. Each CUSTOMER is related
to zero or more ACCOUNTs and to zero or more LOANs.
(e) Specify the (min,max) constraint on each participation of an entity type in a relationship
type. The additional (min, max) constraints are: Every customer must have at least one
account but is restricted to at most two loans at a time, and that a bank branch cannot
have more than 500 loans.
(f) Identify total participations in the diagram.
Answer:
(a) List the non-weak entity types in the ER diagram.
a. Bank
b. Account
c. Loan
d. Customer
(b) Identify any weak entity type, and give its name, its partial key, and its identifying
relationship and entity.
a. Bank-Branch – BranchNo is the partial key – it is identified by the Branches
relationship to the Bank Entity
(c) Explain rule(s) that are used to map a weak entity type to a relation (table).
a. For each weak entity type, create a relation R and include all the simple
attributes of the entity type as attributes of R
b. Include the primary key attribute of owner as foreign key attributes of R
c. The primary key of R is the combination of the primary key of S (owner) and
partial key of the weak entity
(d) List the names of all relationship types, and specify the cardinality constraints. Each
BANK is related to one or more BANK-BRANCHEs. Each BANK_BRANCH has zero
or more LOANs and zero or more ACCOUNTs. Each ACCOUNT is related to exactly
one BANK-BRANCH and to at least one CUSTOMER. Each LOAN is related to exactly
one BANK_BRANCH and to at least to one CUSTOMER. Each CUSTOMER is related
to zero or more ACCOUNTs and to zero or more LOANs.
Answer: see the figure below
(e) Specify the (min,max) constraint on each participation of an entity type in a relationship
type. The additional (min, max) constraints are: Every customer must have at least one
account but is restricted to at most two loans at a time, and that a bank branch cannot
have more than 500 loans.
Answer: see the figure below
(f) Identify total participations in the diagram.
Bank to Branches
Branches to Bank_Branch
Account to ACCTS
Account to A-C
Loan to Loans
Loan to L-C
Download