Tutorial 3 Solution Exercise1: Exercise 2: { PreviousEducation ( CollegeName, StartDate, EndDate, { Degree (DegreeName, Month, Year) }, { Transcript (CourseName, Semester, Year, Grade) } ) } Exercise 3: (a) Entity types: BANK, ACCOUNT, CUSTOMER, LOAN (b) Weak entity type: BANK-BRANCH. Partial key: BranchNo. Identifying relationship: BRANCHES. (c) The partial key BranchNo in BANK-BRANCH specifies that the same BranchNo value may occur under different BANKs. The identifying relationship BRANCHES specifies that BranchNo values are uniquely assigned for those BANK-BRANCH entities that are related to the same BANK entity. Hence, the combination of BANK Code and BranchNo together constitute a full identifier for a BANK-BRANCH.
(d) Relationship Types: BRANCHES, ACCTS, LOANS, A-C, L-C. The (min, max) constraints are shown below. (e) The requirements may be stated as follows: Each BANK has a unique Code, as well as a Name and Address. Each BANK is related to one or more BANK-BRANCHes, and the BranhNo is unique among each set of BANK-BRANCHes that are related to the same BANK. Each BANK-BRANCH has an Address. Each BANK-BRANCH has zero or more LOANS and zero or more ACCTS. Each ACCOUNT has an AcctNo (unique), Balance, and Type and is related to exactly one BANK-BRANCH and to at least one CUSTOMER. Each LOAN has a LoanNo (unique), Amount, and Type and is related to exactly one BANK-BRANCH and to at least one CUSTOMER. Each CUSTOMER has an SSN (unique), Name, Phone, and Address, and is related to zero or more ACCOUNTs and to zero or more LOANs. (f) The (min, max) constraints would be changed as follows:
Mid 1 Solution: Question 1 : DBMS enables users to create and maintain a database. A foreign key is a referential constraint between two tables. It identifies a column or a set of columns in one table that refers to a set of columns in another table. Referential integrity constraint is specified between two relations and is used to maintain the consistency among tuples in the two relations. Question 2 : The schema is: Person(ssn, name, address) Car(license, state, model, make, year, owner) Car.owner foreign key to Person.ssn Claim(claimId, description, policy) ** Note: This is for 3-way relationship version. For weak entity version, Item.date is also a key field. Item(ssn, license, state, claimid, date, amount)
Item.ssn foreign key to Person.ssn Item.claim foreign key to Claim.claimId Item.license and Item.state foreign key to Car.license and Car.state Payment(claimId, date, ssn, amount) Payment.ssn foreign key to Person.ssn Payment.claimId foreign key to Claim.claimId Note the primary key of payment depends on your assumptions. You could make it have an auto-number or some other system generated id in which case it is: Payment(id, ssn, claimid, date, amount) You could make Payment a weak entity dependent on claim id and use id or date as PPK: Payment(id, ssn, claimid, date, amount) OR Payment(ssn, claimid, date, amount) In all cases, there still is a foreign key on ssn and on claimid. Question 3: 1. What are the names of students enrolled in CS380? name( csc380=code(student enrolledin)) 2. Which subjects is Yazeed taking? code( name=yazeed(student enrolledin)) 3. Who teaches CSC 380? lecturer( code=csc380(subject)) 4. What are the names of students in both CSC 380 and CSC 383? name( code=csc380(student enrolledin)) name( code=csc383(student enrolledin))
CSC 380 Introduction to Database Solution: Midterm 2 Exercise 1: First Normal Form (1NF): Remove all repeating groups. Make a separate table for each set of related attributes, and give each table a primary key Second Normal Form (2NF): Remove partial dependencies. Eliminate redundant data. If an attribute depends on part of a multi-part(concatenated) key, remove it to a separate table. (assuming E-phone & B-Phone belong to instructor? If they belong to student, them should go to student. Assume, based on your original example, registration date, total fees and fees paid are applied to the student as a whole, not for each course. They should belong to student) STUDENT_COURSE (Student-ID+ Course-ID, Days/Time, Room/Bldg, Instructor, E-Phone, B-Phone ) STUDENT (Student-ID, S-Name, HomeAddr, Advisor, Ofc-Phone, RegisDate, TotalFees, FeesPaid ) COURSE (Course-ID, HoursCredit) Third Normal Form (3NF): Remove transitive dependencies. Eliminate columns not dependent on the primary key. If attributes do not contribute to a description of a key, move them to a separate table. STUDENT-COURSE (Student-ID + Course-ID [fk], Days/Time, Room/Bldg, Instructor[fk] ) SECTION(Course-ID[fk] + Days/Time, Room/Bldg, Instructor[fk] ) STUDENT (Student-ID, S-Name, HomeAddr, Advisor[fk], RegisDate, TotalFees, FeesPaid ) COURSE (Course-ID, HoursCredit ) ADVISOR (Advisor, Ofc-Phone) INSTRUCTOR (Instructor, E-Phone, B-Phone )
Exercise 3: SQL Queries: a/ Print title and price of all DVDs in the category Comedy, which cost less than 25 SR. Sort the output by the price (lowest price first). SELECT TITLE, PRICE FROM DVD WHERE CATEGORY = Comedy AND PRICE < 25 ORDER BY PRICE b/ Print number and title of all films (DVDs) that have an English audio track and got 5 stars from the user sb. SELECT E.NO, TITLE FROM EVALUATION E, AUDIOTRACK A, DVD D WHERE D.NO = E.NO AND E.NO = A.NO AND UID = sb AND LANGUAGE = English AND STARS = 5 c/ Print the title of all DVDs with an audio track in English and in Spanisch (both languages on the same DVD). SELECT TITLE FROM DVD D, AUDIOTRACK X, AUDIOTRACK Y WHERE X.NO = Y.NO AND D.NO = X.NO AND X.LANGUAGE = Spanisch AND Y.LANGUAGE = English d/ Which DVDs have only an Arabic audio track? Print the title of all such DVDs. SELECT TITLE FROM DVD D WHERE NO NOT IN (SELECT NO FROM AUDIOTRACK WHERE LANGUAGE <> arabic ) e/ Print for each DVD that was evaluated by at least two customers the title, the category, and the average evaluation (number of stars). The output column for the average evaluation should have the name GRADE. SELECT TITLE, CATEGORY, AVG(STARS) AS GRADE FROM DVD D, EVALUATION E WHERE D.NO = E.NO GROUP BY D.NO, TITLE, CATEGORY HAVING COUNT(*) >= 2 SQL CREATE TABLE: CREATE TABLE EVALUATION( NO NUMERIC(5) NOT NULL REFERENCES DVD, UID VARCHAR(20) NOT NULL, STARS NUMERIC(1) NOT NULL, TEXT VARCHAR(2000) NOT NULL, PRIMARY KEY(NO, UID), CONSTRAINT STARS_VALID CHECK(STARS BETWEEN 1 AND 5))
King Saud University College of Computer and Information Sciences Computer Science Department CSC380: Introduction to Database Instructor: Dr. Kamal Haouam Midterm: 2 Sunday: 11/02/1432 (16/01/2011) Time: 90mn Exercise 1: Given the following relation, expressed as: REGISTRATION CARD (Student-ID, S-Name, HomeAddr, Advisor, Ofc-Phone, {Course-ID, Days/Time, HoursCredit, Room/Bldg, Instructor}, E-Phone, B-Phone, RegisDate,TotalFees, FeesPaid) Construct the 1NF, 2NF, AND 3NF. Exercise 2: Given the following relation, expressed as (City, E#, EN, Skill,S, Age) with the typical data shown below, construct the 2NF. Show the answer in the notational form. City E# EN Skill S Age Riyadh 3 Fahed 3 4 24 Dammam 4 Naef 3 4 27 Dammam 2 Saoud 4 5 24 Dammam 3 Fahed 6 9 24 Riyadh 7 Bader 2 6 19 Madinah 2 Saoud 3 4 27 Exercise 3: An online shop for DVDs (video films) uses the following three tables: The first table contains the most important data of the DVDs. Each DVD has a unique number. The database does not contain any null values. DVD NO TITLE CATEGORY PRICE 1 Snow White Cartoon 27.99 2 New Shots! Part 2 Comedy 25.99 3 Fire Tongs Comedy 24.99 4 Spaceballs Comedy 19.99 In contrast to VHS video tapes, DVDs often contain several audio tracks in different languages. The second table contains this information. NO is a foreign key that refers to DVD.
AUDIOTRACK NO LANGUAGE 1 Arabic 1 English 2 Arabic 2 English 2 Spanish 3 Arabic 4 Arabic 4 English 4 French 4 Italian Finally, there is a table with evaluations of the DVDs by customers or users of the website. A user can rate a DVD by giving between one and five stars. NO is again a foreign key that refers to the table DVD. EVALUATION NO UID STARS TEXT 1 Sb 4 Sweet 1 Nina 3 Nice 1 Lisa 5 I watched it already 50 times. 2 Sb 4 funny 3 Sb 3 funny, but picture and sound quality not good. 4 Sb 5 Very funny 4 Nina 3 Well, Ok Questions:( SQL Queries) a) Print title and price of all DVDs in the category Comedy, which cost less than 25 SR. Sort the output by the price (lowest price first). In the example, the query result b) Print number and title of all films (DVDs) that have an English audio track and got 5 stars from the user sb. c) Print the title of all DVDs with an audio track in English and in Spanish (both languages on the same DVD). d) Which DVDs have only an Arabic audio track? Print the title of all such DVDs. e) Print for each DVD that was evaluated by at least two customers the title, the category, and the average evaluation (number of stars). The output column for the average evaluation should have the name GRADE. Questions: (SQL CREATE TABLE) Write a CREATE TABLE statement for the table EVALUATION. Declare the usual constraints (keys, foreign keys, NOT NULL). Use a CHECK-constraint to ensure that the attribute STARS can only take the values 1, 2, 3, 4, 5. The attribute NO is a decimal number of up to five digits. UID is a string up to length 20. TEXT is a string of maximal length 2000. Null values are not allowed in any column.
CSC380 Fall 2010 Questions Homework SQL Due date : January 11, 2011 Use the Mesri s database, found in my account, to answer the following questions: 1. Retrieve the name of male employees who were born before the year 1938 2. Retrieve the supervisor name for employees living at Houston and working at a department located at the same town. 3. List the different dependent relationships. 4. Retrieve the SSN of employees that do not work for any project controlled by department number 4. Explain: a) A natural join between works_on and project with a qualification dnum<>5 is not enough (expl: essn=333445555). b) Explain that we take employee and not works_on because an employee who does not have a project at all will be selected. 5. Retrieve the name of each employee who works on all projects controlled by department 5 (explain how the SQL will proceed to execute this statement) 6. For each employee, retrieve the name of his or her immediate supervisor. 7. For each department which it is assigned more than two employees, list the department number, the department name, the number of employees assigned and the average salary. 8. Create a view called project_summary, wich lists for each project the project number, the project name, the number of employees, and the minimum and maximum salary.
King Saud University College of Computer and Information Sciences Computer Science Department CSC380: Introduction to Database Instructor: Dr. Kamal Haouam Midterm: 1 Wednesday: 9/1/1432 (15/12/2010) Time: 90mn Question 1 : What is the main functionality of a DBMS? What is a foreign key constraint? Why are such constraints important? What is referential integrity? Given two relation schemas R(A1,A2) and S(A2,A3), define a relational algebra expression which is the equivalent of R JOIN S using only operations from the following list: union, difference, Cartesian product, projection, selection, renaming. Question 2 : The Tawuniya insurance company agrees to pay you to design and implement a database based on the following information: 1. A Person is identified by social security number. The person name and address must be recorded. 2. Cars are identified by license plate. For each car, the model, make, and year is recorded. 3. For each claim, the claim id, policy and description must be recorded. The claim id identifies a claim uniquely. 4. A car is owned by a person. 5. A policy holder itemizes elements of a claim by relating a car, a person, and a claim with a date and amount. 6. Multiple payments may be made from a claim to the person involved in the claim with values for amount and date of payment. Draw the ER diagram for this database. Map your ER diagram into a relational schema (show your keys and foreign keys).
Question 3 : Give the following queries in the relational algebra using the relational schema given above, that is student(id, name) enrolledin(id, code) subject(code, lecturer) 1. What are the names of students enrolled in CSC380? 2. Which subjects is Yazeed taking? 3. Who teaches CSC380? 4. What are the names of students in both CSC380 and CSC383? 5. What are the names of students taking a subject taught by Kamal? 6. Count the number of students enrolled in CSC380. Solution: Question 1 : DBMS enables users to create and maintain a database. A foreign key is a referential constraint between two tables. It identifies a column or a set of columns in one table that refers to a set of columns in another table. Referential integrity constraint is specified between two relations and is used to maintain the consistency among tuples in the two relations. Question 2 :
The schema is: Person(ssn, name, address) Car(license, state, model, make, year, owner) Car.owner foreign key to Person.ssn Claim(claimId, description, policy) ** Note: This is for 3-way relationship version. For weak entity version, Item.date is also a key field. Item(ssn, license, state, claimid, date, amount) Item.ssn foreign key to Person.ssn Item.claim foreign key to Claim.claimId Item.license and Item.state foreign key to Car.license and Car.state Payment(claimId, date, ssn, amount) Payment.ssn foreign key to Person.ssn Payment.claimId foreign key to Claim.claimId Note the primary key of payment depends on your assumptions. You could make it have an auto-number or some other system generated id in which case it is: Payment(id, ssn, claimid, date, amount) You could make Payment a weak entity dependent on claim id and use id or date as PPK: Payment(id, ssn, claimid, date, amount) OR Payment(ssn, claimid, date, amount) In all cases, there still is a foreign key on ssn and on claimid.
CSC380 Fall 2010 Tutorial 4 Solution THE RELATIONAL DATA MODEL AND RELATIONAL DATABASE CONSTRAINTS Exercise1: The schema of this question has the following four foreign keys: 1. the attribute SSN of relation ENROLL that references relation STUDENT, 2. the attribute Course# in relation ENROLL that references relation COURSE, 3. the attribute Course# in relation BOOK_ADOPTION that references relation COURSE, and 4. the attribute Book_ISBN of relation BOOK_ADOPTION that references relation TEXT. Exercise2: Answers: (a) No constraint violations. (b) Violates referential integrity because DNUM=2 and there is no tuple in the DEPARTMENT relation with DNUMBER=2. We may enforce the constraint by: (i) rejecting the insertion of the new PROJECT tuple, (ii) changing the value of DNUM in the new PROJECT tuple to an existing DNUMBER value in the DEPARTMENT relation, or (iii) inserting a new DEPARTMENT tuple with DNUMBER=2. (c) Violates both the key constraint and referential integrity. Violates the key constraint because there already exists a DEPARTMENT tuple with DNUMBER=4. We may enforce this constraint by: (i) rejecting the insertion, or (ii) changing the value of DNUMBER in the new DEPARTMENT tuple to a value that does not violate the key constraint. Violates referential integrity because MGRSSN='943775543' and there is no tuple in the EMPLOYEE relation with SSN='943775543'. We may enforce the constraint by: (i) rejecting the insertion, (ii) changing the value of MGRSSN to an existing SSN value in EMPLOYEE, or (iii) inserting a new EMPLOYEE tuple with SSN='943775543'.
(d) Violates both the entity integrity and referential integrity. Violates entity integrity because PNO, which is part of the primary key of WORKS_ON, is null. We may enforce this constraint by: (i) rejecting the insertion, or (ii) changing the value of PNO in the new WORKS_ON tuple to a value of PNUMBER that exists in the PROJECT relation. Violates referential integrity because ESSN='677678989' and there is no tuple in the EMPLOYEE relation with SSN='677678989'. We may enforce the constraint by: (i) rejecting the insertion, (ii) changing the value of ESSN to an existing SSN value in EMPLOYEE, or (iii) inserting a new EMPLOYEE tuple with SSN='677678989'. (e) No constraint violations. (f) No constraint violations. (g) Violates referential integrity because several tuples exist in the WORKS_ON, DEPENDENT, DEPARTMENT, and EMPLOYEE relations that reference the tuple being deleted from EMPLOYEE. We may enforce the constraint by: (i) rejecting the deletion, or (ii) deleting all tuples in the WORKS_ON, DEPENDENT, DEPARTMENT, and EMPLOYEE relations whose values for ESSN, ESSN, MGRSSN, and SUPERSSN, respectively, is equal to'987654321'. (h) Violates referential integrity because two tuples exist in the WORKS_ON relations that reference the tuple being deleted from PROJECT. We may enforce the constraint by: (i) rejecting the deletion, or (ii) deleting the tuples in the WORKS_ON relation whose value for PNO=1 (the value for the primary key PNUMBER for the tuple being deleted from PROJECT). (i) No constraint violations. (j) Violates referential integrity because the new value of SUPERSSN='943775543' and there is no tuple in the EMPLOYEE relation with SSN='943775543'. We may enforce the constraint by: (i) rejecting the deletion, or (ii) inserting a new EMPLOYEE tuple with SSN='943775543'. (k) No constraint violations.
CSC380 Fall 2010 Homework SQL Use the Mesri s database, found in my account, to answer the following questions: 1. Retrieve the name of male employees who were born before the year 1938 Select fname,lname,minit From employe Where sex= M and bdate<to_date( 01-jan-1938 ); 2. Retrieve the supervisor name for employees living at Houston and working at a department located at the same town. 3. List the different dependent relationships. Select E2.fname, E2.minit,E2.lname From employee E1, employee E2, dept_location Where E2.dno=dnumber and E1.superssn=E2.ssn; Dlocation= Houston and E1.address like %Houston% ; Select distinct relationship From dependent; 4. Retrieve the SSN of employees that do not work for any project controlled by department number 4. Select ssn From employee Where ssn not in (select essn from works_on, project where pnumber=pno and dnum=4); 5. Retrieve the name of each employee who works on all projects controlled by department 5 (explain how the SQL will proceed to execute this statement) select Fname, lname from employee where not exists (select * from works_on B, project where pno=pnumber and dnum=5 and not exists (select * from works_on C where B.pno=C.pno and C.essn=ssn)); 1
6. For each employee, retrieve the name of his or her immediate supervisor. Select E1.SSN, E2.fname,E2.minit,E2.lname From employee E1, employee E2 Where E1.superssn=e2.ssn(+); 7. For each department which it is assigned more than two employees, list the department number, the department name, the number of employees assigned and the average salary. Select dnumber, dname, count(*), avg(salary) From department, employee Where dno=dnumber Group by dnumber, dname Having count(*)>2; 8. Create a view called project_summary, wich lists for each project the project number, the project name, the number of employees, and the minimum and maximum salary. Create view project_summary (pnumber, pname, nb_empl, min_sal, max_sal) as Select pnumber, pname, count(*), min(salary), max(salary) From employee, works_on, project Where pno=pnumber and Essn=ssn Group by pnumber,pname. 2