The document discusses how to convert ER diagrams to relational databases. It explains that each entity set maps to a table, while relationship sets can map to tables or be represented within other tables by adding attributes. It also covers handling special cases like one-to-one/many relationships, composite attributes, and specialization/aggregation. The document provides SQL commands for creating tables, adding constraints, and altering or dropping tables during the conversion process.
1. How to create
E-R Diagrams from
Database Tables ?
Ecologic Corporation Training Department
Chandigarh
www.ecologic.co.in
Ecologic Corporation , Chandigarh ,
www.ecologic.co.in 1
26/04/2009
2. Learning Objectives
Conversion of an ER model to relational
schemas
Entity sets, relationship sets, attributes,
redundancy removal, specialization, and
aggregation
SQL constructs to create tables and
constraints
Ecologic Corporation , Chandigarh
2
160022, www.ecologic.co.in
26/04/2009
3. 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
3
160022, www.ecologic.co.in
26/04/2009
4. 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
4
160022, www.ecologic.co.in
26/04/2009
5. 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
5
160022, www.ecologic.co.in
26/04/2009
6. 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
6
160022, www.ecologic.co.in
26/04/2009
7. Removing Redundancy:
Onemany-to-one or one-to-many relationshipto that is total on the
to Many or Many set One
A
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
7
160022, www.ecologic.co.in
26/04/2009
8. 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
8
160022, www.ecologic.co.in
26/04/2009
9. 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
9
160022, www.ecologic.co.in
26/04/2009
10. 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
10
160022, www.ecologic.co.in
26/04/2009
11. 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
11
160022, www.ecologic.co.in
26/04/2009
12. 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 Corporation , Chandigarh
name, salary 12
160022, www.ecologic.co.in
26/04/2009
13. 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
13
160022, www.ecologic.co.in
26/04/2009
14. 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
14
160022, www.ecologic.co.in
26/04/2009
15. 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
15
160022, www.ecologic.co.in
26/04/2009
16. Example of Representing
Aggregation
manages (employee_id, branch_name, title, manager_name)
Ecologic Corporation , Chandigarh
16
160022, www.ecologic.co.in
26/04/2009
17. 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
17
160022, www.ecologic.co.in
26/04/2009
18. 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 Corporation , Chandigarh
Ecologic
18
160022, www.ecologic.co.in
26/04/2009
19. 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
19
160022, www.ecologic.co.in
26/04/2009
20. 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
20
160022, www.ecologic.co.in
26/04/2009
22. 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
22
160022, www.ecologic.co.in
26/04/2009
23. 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
23
160022, www.ecologic.co.in
26/04/2009
24. Examples of ER Diagrams
Ecologic Corporation , Chandigarh
24
160022, www.ecologic.co.in
26/04/2009
28. Guess What this diagram is
Saying?
Ecologic Corporation , Chandigarh
28
160022, www.ecologic.co.in
26/04/2009
29. 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
29
160022, www.ecologic.co.in
26/04/2009