Ders # 2: Veri Tabani Ders Notlari Data Modeling With ER References Addison Wesley"
Ders # 2: Veri Tabani Ders Notlari Data Modeling With ER References Addison Wesley"
Ders # 2: Veri Tabani Ders Notlari Data Modeling With ER References Addison Wesley"
VER TABANI DERS NOTLARI data modeling with ER References ER section from Elmasri, Navathe, Fund. of Database Systems, 5th ed., Addison Wesley
Outline
Overview of Database Design Process Example Database Application (COMPANY) ER Model Concepts Entities and Attributes Entity Types, Value Sets, and Key Attributes Relationships and Relationship Types Weak Entity Types Roles and Attributes in Relationship Types ER Diagrams - Notation ER Diagram for COMPANY Schema EER modelling (Spacification/Generalization, Superclass/ Subclass, Hierarcies, Lattices) Alternative Notations UML class diagrams, others
Applications design focuses on the programs and interfaces that access the database
ER
Entities are specific objects or things in the mini-world that are represented in the database.
For example the EMPLOYEE John Smith, the Research DEPARTMENT, the ProductX PROJECT For example an EMPLOYEE entity may have the attributes Name, SSN, Address, Sex, BirthDate For example a specific employee entity may have Name='John Smith', SSN='123456789', Address ='731, Fondren, Houston, TX', Sex='M', BirthDate='09-JAN-55
Each attribute has a value set (or data type) associated with it e.g. integer, string, subrange, enumerated type,
Entities with the same basic attributes are grouped or typed into an entity type. ( EMPLOYEE and PROJECT are entity types)
An attribute of an entity type for which each entity must have a unique value is called a key attribute of the entity type.
Attributes are displayed in ovals, and is connected to its entity type Each key is underlined in ER model
Types of attribute: Simple, Composite, Multivalued Simple: Each entity has a single atomic value for the attribute. (SSN or
DateofBirth)
{PreviousDegrees}. )
ER Model Concepts-
EXAMPLE: Entity Type CAR with two keys and a corresponding Composite Entity Set
attribute
Entity Set
Entity set is the collection of entities stored in the database Previous slide shows three CAR entity instances in the entity set for CAR Same name (CAR) used to refer to both the entity type and the entity set Entity set is the current state of the entities of that type that are stored in the database
A key
Relationship Set
Relationships of the same type are grouped or typed into a relationship type.Identifies the
relationship name participating entity types, degree of a relationship, cardinality ratio, existence dependency)
We need to create a database schema design based on the following (simplified) requirements of the COMPANY Database: The company is organized into DEPARTMENTs. Each department has a name, number and an employee who manages the department. We keep track of the start date of the department manager. A department may have several locations. Each department controls a number of PROJECTs. Each project has a unique name, unique number and is located at a single location. We store each EMPLOYEEs social security number, address, salary, sex, and birthdate. Each employee works for one department but may work on several projects. We keep track of the number of hours per week that an employee currently works on each project. We also keep track of the direct supervisor of each employee. Each employee may have a number of DEPENDENTs. For each dependent, we keep track of their name, sex, birthdate, and relationship to the employee.
Based on the requirements, we can identify four initial entity types in the COMPANY database:
The initial attributes shown are derived from the requirements description
In the refined design, some attributes from the initial entity types are refined into relationships:
Manager of DEPARTMENT -> MANAGES Works_on of EMPLOYEE -> WORKS_ON Department of EMPLOYEE -> WORKS_FOR Controlling_Department of PROJECT CONTROLS Supervisor of EMPLOYEE SUPERVISION Dependent_name of DEPENDENT DEPENDENTS_OF
In general, more than one relationship type can exist between the same participating entity types(MANAGES and WORKS_FOR are distinct relationship types between EMPLOYEE and DEPARTMENT (Different meanings and different relationship instances.)
COMPANY DB ER DIAGRAM
Recursive relationship
Both participations are same entity type in different roles. In ER diagram, we need to display role names to
distinguish participations.
For example, SUPERVISION relationships between EMPLOYEE (in role of supervisor or boss) and (another) EMPLOYEE (in role of subordinate or worker).
In following figure, first role participation labeled with 1 and second role participation labeled with 2.
Entity Types
An entity that does not have a key attribute A weak entity must participate in an identifying relationship type with an owner or identifying entity type Weak Entities are identified by the combination of: A partial key of the weak entity type The particular entity they are related to in the identifying entity type Example: A DEPENDENT entity is identified by the dependents first name, and the specific EMPLOYEE with whom the dependent is related Name of DEPENDENT is the partial key DEPENDENT is a weak entity type EMPLOYEE is its identifying entity type via the identifying relationship type DEPENDENT_OF
Weak entity types can be represented as complex (composite, multivalued) attributes. How to decide? Different structures may occur s.a.:
>1 number of levels of weak entity types >1 number of identifying entity type >1 degree of identifying relationship
Shown by placing appropriate numbers on the relationship edges. Existence Dependency (varolma bamll) Constraint (specifies minimum
PARTIAL: zero (optional participation, not existence-dependent) TOTAL: one or more (mandatory participation, existencedependent)
Total shown by double line, partial by single line. ( Look at the COMPANY ER diagram for examples..)
Relationship types of degree 3 are called ternary and of degree n are called n-ary relationship which is not equivalent to n binary relationships.
If needed, the binary and n-ary relationships can all be included in the schema design Determining the cardinalities depends on the scenario.
Which one conveys more information? (s,j,p) (s,p) (j,p) (s,j) One more step further: If we do not want to use weak entity type, how to design?
If a particular binary relationship can be derived from a higherdegree relationship at all times, then it is redundant
the TAUGHT_DURING binary relationship in figure below can be derived from the ternary relationship OFFERS (based on the meaning of the relationships) the OFFERED_DURING binary relationship in figure below can be derived from the ternary relationship OFFERS (based on the meaning of the relationships)
Redundant!
In case CAN_TEACH is 1-1 relationship; what do you think about OFFERS relationship?
Redundant!
1 N
1 1
1
DEPT
STUDENT 1 N 1 PERMIT
1 1 N
GRADE 1
N 1
1
1
N
PROF
1
PLATE
MODEL
YEAR
1
SECTION N
1 N
cs
COURSE
1 GPA
1
MAJOR _GPA
N
STUDENT
1 DEPT GPA
N
STUDENT
DEPT 1 N
STUDENT _MAJOR
GPA 1
MAJOR _GPA
N
STUDENT
N
DEPT
N Dependency problem
1 MAJOR_ GPA
18
EId ENROLL N
GRADE
1 SECTION
cs 1 COURSE
renci ayn SECTIONa ok defalar kayt olup her kayd iin farkl notlar alabilir. Buna engel olmak; yani renci bir SECTIONa bir kez kayt olabilir. koulunu emaya nasl dahil edebiliriz..?
renci bir COURSEa bir kez kayt olabilir. koulunu sorudaki emaya nasl dahil edebiliriz..
19
Attributes relationships
another entity type)
(if an entity type has a single attribute and has only 1 relationship)
Choices concerning the degree of relationships. Top-down&bottomup refinements are defined for large-scale designs.
Example-1
1
S
M
Nehir
N N
lke
Nehir
Example-2
Bir irketteki blmler ve personel bilgilerini tutan Blm-Personel veri tabann dnelim.
Her blm ok sayda personele sahiptir ve her personel en fazla bir blme baldr. Bir blmn sadece bir yneticisi (mutlaka) vardr. Blm iin Bid, personel iin Pid anahtardr.
Buna gre aadaki durumlar birbirinden bamsz olarak cevaplaynz Bir blmn yneticisi ancak o blme bal bir personel olabiliyor ise, Nasl bir ER modeli izilebilir?
1 bid B Y? N P pid
Bir blmn yneticisi herhangi bir personel olabiliyor ise, Nasl bir ER modeli izilebilir?
1 B N y 1 N P
Example-3
SATI FORMU
SatNO: 12 Birim Fiya t Tarih: 11.06.2009
SatrNo 1 2
3
ParaNo
Parasim
Tanm
Spor,basketbol
Miktar 1 2
110 80
klasik tarz
...
GenelTopl am:
880
Yukarda sat bilgileri tutan rnek bir sat formu ierii grlmektedir. Buna karlk gelen veri taban, SATI, PARA ve SATIR olmak zere 3 varlk setinden olumaktadr. (Ayn ParaNolu paralar farkl satrlarda tekrar edebilir.) Bu varlk setlerini, zeliklerini ve aralarndaki ilikileri gsteren ER diyagramn iziniz.
satno tarih
geneltoplam Sat 1 ss N
Satr
Example-4
Trafik sigorta irketine ait srcler ve yaplan kazalar hakknda bilgi saklayan bir veri taban tasarm:
Her srcnn ehliyet no, isim Her aracn plakas Her kaza iin kaza_no, tarih ve yer bilgisi Bir ara bir src zerine kaytlyken, bir srcnn ok sayda arac olabilir. Srcnn hangi ara ile hangi trafik kazasn yaptn ve kazann masraf GPS konum plaka tarih 1 N ehliyetno
src sahip
ara
kaza
isim
yapt
kaza_no
Bir kazada, birden ok araba varsa, yer alan arabalarin bulunmas yukardaki haliyle ancak ayn tarihli(yl/gn/saat/saniye) ve ayn yerdeki kazalarn bulunmas ile tretilebilir. Bu Mantksal (ilikisel model) aamada sorgu ile yaplabilir.. Bu bilgiyi kavramsal aamaya, ERa, tamak farkl yollar ile olabilir: 1. Ayn kaza iersinde yer alan arabalara ait olan kazalara ayn kaza_no verelim. Bunu salamak KAZA varlk setinin zayf olmas ile olabilir; kaza_no zerinden o numarali kazadaki aralara veya srclere ulaabiliriz.. Her araca ait masraf bilgisi tutlabilir.. 2. ARA ile KAZA arasnda N-N lik bir bant tanmlayabiliriz. Bu bantya tarih ve yer bilgisi ekleriz. Her araca ait masraf bilgisi tutlabilir.. 3. ARA-ARA arasnda N-Nlik bir bant tanmlarz.Burda bir kaza_no olmad iin; tarih nitelii multi-value olmas gerekir.. Her araca ait masraf bilgisi tutulamaz..
N
src sahip
plaka
ara
yapt
src
kaza
sahip
ara
N
N
kaza
isim
1
N
src
N
1
kaza_no
tarih
plaka
ara
yapt
GPS konum
sahip
kaza tarih N
kaza_no masraf
isim
Example-5
Birden ok oyuncu ieren takmlar arasnda, iki takm ieren oyunlar ile ilgili bilgiler tutulmak isteniyor. (Oyuncu en fazla bir takmn eleman olabilir. ) her oyunda hangi takmlarn yer ald (hangi takm ev sahibi hangisi misafir olduu) ve oyunun tarihi ve sonucu gibi bilgiler tutuluyor. her oyunda takmn hangi oyuncularnn yer ald bilgisi takip ediliyor.
N
oyuncu ait
takm
1 1
misafir ev
M
oynar
skor
N oyun
PK tarih sonu
skor
Example-6
Bir banka hesabna ait ilemler hakknda ilem zaman(gn,saat), ilem tipi ve miktar bilgileri tutuluyor.bu olay takip eden ER diyagram:
1
hesap ait ait
miktar
ilem
GS gn saat
tip
Farkl hesaplarda ayn saat iinde ayn miktar ve tip ilem olabilir!!!
Yukardaki zm, bir hesap zerinde ayn saat iinde ilem yaplmamas durumunda geerlidir. Bu koul kalkt zaman, tasarm ilem zaman olarak sistem zamann saklamaldr;
1
hesap ait ait
miktar
ilem
GSS gn saat
tip
saniye
Farkl hesaplarda ayn saniye iinde ayn miktar ve tip ilem olabilir!!!
Example-7
(x1,y1)
(x2,y2)
c isimliizgi geometrik nesnesi, ekilde grld gibi (x1,y1), (x2,y2) noktalar ile tanmlanmaktadr. Sadece nokta ve izgi varlklarn tutan bir veri taban tasarlanmak isteniyor. (NOT: varlklarn ismi, zellikleri, ilikilerin cardinality deerlerini belirmeyi unutmayn.., izgilerin yn yok!)
N
NOKTA
izgi
1.yol:
xy x y
isim
N
sim, partial key oluyor
2.yol:
x y
NOKTA1
izgi
isim
ZG
N N
1
izgi
x
NOKTA2
Example-8
Dnya zerindeki gemi limanlar ve gemi hareketlerini tutan bir veri taban tasarm. Limanlarn hangi lkede hangi okyanusta olduu saklanyor. Limanlarn sadece isimleri saklanyor. Farkl lkelerde ayn isimde limanlar olabilir. Gemilerin tipleri (denizalt, yolcu, tekne, sava gemisi gibi.) var, hangi tiplerin varolduu listelenebilmeli.. Her geminin bal olduu bir liman vardr. Bir geminin bir liman ziyaret ile ilgili sadece giri ve k tarihleri saklanmal. Gemi ayn limana ok defalar urayabilir. Gemi hareketleri, geminin tarihesi ile tutulmal. Bu tarihe bilgisi geminin hangi gn va zamanda hangi enlem boylamda bulunduu bilgilerinden rnekler saklamaktadr. Gemi tabiki ayn noktada farkl zamanlarda bulunabilir.
timestamp
Ship_movement
long.
latt. N
history
sname
owner
SHIP
N
T_giri
gemi
ziyaret
T_k
N Liman
gemi
ziyaret
Bir geminin ziyarete balama tarihi sadece bir liman iin olabilir. O zaman VISITS varlk setinin bal olduu gl varlk seti SHIP yeterli.
Tarih(Giri)
T_k
1 Liman
Example 10
Adaylarn irketler ile yaptklar grme ve teklif edilen i pozisyonunu takip etmek istiyoruz.
Aday bir irketin birden ok blm ile grme yapabilir ve her blmden farkl pozisyon teklifler alabilir; bir blmden ancak 1 pozisyon teklifi alabilir. Teklif edilen pozisyonun maasi, zellikleri gibi bir takm zellikler daha tutulmak isteniyor.. Grmenin yapld gn (yl/ay/gn) bilgisi tutluyor. Ayn blm ile grme ancak farkl bir gnde olabiliyor. Gerek aday isimleri gerek irket isimleri sistemde biricik olduunu varsayabiliriz.. Blm hakknda bir bilgi tutulmuyor.
isim
ADAY
pozisyon
N
mlakat
N
BLM
isim
RKET
isim
yapt
Tarih(gn)
N
isim
RKET
isim
ADAY
grme
Tarih(gn) Blm
BT
N
MLAKAT
isim
POZSYON
teklif
maa
......
31