Sunteți pe pagina 1din 18

Exercises

E-R Diagrams
1. Construct an E-R Diagram for a hospital with a set of patients and a set of medical doctors. A log of the various conducted tests is associated with each patient. Construct the normalized relations from this ER diagram.

2. Construct an E-R Diagram for a car insurance company with a set of customers, each of whom owns a number of cars. Each car has a number of accidents associated with it. Construct the normalized relations from this ER diagram.

3. Consider the following E-R Diagram: Represent the diagram in the relational model by relations (tables). 4.Suppose we have a database consisting of the following 3 relations : FREQUENTS ( DRINKER, BAR ) SERVES ( BAR, BEER ) LIKES ( DRINKER, BEER ) The first relation indicates the bars each drinker visits, the second tells what beers each bar serves, and the last indicates which beers each drinker likes to drink. Draw an E-R Diagram for the given relations.

SQL Star International

6.1

Exercises

5. An education database contains information about an in-house company education training scheme. For each training course, the database contains details of all prerequisite courses and all offerings for that course; and for each offering it contains details of all teachers and all student enrollments for that offering. The database also contains information about employees. The relevant relations are as follows, in outline : COURSE ( COURSE#, TITLE ) PREREQ (SUP_COURSE#, SUB_COURSE# ) OFFERING ( COURSE#, OFF#, OFFDATE, LOCATION ) TEACHER ( COURSE#, OFF#, EMP# ) ENROLLMENT ( COURSE#, OFF#, EMP#, GRADE ) EMPLOYEE ( EMP#, ENAME, JOB ) The meaning of the PREREQ relation is that the superior course (SUP_COURSE#) has the subordinate course (SUB_COURSE#) as an immediate prerequisite. Draw an E-R Diagram for this education database.

SQL Star International

6.2

Exercises

Normalization
6. Consider the table: Course_No.
CIS200 CIS220 CIS220 CIS450 CIS480 CIS480

Course_Name

Student_Name
John Warner Tim Hoffman Jenny Lin Alice Chalmers John Warner Jenny Lin

Address
23, Main St. 87, River Rd. 18, Wind Circle 5483, Ocean Bld. 23, Main St. 18, Wind Circle

Credits
5 5 5 5 5 5

Information Systems Information Systems Information Systems System Ana. and Des. Communication N/ws. Communication N/ws.

a) Does the relation contain any repeating groups ? Explain. b) In what normal form is this relation ? c) If the relation is not already in third normal form, develop new relations that meet the requirements of 3NF. 7. The following figure is an un-normalized representation of a

collection of information to be recorded in a company personnel database. The figure is intended to be read as follows : The company has a set of departments. Each department has a set of employees, a set of projects, and a set of offices. Each employee has a job history (set of jobs the employee has held). For each such job, the employee also has a salary history (set of salaries received while employed on that job). Each office has a set of phones.

SQL Star International

6.3

Exercises

The database is to contain the following information : For each department : department number ( unique ), budget, and the department managers employee number. For each employee : employee number ( unique ), current project number, office number, and phone number; also, title of each job the employee has held, plus date and salary for each distinct salary received in that job. For each project : project number ( unique ) and budget. For each office : office number ( unique ), area in square feet, and numbers ( unique ) of all phones in that office. State any assumptions you make concerning Design an appropriate set of normalized relations to represent this information. the dependencies involved.

Normalization - A sample example


Normalisation is defined briefly, but accurately in the following statement: The Key, the Whole Key and Nothing but the Key! Typically, the literature on normalization covers many levels of

SQL Star International

6.4

Exercises normalization, 9 is not uncommon, but this seems to me to be a race amongst academics to identify as many levels as possible, in 99 cases out of 100, 3 levels of normalization are all that is required. 1st Normal Form: Converting an un-normalized data structure, such as a report or an order form into 1st Normal Form (1NF) is commonly referred to as removing repeating groups, but also may involve removing complex groups, such as the Address Group described in rule 2. The aim is to ensure that each item is atomic. 2nd Normal Form: Converting a 1NF data structure into 2nd Normal Form (2NF) involves looking at each non-primary key attribute and ensuring that it depends on the whole of the key and not just part of it. 3rd Normal Form: Converting a 2NF data structure into 3rd Normal Form (3NF) involves looking at the interrelationships between non key attributes to see if any non key attributes depend only on each other. This is all best described by looking at an example. Consider the following table which has been built up by an order entry clerk. However, this seems to be a clumsy approach and results in a three part key consisting of Cust#, Ord# and Part#. A simpler approach is to separate the repeating groups out into separate tables. Step 1: remove the repeating group of orders CUSTOMERS(Customer_Number, Customer_Name) SQL Star International 6.5

