Вы находитесь на странице: 1из 53

Unit 3 : Mapping ERDs into Relations

Pratian Technologies (India) Pvt. Ltd.


www.pratian.com

Topics
RDBMS Terminology Primary Key Foreign Key Candidate Key Integrity Constraints Transforming ER Models into Relations

Copyright 2012 Pratian Technologies www.pratian.com

SDLC revisited
Project Identification and Selection Project Initiation and Planning Analysis

Purpose information requirements structure Deliverable detailed design specifications

Logical Logical Design Design Physical Design

Database activity logical database design

Implementation Maintenance

Copyright 2012 Pratian Technologies www.pratian.com

Sample Database Table


Attributes

Tuples Relation

C a r d i n a li t y

Instance Degree
Copyright 2012 Pratian Technologies www.pratian.com

RDBMS Terminology
Formal Relational Term Relation Tuple Cardinality Attribute Degree Primary Key Domain Informal Equivalents Table Row, Record Number of Rows Column, Field Number of Columns Unique Identifier Set of legal values

Copyright 2012 Pratian Technologies www.pratian.com

Candidate Key, Primary Key, Alternate Key


A candidate key is an attribute that can uniquely identify a row in a table.
ELEMENT
Symbol Al Fe Ni Cu Ag Au Name Aluminium Iron Nickel Copper Silver Gold AtomicNo 13 26 28 29 47 79 MeltingPoint 933 1811 1728 1357 1235 1337 BoilingPoint 2792 3134 3186 3200 2435 3129

Copyright 2012 Pratian Technologies www.pratian.com

Definitions
Primary Key
An attribute ( or combination of attributes ) that uniquely identifies each row in a relation

Composite key
A primary key that consists of more than one attribute

Copyright 2012 Pratian Technologies www.pratian.com

Foreign Key
Attribute or attribute combination of one table whose values are required to match those of the primary key of some other table Each Foreign key represents a relationship between two entity types.

Copyright 2012 Pratian Technologies www.pratian.com

Foreign Key - Definition


They are added to relations as we go through mapping process They allow the relations to be linked together A relation can have several foreign keys Generally there will be a foreign key from each table a relation is related to Foreign keys are usually show in italics or wiggly underline

Copyright 2012 Pratian Technologies www.pratian.com

Foreign Key
AUTHORS TABLE AUTHORID A1 NAME SHOBHA DE CITY MUMBAI COUNTRY INDIA

PUBLISHER TABLE PUB_ID P1 NAME MCGRAW HILL CITY NEW YORK WORK TABLE AUTHORID A1 PUB_ID P1 TITLE SISTERS COUNTRY U.S.A

Copyright 2012 Pratian Technologies www.pratian.com

Integrity Constraints
Domain Constraints
All the values that appear in a column must be taken from the same domain Domain definition consists of the following components
Domain Name Meaning Data type Size/ Length Allowable values or range

Entity Integrity
Primary key

Referential Integrity
Involves foreign key

Operational Constraints
Implement real world limitations
Copyright 2012 Pratian Technologies www.pratian.com

Transforming E R Models to Relations


EMPLOYEE ( empno , name, sex, dateofbirth, deptid)
sex name dateofbirth

EMPLOYEE name empno

deptid

EMPLOYEE TABLE EmpNo 1 2 Name Mohan Sarvesh Sex M M DateOfBirth 01-01-1978 12-01-1980 DeptId 10 20

Copyright 2012 Pratian Technologies www.pratian.com

Properties of a Relation
Each entity or a table in a database has a unique name An entry at the intersection of each row and column is atomic. There can be no multivalued attributes in a relation Each row is unique. Each attribute or column within a table has a unique name. The sequence / arrangement of columns is insignificant The sequence / arrangement of rows is insignificant

Copyright 2012 Pratian Technologies www.pratian.com

Mapping the ER Model


Mapping Strong entity DEPARTMENT ( DeptId, DeptName, CostCenter)
deptid deptname costcenter

DEPARTMENT name

Copyright 2012 Pratian Technologies www.pratian.com

Mapping 1:1 Relationship


