COMP3030 Database Management System Final Review

Slides:



Advertisements
Similar presentations
COP4540 Database Management System Midterm Review
Advertisements

ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
Logical DB Design: ER to Relational Entity sets to tables. Employees ssn name lot CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER, PRIMARY.
CSC343: Intro. to Databases1 Tutorial for CSC343 Introduction to Databases Fall 2006 Week 2.
IS698: Database Management Min Song IS NJIT. The Relational Data Model.
COP-5725 Practice Exercises
Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 SQL: Queries, Programming, Triggers Chapter 5 Modified by Donghui Zhang.
The Entity-Relationship Model
(Uses slides from Fernando Farfan and Eduardo J. Ruiz
Entity-Relationship Models
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
1 The Entity-Relationship Model Chapter 2. 2 Overview of Database Design  Conceptual design: (ER Model is used at this stage.) –What are the entities.
COP5725 – Principles of Database Management Systems
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
SPRING 2004CENG 3521 The Relational Model Chapter 3.
The Relational Model 198:541 Rutgers University. Why Study the Relational Model?  Most widely used model. Vendors: IBM, Informix, Microsoft, Oracle,
1 Relational Model. 2 Relational Database: Definitions  Relational database: a set of relations  Relation: made up of 2 parts: – Instance : a table,
1 Data Modeling Yanlei Diao UMass Amherst Feb 1, 2007 Slides Courtesy of R. Ramakrishnan and J. Gehrke.
SQL SQL stands for Structured Query Language SQL allows you to access a database SQL is an ANSI standard computer language SQL can execute queries against.
Entity Relationship Model Chapter 6. Basic Elements of E-R Model Entity Object of the real world that stores data. Eg. Customer, State, Project, Supplier,
ER to Relational Mapping. Logical DB Design: ER to Relational Entity sets to tables. CREATE TABLE Employees (ssn CHAR (11), name CHAR (20), lot INTEGER,
The Relational Model Chapter 3
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3 Modified by Donghui Zhang.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model  Vendors: IBM, Informix, Microsoft, Oracle, Sybase  Recent.
Review for Midterm. Problem 2.7 HasPriPhy Patients address name ssn age Doctors exp_year name ssn specialty Pharmacies name address phone PharmCo name.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc.
Assignements. CSC343: Intro. to Databases2 Exercise 1 Superkeys: Candidate keys: Primary key:
1 The Relational Model. 2 Why Study the Relational Model? v Most widely used model. – Vendors: IBM, Informix, Microsoft, Oracle, Sybase, etc. v “Legacy.
FALL 2004CENG 351 File Structures and Data Management1 Relational Model Chapter 3.
1.1 CAS CS 460/660 Relational Model. 1.2 Review E/R Model: Entities, relationships, attributes Cardinalities: 1:1, 1:n, m:1, m:n Keys: superkeys, candidate.
Relational Data Model Ch. 7.1 – 7.3 John Ortiz Lecture 3Relational Data Model2 Why Study Relational Model?  Most widely used model.  Vendors: IBM,
The Relational Model1 ER-to-Relational Mapping and Views.
Database Management System Quiz 1. A company needs to store information about: the employees identified by EmpNo, Name, Salary and Phone; departments.
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems,1 Conceptual Design Using the Entity-Relationship (ER) Model.
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
The Relational Model Jianlin Feng School of Software SUN YAT-SEN UNIVERSITY.
ICS 421 Spring 2010 Relational Model & Normal Forms Asst. Prof. Lipyeow Lim Information & Computer Science Department University of Hawaii at Manoa 1/19/20101Lipyeow.
CMPT 258 Database Systems Final Exam Review.
COP-5725 Practice Exercises
ER & Relational: Digging Deeper R &G - Chapters 2 & 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Ch 7: Normalization-Part 1
Mapping E/R to RM, R. Ramakrishnan and J. Gehrke with Dr. Eick’s additions 1 Mapping E/R Diagrams to Relational Database Schemas Second Half of Chapter.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #4 (E-R  Relational Translation) Instructor: Chen Li.
1 The Relational Model Chapter 3. 2 Why Study the Relational Model?  Most widely used model.  Multi-billion dollar industry, $15+ bill in  Vendors:
CSC 411/511: DBMS Design Dr. Nan Wang 1 Schema Refinement and Normal Forms Chapter 19.
SQL Exercises. 1) Names of suppliers. Suppliers(sid, sname, address) Parts(pid, pname, color) Catalog(sid, pid, cost)
Assignements.
The Entity-Relationship Model
SQL – Part 2.
COP Introduction to Database Structures
Payroll Management System
The Entity-Relationship Model
Translation of ER-diagram into Relational Schema
From ER to Relational Model
Question Which of the following plays an important role in representing information about the real world in a database? Explain briefly 1)DDL.
1.(5) Describe the working process with a database system.
실습 4주차.
Question Which of the following plays an important role in representing information about the real world in a database? Explain briefly 1)DDL.
Presentation transcript:

COMP3030 Database Management System Final Review

