0% found this document useful (0 votes)
19 views57 pages

Dbday1

The document provides an introduction to database concepts including the Entity Relationship model. It describes key components of the ER model like entities, attributes, relationships and also mapping of ER diagrams to relational database tables.

Uploaded by

Ahmed Allam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views57 pages

Dbday1

The document provides an introduction to database concepts including the Entity Relationship model. It describes key components of the ER model like entities, attributes, relationships and also mapping of ER diagrams to relational database tables.

Uploaded by

Ahmed Allam
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 57

Intro To Database

Mohamed ELshafei
ER Model
Entity Relationship Diagram
Concepts
Importance of ER Model

ERD stands for Entity Relationship diagram.


ER diagram shows the relationship between objects, places, people,
events etc. within that system.
ER Model is a graphical representation for designing the structure.
Without ER diagrams you cannot make a database structure & write
production code.
It displays the clear picture of the database structure.
Basic constructs of the E-R model

Entities - person, place, object, event, concept (often corresponds to a


real time object that is distinguishable from any other object)

Attributes - property or characteristic of an entity type (often


corresponds to a field in a table)

Relationships – link between entities (corresponds to primary key-


foreign key equivalencies in related tables)
ER Diagram: Starting Example

access-date
cust-name number

cust-id

customer has account


cust-street

cust-city balance

Rectangles: entity sets


Diamonds: relationship sets
Ellipses: attributes
Entity

A definable thing—such as a person, object, concept or event—that


can have data stored about it.
Think of entities as nouns.
Examples: a customer, student, car or product.
Typically shown as a rectangle.
Entity categories

strong entity can be defined solely by its own attributes.


weak entity cannot defined solely by its own attributes, An entity set
that do not have sufficient attributes to form a primary key.
Entity keys

Candidate Key
Primary Key
Foreign Key
Composite Key
Partial Key
Alternate key
Super Key
Primary Key

Identifier used to uniquely identify one particular instance of an entity.

Can be one or more attributes.


Must be unique .
Value should not change over time.
Must always have a value .
Entity keys

Candidate Key :
when multiple possible identifiers exist, each is a candidate key.
Foreign Keys :
Foreign keys reference a related table through the primary key of that
related table.
Referential Integrity Constraint:
For every value of a foreign key there is a primary key with that value in
the referenced table e.g. if student name is to be used in a dormitory
table then that name must exist in the student table.
Attribute

A property or characteristic of an entity. Often shown as an oval or


circle.
Attribute categories:
 Simple (Scalars) Attribute
 Composite Attribute
 Derived Attribute
 Multi-valued Attribute
 Complex Attribute
Simple Attribute

Means the attribute value is atomic and can’t be further divided, such
as a phone number.
access-date
cust-name number

cust-id

customer has account


cust-street

cust-city balance
Composite Attribute

Sub-attributes spring from an attribute


First Last

Name
year

day date-of-birth customer

month
Derived Attribute

Attributed is calculated or otherwise derived from another attribute,


such as age from a birthdate.
age

date-of-birth

Emp

Salary

Deduction
Net Salary
Multi-valued

More than one attribute value is denoted, such as multiple phone


numbers for a person.
address

customer

phone no.
Complex Attribute

multi-valued + Composite

Street City Country

address

customer
Relationship

A Relationship is an association among several entities.


A relationship may also have attributes
For example, consider the entity sets customer and loan and the
relationship set borrower. We could associate the attribute date-issued to
that relationship to specify the date when the loan was issued.
Relation Properties

Relation has three Properties:

 Degree of Relationships
 Cardinality Constraint
 Participation Constraint
Degree of Relationships

number of entity types that participate in a relationship


Three cases:
 Unary: between two instances of one entity type
 Binary: between the instances of two entity types
 Ternary: among the instances of three entity types
Degree of Relationships
Recursive Relationship (Unary)

 Recursive Relationships - A relationship in which the same entity


participates more than once.
Binary Relationship

A binary relationship set is of degree 2.


Ternary Relationship

Ternary relationship set is of degree 3.


Cardinality

How many instances of one entity will or must be connected to a single


instance from the other entities.

One-One Relationship
One-Many Relationship
Many- Many Relationship
Mapping Cardinalities

1 1
One-to-One Employee own Car

M
1
One-to-Many Department has Employee

M M
Many-to-Many Course take Student
PARTICIPATION CONSTRAINT

An employee MUST work for a department


An employee entity can exist only if it participates in a WORKS_FOR
relationship instance
So this participation is TOTAL
Only some employees manage departments
The participation is PARTIAL
PARTICIPATION CONSTRAINT

Employee Has Car

-An Employee may have a car.


-A Car must be assigned to particular employee
PARTICIPATION CONSTRAINT

Department Hire Employee

- A department may hire many employees ( Zero or more)


- An employee must be employed by a department
(Department membership is Optional, Employee membership is
Mandatory)
Case Study
Company
Result
Relational Database

A data structure through which data is stored in tables that are related
to one another in some way.
The way the tables are related is described through a relationship.

Intake 39 - DB
Basic Database Structure

Table or entity: a collection of records


Attribute or Column or field: a Characteristic of an entity
Row or Record : the specific characteristics of one entity
Database: a collection of tables

