Entity Relationship Diagrams and Relational DBs BSAD 141 Dave Novak
Topics Covered ERD (entity relationship diagram) Data Modeling Entity –vs- Entity set Attribute –vs- Relationship Types of attributes and their domain Characteristics of relationships Degree Connectivity Existence
Modeling Why would it be necessary or useful to model the structure of a database system?
Modeling Data Model Entity-Relationship Diagram (ERD) Popular model that focuses on data
ERD ERD: Graphical blueprint for the exact design and structure of a relational DB Shows details of things organization collects data about and how those things are related
Relational Database Management System RDBMS A very popular, widely used DBMS Made possible by modern computing power – computer must a lot of processing work User does not need to understand the underlying structure of the database
Relational Databases Based on organization of data in two- dimensional tables connected via relationships Tables are linked using a common data element (attribute) that appears in both tables By linking tables via relationships users can navigate the database and extract and combine data from different tables
Relational Databases The Table is the fundamental component in a relational database All data are divided into different tables This approach provides a much easier way to manage data Data are essentially grouped by common themes and then connected by relationships in a logical manner based on the ERD
Relational Databases A relationship is a connection between two or more simple two-dimensional tables that consist of rows (entity / records) and columns (attributes / fields) of data Each entity is a unique observation and the attributes are the common characteristics that are used to describe all the entities
Relational Databases Power of an RDBs comes from ability to quickly find and bring together data stored in separate tables using queries, forms, and reports RDBs utilize the Structured Query Language (SQL) A programming language used to manage data in a RDB
Relational Databases CLASS SECTION Table
Review What is the entity set represented here? What is an example of an entity within the entity set? What are some of the attributes of the entity? Why is the first field – Employee ID – underlined?
Primary Keys Each record/entity must have a attribute (field/column) or set of attributes that uniquely identifies that specific entity from all the other entities stored in the table The unique attribute (or set of attributes) is called the primary key
Primary Keys For our purposes: ALL relational DB tables must have a primary key ALL entities within the table must have a unique primary key value The primary can be created by you Doesnt have to provide value-added information Can be a combination of fields/attributes Can be sequential numbers Can be a combination of letters and numbers Cannot be repeated or left blank for an entity
Review Key Terminology Entity Set / Table (has a name that directly relates to the primary information stored in the table) Entity / Record (this is a row in a table) Attribute / Field (this is a column in the table that describes a characteristic of the entity) Relationship (an association or linkage between two (+) tables)
Different Types of Attributes Simple –vs- composite Single valued –vs- multi-valued
Attribute Domain The collection or range of all possible allowable values an attribute may take on EmpDOB: list of all valid employee birth NumDependents: positive integers from 0 to 15 Height: positive real numbers between 36.0 and 84.0 inches. UnionMember: logical yes (true) or no (false) EmpSSN: list of valid social security numbers
Value for Each Attribute Each data attribute takes on a specific value: One particular instance of an entitys attribute e.g. Employee attributes: EmpSSNEmpDOBNumDependentsUnionmemberHeight
Relationships Entities are connected to one another via relationships The association between two or more entities We will discuss three (3) important characteristics of relationships: 1) Degree 2) Connectivity 3) Existence
Relationship Degree Loosely equal to the number of entities involved in a particular relationship Could also view this more simplistically as the number of tables linked by each relationship 1= Unary (aka recursive) 2= Binary 3= Ternary
Relationship Connectivity Three types of connectivity relationships are used to describe associations between the entities in different tables How are entities logically connected in the DB? 1) One-to-Many 2) Many-to-Many 3) One-to-One
Relationship Connectivity Employee #1 Employee #2 Employee #3 Company Vehicle #1 Company Vehicle #2 Company Vehicle #3 1:1 This relationship connectivity shows that each employee can be associated with only one company vehicle and that each company vehicle can be associated with only one employee. In this case, the RDB will be structured in a manner that matches a specific employee to a specific company car each and every day. Each car would be explicitly linked to a particular employee 1 1 Employee Vehicle
Relationship Connectivity Employee #1 Employee #2 Employee #3 Employee #4 Accounting Department Human Resources Dept M This relationship connectivity shows that each employee can be associated with only one organizational department (employees cannot simultaneously be assigned to, or work in, two different departments). However, each department can be staffed with many / multiple employees 1 1:M Employee Department
Relationship Connectivity Customer #1 Customer #2 Customer #3 Product #1 Product #2 Product #3 Product #4 M:M This relationship connectivity shows that customer can be associated with many different products and that each product can be associated with many different customers This relationship will be modeled in a special way!! M M Customer Product
Relationship Connectivity One-to-Many relationship A professor can teach many different class sections Each class section is taught by only one professor A customer account can contain many invoices Each invoice is associated with only one customer account
Relationship Connectivity One-to-Many example Relationship is labeled 1:M Professor Class Section Each teaches M Each is taught by 1 Read: left to right on the top of the line Read: right to left on the bottom of the line
Relationship Connectivity Many-to-Many relationship Employee has many job skills Each job skill can be learned by many employees Student can take many class section Each class section can be taken by many students
Relationship Connectivity One-to-One relationship Employee is assigned to one Company Car Each Company Car is assigned to only one employee
Relationship Connectivity Why does this matter? Logically the relationship needs to make sense Dont want the database functionality to allow relationships that are not feasible or practical From a practical perspective, the foreign key always goes on the many side of the relationship – need to get this correct Special implementation of M:M & 1:1
Relationship Existence Denotes whether participation in a relationship is required (or not), and the minimum and maximum number of entities that participate in a particular relationship Mandatory: The existence of one entity depends on the existence of another entity Optional: The existence of one entity is independent of the existence of another entity
Mandatory Relationship Existence Example Employee #1 Employee #2 Employee #3 Employee #4 Accounting Department Human Resources Dept 1:M The existence of an instance of employee (an individual employee) depends on the existence of a department where the employee will be assigned In this case, the entity set Department will need to be populated before the entity set Employee is populated Employee Department
Optional Relationship Existence Example Employee #1 Employee #2 Employee #3 Employee #4 Accounting Department Human Resources Dept 1:M The existence of an instance of employee (an individual employee) is independent of the existence of a department where the employee will be assigned In this case, the entity instance employee #4 can be created without the need to assign that employee to a department Employee Department
Relationship Existence Example CRN 5099 CRN 5060 CRN 5061 CRN 5062 BSAD 141 BSAD 173 BSAD 120 1:M Discuss… Course Section Course
Investigating Relationships 1:M relationship This relationship states that each supplier can provide (or distributes) many different products (1, 2, … n), but each individual product is provided by (or distributed by) only one supplier In this case, the structure would not allow a specific product to be provided by (or distributed by) to more than one supplier Supplier Product Can provide M Is provided by 1
Supplier Product Can provide M Is provided by 1 Supplier IDCompany NameContact NameContact TitleAddressCityRegionPostal CodeCountryPhone 2 New Orleans Cajun DelightsShelley Burke Order Administrator P.O. Box New OrleansLA70117USA (100) Grandma Kelly's HomesteadRegina Murphy Sales Representative707 Oxford Rd.Ann ArborMI48104USA (313) Bigfoot BreweriesCheryl Saylor Regional Account Rep th Avenue Suite 210BendOR97101USA (503) Supplier Table
Supplier Product Can provide M Is provided by 1 Product Table Product IDProduct Name Supplier IDCategory Quantity Per Unit Unit Price Units In Stock Units On Order Reorder Level 7 Uncle Bob's Organic Dried Pears3Seafood lb pkgs.$ Northwoods Cranberry Sauce3 Condiment s oz jars$ Chef Anton's Cajun Seasoning2 Condiment s oz jars$ Chef Anton's Gumbo Mix2 Condiment s36 boxes$ Sasquatch Ale16Beverages oz bottles$ Steeleye Stout16Beverages oz bottles$ Louisiana Fiery Hot Pepper Sauce2 Condiment s oz bottles$ Louisiana Hot Spiced Okra2 Condiment s oz jars$
Product Table Product IDProduct Name Supplier IDCategory Quantity Per Unit Unit Price Units In Stock Units On Order Reorder Level 7 Uncle Bob's Organic Dried Pears3Seafood lb pkgs.$ Northwoods Cranberry Sauce3 Condiment s oz jars$ Chef Anton's Cajun Seasoning2 Condiment s oz jars$ Chef Anton's Gumbo Mix2 Condiment s36 boxes$ Sasquatch Ale16Beverages oz bottles$ Steeleye Stout16Beverages oz bottles$ Louisiana Fiery Hot Pepper Sauce2 Condiment s oz bottles$ Louisiana Hot Spiced Okra2 Condiment s oz jars$ Notice that SupplierID is not an attribute of Product – it is the PK of the Supplier table and will be the FK (to the Supplier table) in the Product table
Product IDProduct Name Supplier IDCategory Quantity Per Unit Unit Price Units In Stock Units On Order Reorder Level 7 Uncle Bob's Organic Dried Pears3Seafood lb pkgs.$ Northwoods Cranberry Sauce3 Condiment s oz jars$ Chef Anton's Cajun Seasoning2 Condiment s oz jars$ Chef Anton's Gumbo Mix2 Condiment s36 boxes$ Sasquatch Ale16Beverages oz bottles$ Steeleye Stout16Beverages oz bottles$ Louisiana Fiery Hot Pepper Sauce2 Condiment s oz bottles$ Louisiana Hot Spiced Okra2 Condiment s oz jars$ Supplier IDCompany NameContact NameContact TitleAddressCityRegionPostal CodeCountryPhone 2 New Orleans Cajun DelightsShelley Burke Order Administrator P.O. Box New OrleansLA70117USA (100) Grandma Kelly's HomesteadRegina Murphy Sales Representative707 Oxford Rd.Ann ArborMI48104USA (313) Bigfoot BreweriesCheryl Saylor Regional Account Rep th Avenue Suite 210BendOR97101USA (503)
Investigating Relationships The 1:M relationship example The Supplier ID attribute in the Product table is a foreign key that links the individual products in the Product table to a specific supplier in the Supplier table
Learning Outcomes 1. Describe the differences between ERDs and DFDs 2. Describe importance of relational DBs 3. Familiar with all terminology: tables, relationships, entities, attributes, keys, etc 4. Understand how to implement a relationship 5. Discuss / define the 3 characteristics of relationships