COP-5725 Practice Exercises

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.
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.
Tallahassee, Florida, 2014 COP4710 Database Systems Relational Model Fall 2014.
The Entity-Relationship Model
Database Management Systems, R. Ramakrishnan and J. Gehrke1 The Entity-Relationship Model Chapter 2.
(Uses slides from Fernando Farfan and Eduardo J. Ruiz
Entity-Relationship Models
Book Chapter 3 (part 2 ) From ER to Relational Model.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Fall 2005 ICS184/EECS116 – Notes 08 1 ICS 184/EECS116: Introduction to Data Management Lecture Note 8 SQL: Structured Query Language -- DDL.
Overview Begin 6:00 Quiz15 mins6:15 Review Table Terms25 mins6:40 Short Break10 mins6:50 SQL: Creating Tables60 mins7:50 Break10 mins8:00 Lab – Creating.
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.
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.
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,
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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  Recent.
1 Translation of ER-diagram into Relational Schema Prof. Sin-Min Lee Department of Computer Science.
1 CSE 480: Database Systems Lecture 5: Relational Data Model.
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.
1 ICS 184: Introduction to Data Management Lecture Note 11: Assertions, Triggers, and Index.
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.
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.
Dec 8, 2003Murali Mani Constraints B term 2004: lecture 15.
Mapping E/R Diagrams to Relational Database Schemas
CMPT 258 Database Systems The Relationship Model PartII (Chapter 3)
Lecture 3 Book Chapter 3 (part 2 ) From ER to Relational.
COMP3030 Database Management System Final Review
IS6145 Database Analysis and Design Lecture 4: Fine-Granular Design-Specific ER Modelling Rob Gleasure
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
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.
Constraints and Views Chap. 3-5 continued (7 th ed. 5-7)
SCUHolliday - coen 1789–1 Schedule Today: u Constraints, assertions, triggers u Read Sections , 7.4. Next u Embedded SQL, JDBC. u Read Sections.
Database Management Systems 3ed, R. Ramakrishnan and J. Gehrke1 The Relational Model Chapter 3.
1 CS122A: Introduction to Data Management Lecture #5 (E-R  Relational, Cont.) Instructor: Chen Li.
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:
IS6145 Database Analysis and Design Lecture 4: Fine-Granular Design-Specific ER Modelling Rob Gleasure
Assignements.
SQL – Part 2.
COP Introduction to Database Structures
SQL: Schema Definition and Constraints Chapter 6 week 6
IS6145 Database Analysis and Design Lecture 5: Enhanced Entity-Relationship (EER) Modeling Rob Gleasure
Translation of ER-diagram into Relational Schema
From ER to Relational Model
Practice of ER modeling
Question Which of the following plays an important role in representing information about the real world in a database? Explain briefly 1)DDL.
Question 01 A company database needs to store information about employees (identified by NIC, with salary and phone as attributes), departments (identified.
Translation of ER-diagram into Relational Schema
IS6125/IS6145 Database Analysis and Design Lecture 4: Fine-Granular Design-Specific ER Modelling Rob Gleasure
IS6125 Database Analysis and Design Lecture 6: Fine-Granular Design-Specific ER Modelling Rob Gleasure
IS6145 Database Analysis and Design Lecture 5: Enhanced Entity-Relationship (EER) Modeling Rob Gleasure
IS6125 Database Analysis and Design Lecture 7: Enhanced Entity-Relationship (EER) Modeling Rob Gleasure
Database Systems (資料庫系統)
Database Design: Relational Model
ISC321 Database Systems I Chapter 4: SQL: Data definition, Constraints, and Basic Queries and Updates Fall 2015 Dr. Abdullah Almutairi.
Session - 6 Sequence - 1 SQL: The Structured Query Language:
Presentation transcript:

COP-5725 Practice Exercises Chapter 2: Database Design Chapter 3: Relational Model M. Amanda Crick

Exercise 2.4 Problem 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).

Exercise 2.4 Problem 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. Draw an ER diagram that captures this information.

Exercise 2.4 Solution First, we shall design the entities and relationships. “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.”

Exercise 2.4 Solution dname salary phone dno ssn budget Manages Employees Departments Dependent Works_In Child name age

Exercise 2.4 Solution Now, we will design the constraints. “…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.”

Exercise 2.4 Solution dname salary phone dno ssn budget Manages Employees Departments Dependent Dependent Works_In Child Child name name age

Exercise 2.8 Problem Although you always wanted to be an artist, you ended up being an expert on databases because you love to cook data and you somehow confused database with data baste. Your old love is still there, however, so you set up a database company, ArtBase, that builds a product for art galleries. The core of this product is a database with a schema that captures all the information that galleries need to maintain.

