Relationships as Primary & Foreign Keys

Slides:



Advertisements
Similar presentations
ER Modeling Case Studies
Advertisements

Relational Database. Relational database: a set of relations Relation: made up of 2 parts: − Schema : specifies the name of relations, plus name and type.
SQL Lecture 10 Inst: Haya Sammaneh. Example Instance of Students Relation  Cardinality = 3, degree = 5, all rows distinct.
Ch5: ER Diagrams - Part 1 Much of the material presented in these slides was developed by Dr. Ramon Lawrence at the University of Iowa.
ER to Relational Translation COMSATS INSTITUTE OF INFORMATION TECHNOLOGY, VEHARI.
Agenda for Week 1/31 & 2/2 Learn about database design
Sunday, June 28, Entity-Relationship and Enhanced Entity-Relationship Conceptual Data Models (Chapters 6 & Section 7.1)
Database Design Chapter 2. Goal of all Information Systems  To add value –Reduce costs –Increase sales or revenue –Provide a competitive advantage.
Entity Relationship Diagram Farrokh Alemi Ph.D. Francesco Loaiza, Ph.D. J.D. Vikas Arya.
Introduction to Accounting Information Systems
Objectives for Week (1/29 & 1/31)  Know how to read, understand, and create a database model using a modeling tool - ERD’s.  Know the process of completing.
Modeling Relationships Farrokh Alemi, Ph.D. Updated by Janusz Wojtusiak, Fall 2009.
Relational databases and third normal form As always click on speaker notes under view when executing to get more information!
1 Relational Databases and SQL. Learning Objectives Understand techniques to model complex accounting phenomena in an E-R diagram Develop E-R diagrams.
IE 423 – Design of Decision Support Systems Data modeling and database development.
3 & 4 1 Chapters 3 and 4 Drawing ERDs October 16, 2006 Week 3.
3 & 4 1 Database Systems: Design, Implementation, & Management, 7 th Edition, Rob & Coronel Keys Consists of one or more attributes that determine other.
Database Management COP4540, SCS, FIU Database Modeling Using the Entity-Relationship Model (Continued)
Database Management Systems MIT Lesson 02 – Database Design (Entity Relationship Diagram) By S. Sabraz Nawaz.
MIS2502: Data Analytics Relational Data Modeling
Week 7-8 DBMS ER-Relational Mapping. ER-Relational Mapping.
Understand Primary, Foreign, and Composite Keys Database Administration Fundamentals LESSON 4.2.
IE 423 – Design of Decision Support Systems Data modeling and database development.
©Silberschatz, Korth and Sudarshan7.1Database System Concepts - 6 th Edition Chapter 7: Entity-Relationship Model.
IT 5433 LM3 Relational Data Model. Learning Objectives: List the 5 properties of relations List the properties of a candidate key, primary key and foreign.
How to translate ER Model to Relational Model. Review - Concepts Relational Model is made up of tables A row of table = a relational instance/tuple A.
ER Diagrams and Relational Model CS 174a (Winter 2015)
Data Modeling Using the Entity- Relationship (ER) Model
COP Introduction to Database Structures
Let try to identify the conectivity of these entity relationship
Logical DB Design: ER to Relational
Entity Relationship Model
Chapter 9 Part-1: Concepts & Foreign Keys
Creating a Simple Database with Microsoft Access 2007
MIS2502: Data Analytics Relational Data Modeling
© The McGraw-Hill Companies, All Rights Reserved APPENDIX C DESIGNING DATABASES APPENDIX C DESIGNING DATABASES.
Entity Relationship (E-R) Modeling
Entity-Relationship Model
Tables and Their Characteristics
Quiz Questions Q.1 An entity set that does not have sufficient attributes to form a primary key is a (A) strong entity set. (B) weak entity set. (C) simple.
Chapter 7 Entity-Relationship Model
Entity-Relationship Model
MIS5101: Business Intelligence Relational Data Modeling
Entity Relationship Diagram
How to translate ER Model to Relational Model
Entity-Relationship Model and Diagrams (continued)
ERD’s REVIEW DBS201.
Normalization of Databases
Entity Relationship Diagrams
MIS2502: Data Analytics Relational Data Modeling
Types of Joins Farrokh Alemi, Ph.D.
Chapter 9 Part-1: Concepts & Foreign Keys
Creating Tables & Inserting Values Using SQL
Chapter 2 Modeling Data in the Organization
Database Modeling using Entity Relationship Model (E-R Model)
Chapter 1: The Database Environment
Lecture 9: The E/R Model II
Relational Databases Farrokh Alemi, PhD Narrated by Farhat Fazelyar
Normalization Organized by Farrokh Alemi, Ph.D.
Chengyu Sun California State University, Los Angeles
Chapter 7: Entity-Relationship Model
DBMS ER-Relational Mapping
Mapping an ERD to a Relational Database
INSTRUCTOR: MRS T.G. ZHOU
MIS2502: Data Analytics Relational Data Modeling 2
Conceptual modeling of databases
Database Management system
MIS2502: Data Analytics Relational Data Modeling 3
Information system analysis and design
Presentation transcript:

Relationships as Primary & Foreign Keys Organized by Farrokh Alemi, Ph.D. Narrated by Samantha Tone This section describes the relationship among tables. This brief presentation was organized by Dr. Alemi and narrated by Samantha Tone.

Relationships ER diagrams describe the entities that should be included in the database as tables and the relationship among these entities. Here we see two entities one describing the patient and the other street addresses. The relationship between these two entities is described by another table called relationship. This table indicates if the address is current or old address.