Mandatory at both ends Mandatory at one end and optional at the other Optional at both ends

Copyright 2012 Pratian Technologies www.pratian.com

Mandatory at both ends Option 1


EMPLOYEE has CONTRACT

EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate) Amalgamated into one as EMPLOYEE (empno, name, contractno, startdate, duedate)

Copyright 2012 Pratian Technologies www.pratian.com

Mandatory at both ends Option 2


EMPLOYEE has CONTRACT

EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate) The same can be kept apart using Foreign Key EMPLOYEE (empno, name, contractno) CONTRACT (contractno, startdate, duedate)

Copyright 2012 Pratian Technologies www.pratian.com

Mandatory at both ends Option 3


EMPLOYEE has CONTRACT

EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate) Or EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate, empno)

Copyright 2012 Pratian Technologies www.pratian.com

Mandatory at one end and Optional at other end


EMPLOYEE has CONTRACT

EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate) Amalgamated into one as EMPLOYEE (empno, name, contractno, startdate, duedate)

Copyright 2012 Pratian Technologies www.pratian.com

Mandatory at one end and Optional at other end


EMPLOYEE has CONTRACT

EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate)

The same can be kept apart using Foreign Key EMPLOYEE (empno, name, contractno) CONTRACT (contractno, startdate, duedate) Map the foreign key into Employee - the key is null for employee without a contract. Incorrect Design
Copyright 2012 Pratian Technologies www.pratian.com

FK in EMPLOYEE
EMPLOYEE
Empno 10 11 Name Gordon Andrew Contract No 1 NULL

CONTRACT
Contractno StartDate DueDate

1st Jan 2007

10th Oct 2007

Copyright 2012 Pratian Technologies www.pratian.com

Mandatory at one end and Optional at other end


EMPLOYEE has CONTRACT

EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate) EMPLOYEE (empno, name) CONTRACT (contractno, startdate, duedate, empno) Map the foreign key into Contract empno is mandatory thus never null.

Copyright 2012 Pratian Technologies www.pratian.com

FK in CONTRACT
So for 1:1 optional relationships, take the primary key from the mandatory end and add it to the optional end as a foreign key.

EMPLOYEE
Empno 10 11 Name Gordon Andrew

CONTRACT
Contractno 1
Copyright 2012 Pratian Technologies www.pratian.com

StartDate

EndDate

Empno 10

1st Jan 2007 10th Oct 2007

Optional Participation at Both Ends

Such examples cannot be amalgamated as you could not select a primary key.

Copyright 2012 Pratian Technologies www.pratian.com

Mapping 1:M Relationships


DEPARTMENT (Deptid, name, type, location) EMPLOYEE (empno, name)

DEPARTMENT

has

EMPLOYEE

After Mapping the foll relations are produced DEPARTMENT (Deptid, name, type, location) EMPLOYEE (empno, name, Deptid)

Copyright 2012 Pratian Technologies www.pratian.com

Mapping 1:M in Unary Relationships

Employee manages employees Employee(employee_no, manager_no, name,...) Foreign key

Primary key

Copyright 2012 Pratian Technologies www.pratian.com

Mapping 1:M in Unary Relationships


EMPNO 7369 7499 7521 7566 7654 7698 7782 7788 7839 7844 7876 7900 7902 7934 ENAME SMITH ALLEN WARD JONES MARTIN BLAKE CLARK SCOTT KING TURNER ADAMS JAMES FORD MILL JOB CLERK SALESMAN SALESMAN MANAGER SALESMAN MANAGER MANAGER ANALYST PRESIDENT SALESMAN CLERK CLERK ANALYST CLERK 7698 7788 7698 7566 7782 MGR 7902 7698 7698 7839 7698 7839 7839 7566 SAL 1800 1600 1250 2975 1250 2850 2450 3000 6000 9300 2100 1950 7777 7654

Copyright 2012 Pratian Technologies www.pratian.com

Mapping M:N Relationships


A new table has to be created to map M : N relations

AUTHOR

N PUBLISHER

authorid

name pub_id city country city country name

Copyright 2012 Pratian Technologies www.pratian.com

Mapping N:M Relationships