Exercise 2.8 Problem Galleries keep information about artists, their names (which are unique), birthplaces, age, and style of art. For each piece of artwork, the artist, the year it was made, its unique title, its type of art (e.g., painting, lithograph, sculpture, photograph), and its price must be stored. Pieces of artwork are also classified into groups of various kinds, for example, portraits, still lifes, works by Picasso, or works of the 19th century; a given piece may belong to more than one group. Each group is identified by a name (like those just given) that describes the group.

Exercise 2.8 Problem Finally, galleries keep information about customers. For each customer, galleries keep that person’s unique name, address, total amount of dollars spent in the gallery (very important!), and the artists and groups of art that the customer tends to like. Draw the ER diagram for the database.

Exercise 2.8 Solution Like before, we begin with the entities and relationships. “…artists, their names (which are unique), birthplaces, age, and style of art.” “For each piece of artwork, the artist, the year it was made, its unique title, its type of art … and its price must be stored.”

Exercise 2.8 Solution “Pieces of artwork are also classified into groups of various kinds, … Each group is identified by a name (like those just given) that describes the group. “ For each customer, galleries keep that person’s unique name, address, total amount of dollars spent in the gallery (very important!), and the artists and groups of art that the customer tends to like.

Exercise 2.8 Solution type year name title price Like_Group Classify Artwork Like_Artist Paints Customer Artist style address name cust_id birthplace age amount name

Exercise 2.8 Solution Now we look at constraints. Although not explicitly mentioned in the problem, we assume that each piece of artwork had to be painted by an artist. We also assume that each piece of artwork was created by exactly one artist.

Exercise 2.8 Solution type year name title price Like_Group Classify Artwork Like_Artist Paints Customer Artist style address name cust_id birthplace age amount name

Exercise 2.8 Solution Suppose we had several piece of artwork with the same title, and we told them apart by artist? Example: “What is Love?” by Cheryl D, “What is Love?” by Joe Brown, etc.

Exercise 2.8 Solution type year name title title price Like_Group Classify Group Artwork Artwork Like_Artist Paints Paints Customer Artist style address name cust_id birthplace age amount name

Exercise 3.14 Problem Consider the scenario from Exercise 2.4, where you designed an ER diagram for a company database. Write SQL statements to create the corresponding relations and capture as many of the constraints as possible. If you cannot capture some constraints, explain why.

Exercise 3.14 ER Diagram from Exercise 2.4 dname salary phone dno ssn budget Manages Employees Departments Dependent Dependent Works_In Child Child name name age

Exercise 3.14 Solution First we begin with the entities “Employees” and “Departments. Translating these to SQL is straightforward.