Relational Model Concepts A table = a schema/relation A row of table = a relational instance/tuple A column of table = an attribute Cardinality = number of rows Degree = number of columns Relational Model Concepts Relational databases are made of tables. Each table defines either a schema for an entity or a relationship. The rows in the table are relational instances. A column in the table represents entity attributes or table fields. The cardinality of the table refers to number of the rows in the table. Degree of the table refers to number of columns in the table

Example Instance of Student Relation StudentID StudentName login Age GPA 53666 Jones jones@cs 18 3.4 53688 Smith smith@eecs 3.2 53650 smith@math 19 3.8 Cardinality = 3, degree = 5 Here we have a table with cardinality 3 and degree 5, meaning we have 3 rows of data and 5 columns or fields.

From design to implementation ER Diagram Database implementation Entity, Relationship Table Relationship Primary & Foreign key(s) Attribute Field To map an ER diagram to the database one builds a table for each entity; if necessary builds a table for relationships among tow entities, makes a column in the table for each attribute in the entity, sets a primary key in the table and puts related foreign keys. Combination of primary and foreign keys are used to implement relationships.

Employee Address EmployeeID Name Entity Sets to Tables Employee Address EmployeeID Name Here Employee entity is mapped to a table with three fields, Employee ID which is a Primary key, name which is an attribute of the employee and now a field in the table, and an address, which is also an attribute of the employee and now another field in the table.

Primary Key Unique for Each Row of the Table PK No two distinct records in a table can have the same values of the primary key. For example, a patient record number identifies one record in “patient” table. Another example, G number identifies one student in GMU patriotweb system

Foreign Key Relationship to Other Tables PK Foreign Key is used to identify records in other table following relations. Foreign Key in one table always corresponds to PK in another table

One-to-One Relationships Employee EmployeeID FirstName LastName … Security DateObtained SecurityLevel Using a common identifiers in both entities creates the relationship. Here the primary key in the employee table is a foreign key in the Security table. Note that Types of primary and foreign keys must be identical. You cannot have patient ID once as numeric and once as text field.

Keys – let’s make it more complicated A Key Can Be Multiple Fields A key does not need to be defined as a single field. For example Patient name + date of birth can be used as a key (assuming it uniquely identifies patients).

Example PatientID AddressType Address City Zip 1111 Home 12345 Ox Rd Fairfax 22030 Business 11111 Lee Hwy Patient ID + address type can be a primary key for address.

One-to-One Relationships One-to-one relationships are rare and are used in specific situations, such as: To avoid missing data, To model hierarchies, For privacy/security reason, or to break very large tables, if some fields are rarely accesses at the same time.

(2) One-to-Many Relationships One to many relationships are the most important and the most frequent type of relationship in relational databases. They are used to represent all types of relationships in relational databases. When modeling one-to-many relationships you use foreign key in “many” to refer to record in “one”. Examples include patient-address. The primary key is the patient ID is used several times as foreign key in address table to indicate multiple addresses for the patient.

Foreign keys Here is an example of one patient having many addresses: Foreign key in address points to primary key in patient.

Primary and foreign keys RecordNumber FirstName LastName 123 John Smith 445 Andrew Brown AddressID PatientRecordNumber Number Street City 1 123 4400 University Dr. Fairfax 2 445 4310 3 650 K St. NW Washington These two tables are related to each other because the ferign key in the second table points to the first table’s primary key.

Some Relationships Suggest New Entities Many to Many Relationships

Sometimes we have entities that have many to many relationships Sometimes we have entities that have many to many relationships. In this design, patients can have many physicians and physicians can have many patients. It is not possible to indicate the many to many relationship with just foreign key in either table.

(3) Keys in many-to-many The solution is to have a relationship table that indicates the nature of the relationship in connecting many to many entities. Here, the visit table is the linking table between physicians and patients. In this design, patients can have many physicians and physicians can have many patients and the nature and number of each can be discovered in the visit table. The Linking table has two foreign keys. These foreign keys point to two different entities. Neither one of the foreign keys are unique in the linking table.

Let us look at an example. This group has several clinics Let us look at an example. This group has several clinics. Each clinic employs a number of healthcare providers. The group serves a number of patients in the area. The database to be constructed will serve the information needs of practice group management as well as providers and patients.

Entity - Table We have three entities: patient, provider and clinic. The attributes of each of these 3 entities are listed as fields within the 3 tables. Patient table has ID, Patient Name, Date of birth, Social security number and Insurance Policy.

Entity - Table The provider table has ID, Provider Name, Date of birth and Employee ID.

Entity - Table The clinic has Clinic ID, Clinic Name, Address, and Phone.

Relationship - Table We also need a relational table or an association table, here called encounter. It has an id, date and diagnosis code.

Keys Patient (ID, PatientName, DOB, SSN, Insurance Policy, ClinicID) Provider (ID, ProviderName, DOB, EmployeeID, ClinicID) Clinic (ClinicID, ClinicName, Address, Phone) Encounter (EncounterID, Date, DiagnosisCode, PatientID, ProviderID) To make this scheme work, we need to also introduce various relationships among these tables by introducing foreign keys. The clinic id is added as a foreign key to the patient and provider ID as these are one to many relationships and do not require additional association tables.

Keys Patient (ID, PatientName, DOB, SSN, Insurance Policy, ClinicID) Provider (ID, ProviderName, DOB, EmployeeID, ClinicID) Clinic (ClinicID, ClinicName, Address, Phone) Encounter (EncounterID, Date, DiagnosisCode, PatientID, ProviderID) The relationship between patients and providers is many to many and requires an additional association table, now called encounter table. We introduce both the patient and provider ID as foreign keys in the encounter table.

Primary and Foreign Keys indicate relationship among tables Cursor and Do-While commands repeatedly execute an SQL code