review lab 5 erd’s

47
REVIEW LAB 5 ERD’S DBS201

Upload: hedy-anthony

Post on 31-Dec-2015

42 views

Category:

Documents


0 download

DESCRIPTION

Review Lab 5 ERD’s. DBS201. How do you get to this screen in SQL?. The following statements are run. Set schema PRMIERC40 Select * From Customer Select * from Salesrep Select * from Cust_Rep. Frank Soltis is a Customer with salesrep 99. SalesRep 99 is Russell Pangborn. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Review Lab 5 ERD’s

REVIEWLAB 5ERD’S

DBS201

Page 2: Review Lab 5 ERD’s

HOW DO YOU GET TO THIS SCREEN IN SQL?

Page 3: Review Lab 5 ERD’s

The following statements are run

• Set schema PRMIERC40• Select * From Customer• Select * from Salesrep• Select * from Cust_Rep

Page 4: Review Lab 5 ERD’s

FRANK SOLTIS IS A CUSTOMER WITH SALESREP 99

Page 5: Review Lab 5 ERD’s

SALESREP 99 IS RUSSELL PANGBORN

Page 6: Review Lab 5 ERD’s

WHERE ARE FRANK SOLTIS AND RUSS PANGBORN

Page 7: Review Lab 5 ERD’s

create View PRMIERC40.CUST_REP as ( SELECT C.CUSTOMER_NUMBER, TRIM(C.FIRST_NAME) || ' ' || C.LAST_NAME AS CUST_NAME, TRIM(S.FIRST_NAME) || ' ' || S.LAST_NAME AS REP_NAME FROM PREMIERE.CUSTOMER C, PREMIERE.SALESREP S WHERE C.SALES_REP_NUMBER = S.SALES_REP_NUMBER )

Anything wrong?

Page 8: Review Lab 5 ERD’s

Duplicate key value specified. INSERT INTO PRMIERC40.CUSTOMER (SELECT * FROM PREMIERE.CUSTOMER)

Page 9: Review Lab 5 ERD’s

SELECT CUSTOMER_NUMBER, LAST_NAME, FIRST_NAME, STREET, CITY, STATE, BALANCE, SALES_REP_NUMBER FROM PREMIERE.CUSTOMER

Page 10: Review Lab 5 ERD’s

• What command is being used?

• DSPFD PRMIERC40/CUSTOMER *CST

Page 11: Review Lab 5 ERD’s
Page 12: Review Lab 5 ERD’s

LAB 5

Page 13: Review Lab 5 ERD’s

ENTITY RELATIONSHIP DIAGRAMSERDS

DBS201

Page 14: Review Lab 5 ERD’s

Why ERDs

• Documentation used to represent the database in an abstract way

• The data model can be reviewed by the end user and the person responsible for the physical database design

• Useful tool for the person creating the data model.

Page 15: Review Lab 5 ERD’s

What is an ERD

• This conceptual data model represents the data used in an organization and the relationships between the data

• It is a graphical representation of the proposed database

Page 16: Review Lab 5 ERD’s

Entities and Events

• Entities - People, places, things or concepts about which information must be recorded

• Entities as Events – Placing an order or approving a loan

• Attributes for entities, events and relationships would be things like customer names or dates on which orders were placed

• Attribute – one single valued fact about an entity that we may want to record

Page 17: Review Lab 5 ERD’s

E (Relationship)Ds

• Relationships are found between entities• An employee is in a department• A department has many employees• Business rules must be taken into account• Every employee must be in a single

department

Page 18: Review Lab 5 ERD’s

E (Relationship)Ds

• Relationships are found between entities and events

• A customer (entity) places an order (event)• A loan officer (entity) approves a loan (event)• Business rules must be taken into account• Loan example - a rule that the borrower must

have an adjusted gross income of at least half of his or her outstanding debt may be enforced

Page 19: Review Lab 5 ERD’s

An ERD should

• capture all required information• make sure data appears only once in the

database design• not include in the data model any data that is

derived from other data that is already in the data model

• arrange data in the data model in a logical manner

Page 20: Review Lab 5 ERD’s

Equivalent Terms:Relational Model

Table-Oriented DBMS

Conventional File Systems

Conceptionally Represents

Relation Table File Entity Type

Tuple Row Record Entity Instance

Attribute Column Field Property

Domain Column Type Data Type Allowable Values

Element Column Value Field Value Property Value

Page 21: Review Lab 5 ERD’s

Customer

• Customer Relation• Customer Table• Customer File• Customer Entity

Page 22: Review Lab 5 ERD’s

Last Name

• Last name attribute in Customer Relation• Last name column in Customer Table• Last name field in Customer File• Last name property of Customer Entity

Page 23: Review Lab 5 ERD’s

Phone Number

• Customer Relation phone number domain is numeric or character

• Customer Table phone number column type is numeric or character

• Customer File phone number data type is numeric or character

• Customer Entity phone number allowable values are numeric or character

Page 24: Review Lab 5 ERD’s

Phone Number is 4164915050

• Customer Relation phone number element is 4164915050 or (416) 491-5050

• Customer Table phone number column value is 4164915050 or (416) 491-5050

• Customer Table phone number field value is 4164915050 or (416) 491-5050

• Customer Table phone number property value is 4164915050 or (416) 491-5050

• A numeric field can used an edit code to get the special characters included “() – ”

Page 25: Review Lab 5 ERD’s

Last Name, First Name, Phone

• Smith, Bill, 9056668888 as a tuple in the Customer Relation

• Smith, Bill, 9056668888 as a row in the Customer Table