Exercises ORDERS(Order_Number, (Part_Number, Part_Description, Supplier_Name)) Step 2: remove the repeating group of parts CUSTOMERS(Customer_Number, Customer_Name) ORDERS(Order_Number, Customer_Number*, Order_Date) ORDER_PARTS(Part_Number, Part_Quantity, Part_Price, Supplier_Number, Supplier_Name) The structure is now in 1NF, since there are no repeating or complex group items (each item depends on the key). The next step is to convert the structure into 2NF, by examining each non primary key attribute to ensure that each depends on the whole of the key. The CUSTOMERS and ORDERS tables each have a single column making up their primary key and are therefore by definition in 2NF. However, looking at the ORDER_PARTS table, it can be seen that Part_Description, Part_Price, Supplier_Number and Supplier Name only depend on Part_Number, i.e. their values are the same regardless of Order_Number. (Part_Quantity depends on the whole of the key since different quantities can appear on different orders.) To convert to 2NF, a separate table is created for part descriptions, prices, and supplier details Order_Number*, Part_Description, Part_Quantity, Part_Price, Supplier_Number, Customer_Number*, Order_Date,

SQL Star International

6.6

Exercises CUSTOMERS(Customer_Number, Customer_Name) ORDERS(Order_Number, Customer_Number*, Order_Date) ORDER_PARTS(Part_Number, Order_Number*, Part_Quantity) PARTS(Part_Number, Part_Description, Part_Price, Supplier_Number, Supplier_Name) The structures are now in 2NF, since every non-primary key attribute depends on the whole of the key. The next step is to convert the structure into 3NF by ensuring that each non-primary key attribute depends on nothing, but the key. The CUSTOMERS table is patently in 3NF, because there is no nonprimary key attribute for Customer_Name to depend on. The ORDERS table is in 3NF, because there is no dependency between Order_Date and Customer_Number (a customer can place different orders on different dates). The ORDER_PARTS table is in 3NF, because the quantity ordered is dependent on both the order number and the part number. Looking however at the PARTS table it can be seen that the Supplier_Name attribute depends on the Supplier_Number and has nothing to do with the part number. To convert the structure into 3 NF, a separate table is created containing supplier details. CUSTOMERS(Customer_Number, Customer_Name) ORDERS(Order_Number, Customer_Number*, Order_Date) ORDER_PARTS(Part_Number, Order_Number*, Part_Quantity) PARTS(Part_Number, Supplier_Number*, Part_Description, Part_Price) SUPPLIERS(Supplier_Number, Supplier_Name)

SQL Star International

6.7

Exercises

Sample Example Of E-R Diagram


Company: Organized into Departments, Each Department has a name, no and manager who manages the department. The Company keeps track of the date that employee managing the department. A Department may have a Several locations. Department : A Department controls a number of Projects each of which has a unique name , no and a single Location. Employee : Name, Age, Gender, BirthDate, SSN, Address, Salary. An Employee is assigned to one department, may work on several projects which are not controlled by the department. Track of the number of hours per week is also controlled. Keep track of the dependents of each employee for insurance policies : We keep each dependant first name, gender, Date of birth and relationship to the employee.

SQL Star International

6.8

Exercises

Example Manage: Department and Employee Partial Participation Relation Attribute : StartDate. Works For: Department and Employee

SQL Star International

6.9

Exercises Total Participation Control : Department , Project Partial Participation from Department Total Participation from Project Control Department is a RKA. Supervisor : Employee, Employee Partial and Recursive Works On : Project , Employee Total Participation Hours Worked is a RKA. Dependants of: Employee , Dependant Dependant is a Weaker Dependant is Total , Employee is Partial.

SQL Star International

6.10

Exercises

Summary of Conceptual Design


Conceptual design follows requirements analysis. It yields a high-level description of data to be stored. ER model is popular for conceptual design. Its constructs are expressive, close to the way people think about their applications. Basic constructs: entities, relationships, and attributes (of entities and relationships). Some additional constructs are: weak entities, ISA hierarchies, and aggregation. Note: There are many variations on ER model.

Summary of ER
Several kinds of integrity constraints can be expressed in the ER model:

SQL Star International

6.11

Exercises key constraints, participation constraints, and overlap/ covering

constraints for ISA hierarchies. Some foreign key constraints are also implicit in the definition of a relationship set. Some of these constraints can be expressed in SQL only if we use general CHECK constraints or assertions. Some constraints (notably, functional dependencies ) cannot be expressed in the ER model. Constraints play an important role in determining the best database design for an enterprise. ER design is subjective . There are often many ways to model a given scenario! Analyzing alternatives can be tricky, especially for a large enterprise. Common choices include: Entity vs. attribute, entity vs. relationship, binary or n- ary relationship, whether or not to use ISA hierarchies, and whether or not to use aggregation. Ensuring good database design: resulting relational schema should be analyzed and refined further. FD information and normalization techniques are especially useful.

Case Studies
1. Prescriptions-R-X chain

SQL Star International

6.12

Exercises

