Vous êtes sur la page 1sur 29

How to create

E-R Diagrams from


Database Tables ?

Ecologic Corporation Training Department


Chandigarh
www.ecologic.co.in

Ecologic Corporation , Chandigarh ,


26/04/2009 www.ecologic.co.in 1
Learning Objectives
 Conversion of an ER model to relational
schemas
 Entitysets, relationship sets, attributes,
redundancy removal, specialization, and
aggregation
 SQL constructs to create tables and
constraints

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 2
Doing from E-R to Relational
 To create a relational database, we need a
set of relational schemas.
 The first step of database design is to
produce an E-R diagram that conforms to
the user requirement.
 The subsequent step is to convert the E-R
diagram into a collection of relational
schemas, including constraints.
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 3
Step 1 : Conversion
 For each entity set, create a
corresponding relational schema.
 For each relationship set, create a
corresponding relational schema.
 Convert attributes in the E-R diagram to
columns (attributes) in the relational
schemas.

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 4
Entity Sets to Schemas
 A strong entity set is converted to a relation
with no additional column.
 A weak entity set becomes a table that
includes the primary key of the identifying
strong entity set. E.g.,
payment =
( loan_number, payment_number,
payment_date, payment_amount )

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 5
Relationship Sets to Schemas
A many-to-many relationship set is
represented as a relation. Its
attributes include the primary keys
of the participating entity sets and
the attributes of the relationship set.
Example:
borrower = (customer_id, loan_number )

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 6
Removing Redundancy:
OneA many-to-one

to Many or Many to One
or one-to-many relationship set that is total on the
many-side can be represented by adding the attributes of the
relationship set plus the primary key of the “one” side to the relation
that correponds to the “many” side
 Example: Instead of creating a separate relation for the relationship
set account_branch, we can just add an attribute branch_name to
the relation that represents the entity set account.

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 7
The next step:
Removing Redundancy :
One to One

 For a one-to-one relationship set, it can be


represented by the relation that
corresponds to the entity set on either side
after adding the attributes from the
relationship set and the primary key of the
entity from the other side.

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 8
Removing Redundancy :
Identifying Relationship
 The schema corresponding to the
identifying relationship set linking a weak
entity set to its identifying strong entity set
is redundant.
 Example: The payment schema already
contains the attributes that would appear in
the loan_payment schema (i.e., loan_number
and payment_number).

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 9
Composite Attributes
 Composite attributes are flattened out by
creating a separate attribute for each
component attribute
 Example: given entity set customer with
composite attribute name with component
attributes first_name and last_name, the
schema corresponding to the entity set has
two attributes
name.first_name and name.last_name
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 10
Multivalued Attributes
 A multivalued attribute M of an entity E is represented by a
separate schema EM
 Schema EM has attributes corresponding to the primary key
of E and an attribute corresponding to the multivalued
attribute M.
 E.g.,: dependent_names of employee is represented as
employee_dependent_names = ( employee_id, dname)
 Each value of the multivalued attribute maps to a tuple of a
relation on schema EM.
 E.g., an employee entity with primary key 123 and
dependents Jack and Jane maps to two tuples: (123,
Jack) and (123, Jane).

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 11
Representing Specialization (1)

 Method 1:
 Form a schema for the higher-level entity set
 Form a schema for each lower-level entity
set and include the primary key of the higher-
level entity set and local attributes

schema attributes
person name, street, city
customer name, credit_rating
employee Ecologic
name, salary
Corporation , Chandigarh
12
26/04/2009 160022, www.ecologic.co.in
Representing Specialization (2)
 Method 2:
 Form a schema for each entity set with all
local and inherited attributes

schema attributes
person name, street, city
customer name, street, city, credit_rating
employee name, street, city, salary

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 13
Comparison of the Two Methods of
Representing Specialization
 Method 1: getting information about an
employee requires accessing two relations, the
one corresponding to the low-level schema and
the one corresponding to the high-level schema
 Method 2: If specialization is total, the schema
for the generalized entity set (person) is not
required to store information
 Drawback: street and city may be stored redundantly
for people who are both customers and employees
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 14
Representing Aggregation
 Create a schema containing
 the primary key of the aggregated relationship
 the primary key of the associated entity set
 any attributes of the relationship

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 15
Example of Representing
Aggregation

manages (employee_id, branch_name, title, manager_name)

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 16
History of SQL
 Developed as Sequel in System R project at
the IBM San Jose Research Laboratory
 Renamed Structured Query Language (SQL)
 Became ANSI and ISO standards:
 SQL-86, SQL-89, SQL-92, SQL:1999, SQL:2003
 Most of SQL-92 features are offered by
commercial systems, plus varying feature
sets from later standards and special
proprietary features.
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 17
Data Definition Language
 Defines
 The schema for each relation
 The domain of each attribute
 Integrity constraints
 Indexes for each relation
 Security and authorization information for
each relation
 The physical storage structure of each
relation on disk
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 18
Domain Types in SQL
 char(n), varchar(n) Fixed length or
variable length character strings
 int, smallint Integers
 numeric(p,d) Fixed point number
 real, double precision, float(n) Floating
point and double-precision floating point
numbers
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 19
Create Table SQL Construct
create table r (A1 D1, A2 D2, ..., An Dn,
(integrity-constraint1),
...,
(integrity-constraintk))
r is the name of the relation
 each Ai is an attribute name in the schema
of relation r
 Di is the data type of values in the domain
of attribute Ai
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 20
Example: Creating a Table
create table branch
(branch_name char(15) not null,
branch_city char(30),
assets integer)

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 21
Integrity Constraints in SQL
 not null
 primary key (A1, ..., An )
create table branch
(branch_name char(15),
branch_city char(30),
assets integer,
primary key (branch_name))
primary key declaration on an attribute automatically ensures
not null in SQL-92 onwards

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 22
Drop and Alter Table Constructs
 The drop table command deletes all
information about the dropped relation
from the database.
 The alter table command can be used to
add attributes to an existing relation:
alter table r add A D
where A is the name of the attribute to be
added to relation r and D is the domain
of A.
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 23
Examples of ER Diagrams

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 24
Shopping Cart ER Diagram

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 25
North Wind DB ER Diagram

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 26
ER Diagrams Rules Simplified

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 27
Guess What this diagram is
Saying?

Ecologic Corporation , Chandigarh


26/04/2009 160022, www.ecologic.co.in 28
Summary
 In general, each entity set or relationship
set is translated into a relation.
 There are special cases, including many-
to-one, one-to-many, and one-to-one
relationships; identifying relationships;
multi-valued and composite attributes;
specialization and aggregation.
 Tables and constraints are created,
altered, and dropped through SQL DDL.
Ecologic Corporation , Chandigarh
26/04/2009 160022, www.ecologic.co.in 29

Vous aimerez peut-être aussi