• Smith, Bill, 9056668888 as a record in the Customer File

• Smith, Bill, 9056668888 as an entity instance of the Customer Entity

Page 26: Review Lab 5 ERD’s

Steps in Designing an ERD

• Create Entities by identifying the people, places or events about which the end user wants to store data

• Define Attributes by determining the attributes that are essential to the system under development. For each attribute, match it with exactly one entity that it describes

Page 27: Review Lab 5 ERD’s

Steps in Designing an ERD

• Select Unique Identifier Identify the data attribute(s) that uniquely identify one and only one occurrence of each entity. Eliminate many to many relationships, and include a unique identifier (UID) and foreign keys in each entity

• Define Relationships Find the natural associations between pairs of entities using a relationship matrix. Arrange entities in rectangles and join those entities with a line

Page 28: Review Lab 5 ERD’s

Steps in Designing an ERD• Determine Optionality and Cardinality Determine the number of

occurrences of one entity for a single occurrence of the related entity.

• Name Relationships Name each relationship between entities

• Eliminate Many-toMany Relationships Many-to-many relationships cannot be implemented into database tables because each row will need an indefinite number of attributes to maintain the many-to-many relationship. Many-to-many relationships must be converted to one-to-many relationships using associative entities

• Determine Data Types Identify the data types and sizes of each attribute

Page 29: Review Lab 5 ERD’s

Case Study• Each employee may be assigned to one and only one

department. Some employees may not be assigned to any department. The employee data is stored in the employee entity.

• Each department could have many employees assigned to it. Some departments nay not have any employees assigned to them. The department data is stored in the department entity.

• Each employee may have one and only one job title. Under certain circumstances, some employees may not be assigned a job title.

Page 30: Review Lab 5 ERD’s

Case Study• Each job title may be assigned to many employees.

Some job titles may not be assigned to any employees. The job data is stored in the job entity.

• Each employee may be assigned to many projects. Sometimes an employee may be off work and is not assigned to any projects.

• Each project must be assigned to at least one employee. Some projects may have several employees assigned to them. The project data is stored in the project entity

Page 31: Review Lab 5 ERD’s

Identify Entities

• Employee• Department• Job• Project

Page 32: Review Lab 5 ERD’s

Identify Attributes

• Employee – employee_id, first_name, last_name, soc_ins_no, hire_date

• Volatile attributes• Required and Optional Attributes• Time dependant attributes• Domains

Page 33: Review Lab 5 ERD’s

Select Unique Identifier (UID)• Every entity must have unique identifying

attribute(s) called a unique identifier

• This is a single attribute or a collection of attributes that uniquely identifies one and only one instance of an entity.

• When two or more attributes are used as the unique identifier it is called a concatenated key

Page 34: Review Lab 5 ERD’s

Candidate Key• Sometimes there are several attributes that

could be the unique identifier

• For an EMPLOYEE entity we could use employee_id, social_ins_no, email_address or telephone_no

• These are all called candidate keys

Page 35: Review Lab 5 ERD’s

Candidate Keys Must:• be unique for each instance within an entity

• never be missing, incomplete or NULL for an instance

• use no attributes other than those necessary to uniquely identify an instance of an entity

Page 36: Review Lab 5 ERD’s

A Unique Identifier:• should be meaningless other than as an identifier

• should never change

• should not have a limited number of values available

• only one (UID) should be specified for each table

Page 37: Review Lab 5 ERD’s

For EMPLOYEE• Soc_ins_no is not meaningless – do you want

people knowing your personal number in the company?

• A telephone number may change

• The best choice is an arbitrarily generated employee number assigned when the employee is hired

Page 38: Review Lab 5 ERD’s

Determining relationships

DEPARTMENT EMPLOYEE

A relationship is like a verb that shows some dependency or natural association between two entities

A department contains employeesAn employee is assigned to a department

Page 39: Review Lab 5 ERD’s

Determining optionality and cardinality

TABLE A TABLE B

Each instance of Table B is related to a maximum of one and a minimum of one instance of Table A

Page 40: Review Lab 5 ERD’s

Each instance of Table A is related to zero, one or more instances of Table B

TABLE A TABLE B

Page 41: Review Lab 5 ERD’s

An employee must be in one and only one department

PK employee_id first_name

last_name soc_sec_no hire_date

job_id

FK1 department_code

PK department_codedepartment_namemanager_id

DEPARTMENT

EMPLOYEE

Page 42: Review Lab 5 ERD’s

A department may have zero, one or many employees

PK employee_id first_name

last_name soc_sec_no hire_date

job_id

FK1 department_code

PK department_codedepartment_namemanager_id

DEPARTMENT

EMPLOYEE

Page 43: Review Lab 5 ERD’s

An employee may be in zero or one department

PK employee_id first_name

last_name soc_sec_no hire_date

job_id

FK1 department_code

PK department_codedepartment_namemanager_id

DEPARTMENT

EMPLOYEE

Page 44: Review Lab 5 ERD’s
Page 45: Review Lab 5 ERD’s

Different notations are used to represent the cardinality of relationships

• 1:1• 1:M• M:N• Which one was shown with more

detail in the previous examples?

Page 46: Review Lab 5 ERD’s

No information on optionality is given with this 1:M example

PK employee_id first_name

last_name soc_sec_no hire_date

job_id

FK1 department_code

PK department_codedepartment_namemanager_id

DEPARTMENT

EMPLOYEE

M1

Page 47: Review Lab 5 ERD’s

Summary

• Entity or Event• Relationship• Optionality• Cardinality• Attributes• UID• ERD Diagrams