AGENDA Ch1. Introduction Ch2. Relational Model Ch3. SQL Ch4. Advanced SQL Ch6. Database Design and the ER model Ch7. Relational Database Design Ch10. XML

CH2 EXERCISES Given two relations R1 and R2, where R1 contains N1 tuples, R2 contains N2 tuples, and N2 > N1 > 0, give the min and max possible sizes for the resulting relational algebra expressions: (1) R1UR2, (2) R1∩R2, (3) R1−R2, (4) R1×R2, (5) σa=5(R1), and (6) πa(R1) Sigma  Select rows Pi  Projection, extract columns UNION COMPATIBLE: Have same number of fields AND Have corresponding fields, taken in order from left to right, have the same domains.

CH2 EXERCISES Consider the Supplier-Parts-Catalog schema from the previous question. State what the following queries compute: Find the Supplier names of the suppliers who supply a red part that costs less than 100 dollars. This Relational Algebra statement does not return anything Find the Supplier names of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars. **Natural joins 3… because of the sequence of projection operators. Once the sid is projected, it is the only field in the set. Therefore, projecting on sname will not return anything.

Ch6 EXERCISES Consider the Supplier-Parts-Catalog schema from the previous question. State what the following queries compute: Find the Supplier ids of the suppliers who supply a red part that costs less than 100 dollars and a green part that costs less than 100 dollars.

AGENDA Ch1. Introduction Ch2. Relational Model Ch3. SQL Ch4. Advanced SQL Ch6. Database Design and the ER model Ch7. Relational Database Design Ch10. XML

CH3 EXERCISES What is the difference between a candidate key and the primary key for a given relation? What is a superkey? The primary key is the key selected by the DBA from among the group of candidate keys, all of which uniquely identify a tuple. A superkey is a set of attributes that contains a key.

CH3 EXERCISES Answer each of the following questions briefly. The questions are based on the following relational schema: Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pcttime: integer) Dept(did: integer, dname: string, budget: real, managerid: integer) 1. Give an example of a foreign key constraint that involves the Dept relation. What are the options for enforcing this constraint when a user attempts to delete a Dept tuple? Consider the following example. It is natural to require that the did field of Works should be a foreign key, and refer to Dept. CREATE TABLE Works ( eid INTEGER NOT NULL , did INTEGER NOT NULL , pcttime INTEGER, PRIMARY KEY (eid, did), FOREIGN KEY (did) REFERENCES Dept ) When a user attempts to delete a Dept tuple, There are four options:

CH3 EXERCISES 2. Write the SQL statements required to create the preceding relations, including appropriate versions of all primary and foreign key integrity constraints. CREATE TABLE Emp ( eid INTEGER, ename CHAR(10), age INTEGER, salary REAL, PRIMARY KEY (eid) ) CREATE TABLE Works ( did INTEGER, pcttime INTEGER, PRIMARY KEY (eid, did), FOREIGN KEY (did) REFERENCES Dept, FOREIGN KEY (eid) REFERENCES Emp, ON DELETE CASCADE CREATE TABLE Dept ( budget REAL, managerid INTEGER , PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp, ON DELETE SET NULL

CH3 EXERCISES 3. Define the Dept relation in SQL so that every department is guaranteed to have a manager. CREATE TABLE Dept ( did INTEGER, budget REAL, managerid INTEGER NOT NULL , PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp )

CH3 EXERCISES 4. Write an SQL statement to add John Doe as an employee with eid = 101, age = 32 and salary = 15, 000. INSERT INTO Emp (eid, ename, age, salary) VALUES (101, ’John Doe’, 32, 15000)

CH3 EXERCISES 5. Write an SQL statement to give every employee a 10 percent raise. UPDATE Emp E SET E.salary = E.salary * 1.10

CH3 EXERCISES 6. Write an SQL statement to delete the Toy department. Given the referential integrity constraints you chose for this schema, explain what happens when this statement is executed. DELETE FROM Dept D WHERE D.dname = ’Toy’ The did field in theWorks relation is a foreign key and references the Dept relation. This is the referential integrity constraint chosen. By adding the action ON DELETE CASCADE to this, when a department record is deleted, theWorks record associated with that Dept is also deleted. The query works as follows: The Dept relation is searched for a record with name = ‘Toy’ and that record is deleted. The did field of that record is then used to look in the Works relation for records with a matching did value. All such records are then deleted from the Works relation. sid name login age gpa 53831 Madayan madayan@music 11 1.8 53832 Guldu guldu@music 12 2.0

CH3 EXERCISES Consider the following schema: Suppliers(sid: integer, sname: string, address: string) Parts(pid: integer, pname: string, color: string) Catalog(sid: integer, pid: integer, cost: real) The Catalog relation lists the prices charged for parts by Suppliers. Write the following queries in SQL: 1. Find the pnames of parts for which there is some supplier. SELECT DISTINCT P.pname FROM Parts P, Catalog C WHERE P.pid = C.pid 5. Find the sids of suppliers who charge more for some part than the average cost of that part (averaged over all the suppliers who supply that part). SELECT DISTINCT C.sid FROM Catalog C WHERE C.cost > ( SELECT AVG (C1.cost) FROM Catalog C1 WHERE C1.pid = C.pid )

Ch3 EXERCISES 8. Find the sids of Suppliers who supply a red parts and a green part SELECT DISTINCT C.sid FROM Catalog C, Parts P WHERE C.pid = P.pid AND P.color = ‘Red’ INTERSECT SELECT DISTINCT C1.sid FROM Catalog C1, Parts P1 WHERE C1.pid = P1.pid AND P1.color = ‘Green’ 9. Find the sids of Suppliers who supply a red parts or a green part SELECT DISTINCT C.sid FROM Catalog C, Parts P UNION SELECT DISTINCT C1.sid FROM Catalog C1, Parts P1

AGENDA Ch1. Introduction Ch2. Relational Model Ch3. SQL Ch4. Advanced SQL Ch6. Database Design and the ER model Ch7. Relational Database Design Ch10. XML

CH6 EXERCISES A university database contains information about professors (id. by SSN) and courses (id. by courseid). Professors teach courses; each of the following situations concerns the Teaches relationship set. For each situation, draw an ER diagram that describes it (assuming no further constraints hold). Professors can teach the same course in several semesters, and each offering must be recorded. WHAT ARE THE ENTITIES? ENTITY SETS? ATTRIBUTES? RELATIONSHIP?

Professors can teach the same course in several semesters, and only the most recent such offering needs to be recorded. (Assume this condition applies in all subsequent questions.)

Every professor must teach some course. Participation constraint is total for Professor in Teaches

Every professor teaches exactly one course (no more, no less). Total participation Each Professors entity appears in at most one Teaches relationship.

Every professor teaches exactly one course (no more, no less), and every course must be taught by some professor.

Certain courses can be taught by a team of professors jointly, but it is possible that no one professor in a team can teach the course. Model this situation, introducing additional entity sets and relationship sets if necessary.

A company database needs to store information about 2.4 A company database needs to store information about employees (identified by ssn, with salary and phone as attributes), departments (identified by dno, with dname and budget as attributes), and children of employees (with name and age as attributes). Employees work in departments; each department is managed by an employee; a child must be identified uniquely by name when the parent (who is an employee; assume that only one parent works for the company) is known. We are not interested in information about a child once the parent leaves the company.

Convert ER diagram to relational schema Employee (ssn, salary, phone, mangerid) Departments (dno, dname, budget) Child (name, age, ssn) Works_in (ssn, dno)

AGENDA Ch1. Introduction Ch2. Relational Model Ch3. SQL Ch6. Database Design and the ER model Ch7. Relational Database Design Ch10. XML

CH7 EXERCISES A relation R is in third normal form if for every functional dependency of the form X  A one of the following statements is true: A Є X that is, A is a trivial functional dependency , or (1) X is a superkey, or (2) A is part of some key for R (3) A relation R is in BCNF if (1) or (2)

CH7 EXERCISES 7.2. Consider a relation R with five attributes ABCDE. You are given the following dependencies: A → B, BC → E, ED → A List all candidate keys for R. CDE, ACD, BCD Is R in 3NF? R is in 3NF because B, E and A are all parts of candidate keys. Is R in BCNF? R is not in BCNF because none of A, BC and ED contain a key. BCNF = Boyce-Codd Normal Form iff X is superkey 3NF X is superkey A is part of some key

BCNF, 3NF decomposition Canonical cover Closure 4NF

AGENDA Ch1. Introduction Ch2. Relational Model Ch3. SQL Ch6. Database Design and the ER model Ch7. Relational Database Design Ch10. XML

Give the DTD for an XML representation of the following nested-relational schema Emp = (ename, ChildrenSet setof(Children), SkillsSet setof(Skills)) Children = (name, Birthday) Birthday = (day, month, year) Skills = (type, ExamsSet setof(Exams)) Exams = (year, city)

<!DOCTYPE db [ <!ELEMENT emp (ename, children*, skills*)> <!ELEMENT children (name, birthday)> <!ELEMENT birthday (day, month, year)> <!ELEMENT skills (type, exams+)> <!ELEMENT exams (year, city)> <!ELEMENT ename( #PCDATA )> <!ELEMENT name( #PCDATA )> <!ELEMENT day( #PCDATA )> <!ELEMENT month( #PCDATA )> <!ELEMENT year( #PCDATA )> <!ELEMENT type( #PCDATA )> <!ELEMENT city( #PCDATA )> ] >

a. Find the names of all employees who have a child who has a birthday in March. for $e in /db/emp, $m in distinct($e/children/birthday/month) where $m = ’March’ return $e/ename

b. Find those employees who took an examination for the skill type “typing” in the city “Dayton”. for $e in /db/emp $s in $e/skills[type=’typing’] $exam in $s/exams where $exam/city= ’Dayton’ return $e/ename

c. List all skill types in Emp. for $t in /db/emp/skills/type return $t

Final Exam Information Ch1. Introduction Ch2. Relational Model 17 Ch3. SQL 41 Ch4. Advanced SQL Ch6. Database Design and the ER model 10 Ch7. Relational Database Design 22 Ch10. XML 10

GOOD LUCK!!