Intake 39 - DB
Mapping
ERD Mapping to Tables
Steps
Intake 39 - DB
ER-to-Relational Mapping

Step 1: Mapping of Regular Entity Types


Step 2: Mapping of Weak Entity Types
Step 3: Mapping of Binary 1:1 Relation Types
Step 4: Mapping of Binary 1:N Relationship Types.
Step 5: Mapping of Binary M:N Relationship Types.
Step 6: Mapping of N-ary Relationship Types.
Step 7: Mapping of Unary Relationship.
Intake 39 - DB
Step 1: Mapping of Regular Entity Types

Create table for each entity type.


Choose one of key attributes to be the primary key.

Intake 39 - DB
Mapping Composite attribute

CUSTOMER relation with address detail

Intake 39 - DB
Mapping Multivalued Attribute

Multivalued attribute becomes a separate relation with foreign key

Intake 39 - DB
Mapping Derived & Complex

In the most cases Derived attribute not be stored in DB.

Mapping Complex Like Mapping Multivalued attribute then


including parts of the multivalued attributes as columns in DB

Intake 39 - DB
Step 2: Mapping of Weak Entity Types

Create table for each weak entity.


Add foreign key that correspond to
the owner entity type.

Primary key composed of:


Partial identifier of weak entity
Primary key of identifying relation (strong entity) Intake 39 - DB
Step 3: Mapping of Binary 1:1 Relation Types

Merged two tables if both sides are Mandatory.

Add FK into table with the total participation relationship to represent


optional side.

Create third table if both sides are optional.

Intake 39 - DB
One To One 2 Mandatory

EID EName CID CName


One-to-One
1 1
Employee Has Computer
2 Mandatory

1 table
Emp(EID, Ename, Cname, CID)
tbl_xy (PK,….,…,….)
PK = PKx or PKy
Intake 39 - DB
One To One Optional-Mandatory

EID EName CID CName


One-to-One
1 1
Employee Has Computer
X optional – Y mandatory

Employee(EID, Ename)
2 tables
tbl_x (PKx,….,…….) Computer(CID, Cname, EID_FK)
tbl_y (PKy,….,….,PKx….)
Intake 39 - DB
One To One 2 Optional

EID EName CID CName


One-to-One
1 1
Employee Has Computer
2 Optional

3 tables
Employee(EID, Ename)
tbl_x (PKx,….,…….)
tbl_y (PKy,….,…….) Car(CID, CType)
tbl_xy (PKxy,….,…,FKxy,….)
Emp_Car(EID, CID_FK) Intake 39 - DB
PKxy = PKx or PKy
Step 4: Mapping of Binary 1:N Relationship Types

Add FK to N-side table if N-Side mandatory

Add any simple attributes of relationship as column to N-side table.

Intake 39 - DB
One To Many (Many is Mandatory)

EID EName DID DName


One-to-Many
M 1
Employee work Department
X whatever– Y mandatory

2 tables Department(DID, Dname)


tbl_x (PKx,….,…….)
tbl_y (PKy,….,….,FKy….) Employee(EID, Ename,DID)
FKy= PKx
Intake 39 - DB
One To Many (Many is Optional)

EID EName PID PName


One-to-Many
M 1
Employee Works_on Project
X whatever– Y Optional

3 tables
Project(PID, Pname)
tbl_x (PKx,….,…….)
tbl_y (PKy,….,…….) Employee(EID, Ename)
tbl_xy (PKxy,….,…….)
PKxy = PKy Proj_Emp(EID,PID_FK) Intake 39 - DB
Step 5: Mapping of Binary M:N Relationship
Types.

Create a new third table


Add FKs to the new table for both parent tables
Add simple attributes of relationship to the new table if any .

Intake 39 - DB
Many To Many

CNam
SID SName CID
e
Many-to-Many
M M
Student take Course
X whatever– Y whatever

3 tables
Student(SID, Sname)
tbl_x (PKx,….,…….)
tbl_y (PKy,….,…….) Course(CID, Cname)
tbl_xy (PKx ,PKy, ….,…….)
PKxy= PKx+PKy Stud_Course(SID, CID) Intake 39 - DB
Many To Many with attribute

The Supplies relationship will need to become a separate relation

Intake 39 - DB
Step 6: Mapping of N-ary Relationship
Types.

If n > 2 then :
Create a new third table.
Add FKs to the new table for all parent tables.
Add simple attributes of relationship to the new table if any .

Intake 39 - DB
Step 6: Mapping of N-ary Relationship
Types.

Intake 39 - DB
Step 7:Mapping Unary Relationship

EMPLOYEE relation with recursive


foreign key

Intake 39 - DB
Case Study
Company

Intake 39 - DB
Case study

Intake 39 - DB
Result

Intake 39 - DB
Intake 39 - DB
Result

Student(St-id,st-fname,st-Lname,st-age,st-super,Dept-ID)
Course(Crs-id.Crs-Name,Crs-Duration,Top-id)
Topic(Top-ID,Top-Name)
Stud-Course(St-ID,Crs-ID,grade)
Instructor(Ins-ID,ins-Name,Address,Salary,Dept-ID)
Ins-Course(Ins-ID,Crs-ID,Evalution)
Department(Dept-ID,Dept-Name,Manager-ID,HireDate)
Intake 39 - DB

You might also like