pub_id authorid title

AUTHOR

Author_Publisher

1 PUBLISHER

authorid

name pub_id city country city country name

Copyright 2012 Pratian Technologies www.pratian.com

Mapping N:M Relationships


AUTHORS TABLE AUTHORID A1 NAME SHOBHA DE CITY MUMBAI COUNTRY INDIA

PUBLISHER TABLE PUB_ID P1 NAME MCGRAW HILL CITY NEW YORK COUNTRY U.S.A

WORK TABLE AUTHORID A1 PUB_ID P1 TITLE SISTERS

Copyright 2012 Pratian Technologies www.pratian.com

Summary
1-1 relationships Depending on the requirement of the relationship, the entities are either combined or the primary key of one entity type is placed as a foreign key in the other relation. 1-m relationships The primary key from the one side is placed as a foreign key in the many side. m-n relationships A new relation is created with the primary keys from each entity forming a composite key.
Copyright 2012 Pratian Technologies www.pratian.com

Lets check our understanding!!!

Copyright 2012 Pratian Technologies www.pratian.com

Identify keys in these relations

Primary Key Foreign Key


(implements 1:N relationship between customer and order)

Combined, these are a composite primary key (uniquely identifies the order line)individually they are foreign keys (implement M:N relationship between order and product)

Copyright 2012 Pratian Technologies www.pratian.com

Write Customer relation for the foll. (a) CUSTOMER entity type with simple attributes

(b) CUSTOMER relation

Copyright 2012 Pratian Technologies www.pratian.com

Map the foll into a relation (a) CUSTOMER entity type with composite attribute

(b) CUSTOMER relation with address detail

Copyright 2012 Pratian Technologies www.pratian.com

Map the foll. into relations (a)

Multivalued attribute becomes a separate relation with foreign key (b)

1tomany relationship between original entity and new relation


Copyright 2012 Pratian Technologies www.pratian.com

Map the foll ERD

Copyright 2012 Pratian Technologies www.pratian.com

NOTE: the domain constraint for the foreign key should NOT allow null value if DEPENDENT is a weak entity Foreign key

Composite primary key

Copyright 2012 Pratian Technologies www.pratian.com

Map the Relationship between customers and orders

Note the mandatory one

Copyright 2012 Pratian Technologies www.pratian.com

Mapping the relationship

Again, no null value in the foreign keythis is because of the mandatory minimum cardinality

Foreign key

Copyright 2012 Pratian Technologies www.pratian.com

Map an M:N relationship into relations E-R diagram (M:N)

Hint :The Supplies relationship will need to become a separate relation


Copyright 2012 Pratian Technologies www.pratian.com

Solution: Three resulting relations

Composite primary key

Foreign key Foreign key

New intersection relation

Copyright 2012 Pratian Technologies www.pratian.com

Map a binary 1:1 relationship In_charge relationship

Copyright 2012 Pratian Technologies www.pratian.com

Solution: Resulting relations

Copyright 2012 Pratian Technologies www.pratian.com

Map the foll ERD into relations

Copyright 2012 Pratian Technologies www.pratian.com

Copyright 2012 Pratian Technologies www.pratian.com

Map the foll ERD : an associative entity with an identifier Associative entity

Copyright 2012 Pratian Technologies www.pratian.com

Three resulting relations

Copyright 2012 Pratian Technologies www.pratian.com

Map a unary 1:N relationship

(a) EMPLOYEE entity with Manages relationship

(b) EMPLOYEE relation with recursive foreign key

Copyright 2012 Pratian Technologies www.pratian.com

Map a unary M:N relationship

(a) Bill-of-materials relationships (M:N)

(b) ITEM and COMPONENT relations

Copyright 2012 Pratian Technologies www.pratian.com

Map a ternary relationship Ternary relationship with associative entity

Copyright 2012 Pratian Technologies www.pratian.com

Solution

Remember that the primary key MUST be unique


Copyright 2012 Pratian Technologies www.pratian.com

Question time
Please try to limit the questions to the topics discussed during the session. Thank you.

Copyright 2012 Pratian Technologies www.pratian.com

Вам также может понравиться