The Prescriptions-R-X chain of pharmacies has offered to give you a free lifetime supply of medicines if you design its database. Given the rising cost of health care, you agree. Here's the information that you gather: Patients are identified by an SSN, and their names, addresses, and ages must be recorded. Doctors are identified by an SSN. For each doctor, the name, specialty, and years of experience must be recorded. Each pharmaceutical company is identified by name and has a phone number. For each drug, the trade name and formula must be recorded. Each drug is sold by a given pharmaceutical company, and the trade name identifes a drug uniquely from among the products of that company. If a pharmaceutical company is deleted, you need not keep track of its products any longer. Each pharmacy has a name, address, and phone number. Every patient has a primary physician. Every doctor has at least one patient. Each pharmacy sells several drugs and has a price for each. A drug could be sold at several pharmacies, and the price could vary from one SQL Star International 6.13

Exercises pharmacy to another. Doctors prescribe drugs for patients. A doctor could prescribe one or more drugs for several patients, and a patient could obtain prescriptions from several doctors. Each prescription has a date and a quantity associated with it. You can assume that if a doctor prescribes the same drug for the same patient more than once, only the last such prescription needs to be stored. Pharmaceutical companies have long-term contracts with pharmacies. A pharmaceutical company can contract with several pharmacies, and a pharmacy can contract with several pharmaceutical companies. For each contract, you have to store a start date, an end date, and the text of the contract. Pharmacies appoint a supervisor for each contract. There must always be a supervisor for each contract, but the contract supervisor can change over the lifetime of the contract. 1. Draw an ER diagram that captures the above information. Identify any constraints that are not captured by the ER diagram. 2. How would your design change if each drug must be sold at a fixed price by all pharmacies? 3. How would your design change if the design requirements change as follows: If a doctor prescribes the same drug for the same patient more than once, several such prescriptions may have to be stored.

SQL Star International

6.14

Exercises

2. Dane County Airport


Computer Sciences Department frequent have been complaining to

Dane County Airport officials about the poor organization at the airport. As a result, the officials have decided that all information related to the airport should be organized using a DBMS, and you've been hired to design the database. Your first task is to organize the information about all the airplanes that are stationed and maintained at the airport. The relevant information is as follows: Every airplane has a registration number, and each airplane is of a specic model. The airport accommodates a number of airplane models, and each model is identified by a model number (e.g., DC-10) and has a capacity and a weight. A number of technicians work at the airport. You need to store the name, SSN, address, phone number, and salary of each technician. Each technician is an expert on one or more plane model(s), and his or her expertise may overlap with that of other technicians. This information about technicians must also be recorded. Traffic controllers must have an annual medical examination. For each Traffic controller, you must store the date of the most recent exam. All airport employees (including technicians) belong to a union. You

SQL Star International

6.15

Exercises must store the union membership number of each employee. You can assume that each employee is uniquely identified by the social security number. The airport has a number of tests that are used periodically to ensure that air-planes are still airworthy. Each test has a Federal Aviation Administration (FAA) test number, a name, and a maximum possible score. The FAA requires the airport to keep track of each time that a given airplane is tested by a given technician using a given test. For each testing event, the information needed is the date, the number of hours the technician spent doing the test, and the score that the airplane received on the test.

1. Draw an ER diagram for the airport database. Be sure to indicate the various attributes of each entity and relationship set; also specify the key and participation constraints for each relationship set. Specify any necessary overlap and covering constraints as well (in English). 2. The FAA passes a regulation that tests on a plane must be conducted by a technician who is an expert on that model. How would you express this constraint in the ER diagram? If you cannot express it, explain briefly.

3. University Database

SQL Star International

6.16

Exercises

Consider the following information about a university database: Professors have an SSN, a name, an age, a rank, and a research specialty. Projects have a project number, a sponsor name (e.g., NSF), a starting date, an ending date and a budget. Graduate students have an SSN, a name, an age and a degree program (e.g., M.S. or Ph.D.). Each project is managed by one professor (known as the project's principal investigator). Each project is worked on by one or more professors (known as the project's co-investigators). Professors can manage and/or work on multiple projects. Each project is worked on by one or more graduate students (known as the project's research assistants). When graduate students work on a project, a professor must supervise their work on the project. Graduate students can work on multiple projects, in which case they will have a (potentially different) supervisor for each one. Departments have a department number, a department name and a main office. Departments have a professor (known as the chairman) who runs the department. Professors work in one or more departments, and for each department that they work in, a time percentage is associated with their job. Graduate students have one major department in which they are

SQL Star International

6.17

Exercises working on their degree. Each graduate student has another, more senior graduate student (known as a student advisor) who advises him or her on what courses to take.

Design and draw an ER diagram that captures the information about the university. Use only the basic ER model here, that is, entities, relationships, and attributes. Be sure to indicate any key and participation constraints.

SQL Star International

6.18

S-ar putea să vă placă și