Exercise 3.14 Solution CREATE TABLE Employees( ssn CHAR(10), salary CREATE TABLE Employees( ssn CHAR(10), sal INTEGER, phone CHAR(13), PRIMARY KEY (ssn) ) CREATE TABLE Departments ( dno INTEGER, budget INTEGER, dname CHAR(20), PRIMARY KEY (dno) ) phone ssn Employees dname dno budget Departments

Exercise 3.14 Solution Next, we translate the relationships, Manages and Dependents. We translate each these to a table mapping one entity to another. We also use foreign constraints to make sure every row in the relationship tables refers only to rows that exist in the entity tables.

Exercise 3.14 Solution CREATE TABLE Works_in( ssn CHAR(10), dname salary phone dno ssn budget Manages Employees Departments Works_In CREATE TABLE Works_in( ssn CHAR(10), dno INTEGER, PRIMARY KEY (ssn, dno), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (dno) REFERENCES Departments) CREATE TABLE Manages ( ssn CHAR(10), dno INTEGER, PRIMARY KEY (dno), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (dno) REFERENCES Departments)

Exercise 3.14 Solution Why did we make dno the primary key for Manages? Since each department can have at most one manager, each dno can appear at most once in the Manages table, making it a key for Manages. Note that if we had made (ssn, dno) the key for Manages, a department could have more than one Manager.

Exercise 3.14 Solution Finally, we translate the weak entity “Child” and its corresponding relationship “Dependent”

Exercise 3.14 Solution CREATE TABLE Dependents( ssn CHAR(10), salary phone ssn Employees CREATE TABLE Dependents( ssn CHAR(10), name CHAR(10), age INTEGER, PRIMARY KEY (ssn, name), FOREIGN KEY (ssn) REFERENCES Employees, ON DELETE CASCADE ) Dependent Dependent Child Child name name age

Exercise 3.18 Problem Write SQL statements to create the corresponding relations to the ER diagram you designed for Exercise 2.8. If your translation cannot capture any constraints in the ER diagram, explain why.

Exercise 3.18 ER Diagram from Exercise 2.8 type year name title price Like_Group Classify Group Artwork Like_Artist Paints Customer Artist style address name cust_id birthplace age amount name

Exercise 3.18 Solution The entities are translated similarly to Exercise 3.4. Since these are fairly simple, we shall skip them. Now, we shall translate the relationships.

Exercise 3.18 Solution CREATE TABLE Like Group ( name CHAR(20), cust name CHAR(20), PRIMARY KEY (name, cust_name), FOREIGN KEY (name) REFERENCES Group, FOREIGN KEY (cust name) REFERENCES Customer) Group Customer address cust_id amount name

Exercise 3.18 Solution CREATE TABLE Like Artist ( name CHAR(20), Customer Artist style address name cust_id birthplace age amount name CREATE TABLE Like Artist ( name CHAR(20), cust name CHAR(20), PRIMARY KEY (name, cust name), FOREIGN KEY (name) REFERENCES Artist, FOREIGN KEY (cust name) REFERENCES Customer)

Exercise 3.18 Solution CREATE TABLE Artwork Paints( title CHAR(20), type year title price Artwork CREATE TABLE Artwork Paints( title CHAR(20), artist name CHAR(20), type CHAR(20), price INTEGER, year INTEGER, PRIMARY KEY (title), FOREIGN KEY (artist name) REFERENCES Artist) Paints Artist style name birthplace age

Exercise 3.18 Solution CREATE TABLE Classify ( title CHAR(20), type year name title price Classify Group Artwork Paints CREATE TABLE Classify ( title CHAR(20), name CHAR(20), PRIMARY KEY (title, name), FOREIGN KEY (title) REFERENCES Artwork_Paints, FOREIGN KEY (name) REFERENCES Group )

Exercise 3.8 Problem 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)

Exercise 3.8 Problem 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) 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?

Exercise 3.8 Solution for (1) An example of a foreign constraint that involves Dept is: CREATE TABLE Works ( eid INTEGER NOT NULL , did INTEGER NOT NULL , pcttime INTEGER, PRIMARY KEY (eid, did), UNIQUE (eid), FOREIGN KEY (did) REFERENCES Dept )

Exercise 3.8 Solution for (1) Furthermore, when a user attempts to delete a tuple from Dept, we can also delete all Works tuples that refer to it. disallow the deletion of the Dept tuple if some Works tuple refers to it. for every Works tuple that refers to it, set the did field to the did of some (existing) ’default’ department. for every Works tuple that refers to it, set the did field to null.

Exercise 3.8 Problem 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) Write the SQL statements required to create the preceding relations, including appropriate versions of all primary and foreign key integrity constraints.

Exercise 3.8 Solution for (2) Emp(eid: integer, ename: string, age: integer, salary: real) Works(eid: integer, did: integer, pcttime: integer) CREATE TABLE Emp ( eid INTEGER, ename CHAR(10), age INTEGER, salary REAL, PRIMARY KEY (eid) ) CREATE TABLE Works ( eid INTEGER, did INTEGER, pcttime INTEGER, PRIMARY KEY (eid, did), FOREIGN KEY (did) REFERENCES Dept, FOREIGN KEY (eid) REFERENCES Emp, ON DELETE CASCADE)

Exercise 3.8 Solution for (2) Dept(did: integer, dname: string, budget: real, managerid: integer) CREATE TABLE Dept ( did INTEGER, budget REAL, managerid INTEGER , PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp, ON DELETE SET NULL)

Exercise 3.8 Example of a Solution for (3) Problem 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) Define the Dept relation in SQL so that every department is guaranteed to have a manager. Example of a Solution for (3) CREATE TABLE Dept ( did INTEGER, budget REAL, managerid INTEGER NOT NULL , PRIMARY KEY (did), FOREIGN KEY (managerid) REFERENCES Emp)

Exercise 3.8 Solution for (4) Problem 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) Write an SQL statement to add John Doe as an employee with eid=101, age=32 and salary=15,000. Solution for (4) INSERT INTO Emp (eid, ename, age, salary) VALUES (101, ’John Doe’, 32, 15000)

Exercise 3.8 Solution for (5) Problem 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) Write an SQL statement to give every employee a 10 percent raise. Solution for (5) UPDATE Emp E SET E.salary = E.salary * 1.10

Exercise 3.8 Problem 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) 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’

Exercise 3.8 Solution for (6) DELETE FROM Dept D WHERE D.dname = ’Toy’ These are the example integrity constraints that affect Dept. CREATE TABLE Works ( … FOREIGN KEY (did) REFERENCES Dept, …) Since the action to take on deletion was not specified, the database takes no action by default. That is, it rejects the deletion.

Exercise 3.8 Solution for (6) What other actions can the system take on deleting a Dept tuple? What are the pros and cons of each action? On delete set null On delete set default On delete cascade

This is the end of the lecture! I hope you enjoyed it.