Upload
hi11et
View
104
Download
4
Tags:
Embed Size (px)
Citation preview
Ders # 2
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
Overview of Database Design Process
Two main activities:
Database design
Applications design
Applications design focuses on the programs and interfaces that access the database
Generally considered part of software engineering
Use Data-flow, sequence
diagrams or scenarios
ER
ER Model Concepts- Entities, Attributes
Entities and Attributes 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
Attributes are properties used to describe an entity. For example an EMPLOYEE entity may have the attributes
Name, SSN, Address, Sex, BirthDate
A specific entity will have a value for each of its attributes. 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, …
ER Model Concepts -Entity Types and Key Attributes
Entities with the same basic attributes are grouped or typed into an entity type. ( EMPLOYEE and PROJECT are entity types) an entity type is displayed in a rectangular box in ER diagram.
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
For example, SSN of EMPLOYEE.
A key attribute may be composite. (At the next slide, VehicleTagNumber is a key of the CAR entity type with components (Number, State).)
An entity type may have more than one key. (At the next slide, VehicleIdentificationNumber (popularly called VIN), VehicleTagNumber (Number, State), aka license plate number.)
Types of attribute: Simple, Composite, Multivalued
Simple: Each entity has a single atomic value for the attribute. (SSN or DateofBirth)
Composite: composition of several components. Address(Apt#, House#, Street, City, State, ZipCode, Country), or Name(FirstName, MiddleName, LastName).
Multi-valued: existence of multiple values for that attribute. (For example, Color of a CAR or PreviousDegrees of a STUDENT. (Denoted as {Color} or {PreviousDegrees}. )
EXAMPLE: Entity Type CAR with
two keys and a corresponding
Entity Set Composite
attribute
A key
A key
Multivalued
attr.
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
ER Model Concepts-
Entity Set
three CAR entity instances in the entity set for CAR
ER Model Concepts :Relationships, Relationship Types and
Relationship Set A relationship relates two or more distinct entities with a specific
meaning. For example, EMPLOYEE John Smith works on the ProductX PROJECT, or
EMPLOYEE Franklin Wong manages the Research DEPARTMENT.
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)
Relationship Set: The current state of a relationship type.
Example COMPANY Database 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 EMPLOYEE’s 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.
Initial Design of COMPANY DB Based on the requirements, we can identify four initial entity types in the COMPANY
database:
DEPARTMENT, PROJECT, EMPLOYEE and DEPENDENT
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
Recursive relationship type
In a recursive relationship type. 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.
Another Entity Type: Weak 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 dependent’s 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
Attributes and Constraints of Relationships A relationship type can have attributes: (HoursPerWeek of
WORKS_ON )
A value of HoursPerWeek depends on a particular (employee, project) combination
Most relationship attributes are used with M:N relationships
In 1:N relationships, they can be transferred to the entity type on the N-side of the relationship
Cardinality Ratio (eleman sayısı oranı) (specifies maximum participation) : (1:1),(1:N) or (N:1), (M:N)
Shown by placing appropriate numbers on the relationship edges.
Existence Dependency (varolma bağımlılığı) Constraint (specifies minimum participation, called participation constraint : TOTAL or PARTIAL )
PARTIAL: zero (optional participation, not existence-dependent)
TOTAL: one or more (mandatory participation, existence-dependent)
Total shown by double line, partial by single line. ( Look at the COMPANY ER diagram for examples..)
Summary of notation for ER
diagrams
Relationships of Higher Degree 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?
Redundancy in n-ary relationships If a particular binary relationship can be derived from a higher-
degree 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!
Redundant!
In case CAN_TEACH is 1-1
relationship; what do you think
about OFFERS relationship?
Constraint on Ternary relationships
Determine the key of SUPPLY..? M
N
1
M
1
1
Determine the key of SUPPLY..?
• It is usually hard to determine the constraints..
• An M or N indicates no constraint..
• A 1 indicates that an entity can participate in at most one relationship
instance that has a particular combination of the other participating entities
Redundancy / dependency problem in ER (in general)
18
ENROLL
PROF
SECTION
N
1 N
1
N
1
GRADE
N
1 N
STUDENT
N
N
YEAR
1
N
1
N
1
se N
COURSE 1
cs N
DEPT
1
1 N
1
1
PERMIT
1
1
PLATE MODEL
1
N
STUDENT
1 N
GPA DEPT
N N
1 N MAJOR_GPA
STUDENT
1 N
GPA DEPT
1 N
1 N MAJOR
_GPA
STUDENT
1 N
GPA
DEPT
N
1
STUDENT
_MAJOR
N
1
MAJOR_
GPA
N
1
Dependency problem
Redundancy problem in ER (in general)
19
ENROLL
SECTION
N
1
STUDENT 1
se N
COURSE 1 cs
N
1
GRADE
N EId Öğrenci aynı SECTION’a çok
defalar kayıt olup her kaydı
için farklı notlar alabilir. Buna
engel olmak; yani «öğrenci
bir SECTION’a bir kez kayıt
olabilir.» koşulunu şemaya
nasıl dahil edebiliriz..?
«Öğrenci bir COURSE’a bir kez kayıt olabilir.» koşulunu sorudaki şemaya
nasıl dahil edebiliriz..
Some refinements after design Attributes relationships (if attribute refers to
another entity type)
Attributes entity types (if an attribute exists in
several entity types)
entity type attribute (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 Şehirler, ülkeler ve nehirleri gösteren bir veritabanı
tasarımı.
Şehir Ülke
Nehir
R
S
1 N N
M
Nehirlerin hangi şehirlerden geçtiğini tutmak istersek?
Şehir Ülke
Nehir
R
S
1 N
N M
Example-2 Bir şirketteki bölümler ve personel bilgilerini tutan Bölüm-Personel veri
tabanını düşünelim. Her bölüm çok sayıda personele sahiptir ve her personel en fazla bir
bölüme bağlıdır.
Bir bölümün sadece bir yöneticisi (mutlaka) vardır.
Bölüm için Bid, personel için Pid anahtardır.
Buna göre aşağıdaki durumları birbirinden bağımsız olarak cevaplayınız…
Bir bölümün yöneticisi ancak o bölüme bağlı bir personel olabiliyor ise, Nasıl bir ER modeli çizilebilir?
Bir bölümün yöneticisi herhangi bir personel olabiliyor ise, Nasıl bir ER modeli çizilebilir?
1 N
1 N
1 N
B P ç
Y?
B P ç
y
bid pid
Example-3
SATIŞ FORMU
SatışNO: 12 Tarih: 11.06.2009
SatırNo ParçaNo Parçaİsim Tanım Miktar
Birim
Fiya
t Toplam
1 23645 ayakkabı Spor,basketbol 1 110 110
2 12674 gomlek klasik tarz 2 80 160
3 ... ...
GenelTopl
am: 880
Yukarıda satış bilgileri tutan örnek bir satış formu içeriği görülmektedir. Buna karşılık gelen
veri tabanı, SATIŞ, PARÇA ve SATIR olmak üzere 3 varlık setinden oluşmaktadır. (Aynı
ParçaNolu parçalar farklı satırlarda tekrar edebilir.)
Bu varlık setlerini, özeliklerini ve aralarındaki ilişkileri gösteren ER diyagramını çiziniz.
Satış
Satır
ss
satışno
1
N
ps
Parça
tarih
geneltoplam
1
N
satırno
toplam miktar
Not: satırno,
partial key oluyor.
pisim
parçano Birim fiyat
tanım
Satır
ss
N
Trafik sigorta şirketine ait sürücüler ve yapılan kazalar hakkında bilgi saklayan bir veri tabanı tasarımı:
Her sürücünün ehliyet no, isim
Her aracın plakası
Her kaza için kaza_no, tarih ve yer bilgisi
Bir araç bir sürücü üzerine kayıtlıyken, bir sürücünün çok sayıda aracı olabilir.
Sürücünün hangi araç ile hangi trafik kazasını yaptığını ve kazanın masrafı
Example-4
sürücü araç sahip
1 N
yaptı N
tarih
kaza
1
sürücü araç sahip 1 N
kaza
N
tarih N
ehliyetno
isim
plaka
kaza_no
Bir kazada, birden çok araba varsa, yer alan arabalarin bulunması yukarıdaki haliyle ancak aynı tarihli(yıl/gün/saat/saniye) ve aynı
yerdeki kazaların bulunması ile türetilebilir. Bu Mantıksal (ilişkisel model) aşamada sorgu ile yapılabilir.. Bu bilgiyi kavramsal aşamaya, ER’a, taşımak farklı yollar ile olabilir:
1. Aynı kaza içersinde yer alan arabalara ait olan kazalara aynı kaza_no verelim. Bunu sağlamak KAZA varlık setinin zayıf olması ile
olabilir; kaza_no üzerinden o numarali kazadaki araçlara veya sürücülere ulaşabiliriz.. Her araca ait masraf bilgisi tutlabilir.. 2. ARAÇ ile KAZA arasında N-N lik bir bağıntı tanımlayabiliriz. Bu bağıntıya tarih ve yer bilgisi ekleriz. Her araca ait masraf bilgisi
tutlabilir.. 3. ARAÇ-ARAÇ arasında N-N’lik bir bağıntı tanımlarız.Burda bir kaza_no olmadığı için; tarih niteliği multi-value olması gerekir.. Her araca
ait masraf bilgisi tutulamaz..
GPS konum
sürücü araç sahip
yaptı N
kaza
1 isim
plaka GPS konum
tarih
kaza_no
1 N
sürücü araç sahip
yaptı N
kaza
N isim
plaka GPS konum
tarih
kaza_no
1 N
masraf
masraf
Example-5 Birden çok oyuncu içeren takımlar arasında, iki takım içeren oyunlar
ile ilgili bilgiler tutulmak isteniyor. (Oyuncu en fazla bir takımın elemanı olabilir. )
her oyunda hangi takımların yer aldığı (hangi takım ev sahibi hangisi misafir olduğu) ve oyunun tarihi ve sonucu gibi bilgiler tutuluyor.
her oyunda takımın hangi oyuncularının yer aldığı bilgisi takip ediliyor.
oyuncu takım ait
1 N
ev
N skor
tarih
oyun
1
misafir
N
1 skor oynar
N
M
sonuç
PK
Example-6 Bir banka hesabına ait işlemler hakkında işlem zamanı(gün,saat), işlem tipi
ve miktar bilgileri tutuluyor.bu olayı takip eden ER diyagramı:
hesap işlem ait
1 N
gün saat
GS tip
miktar Not: gün,saat
partial key oluyor. işlem ait
Yukarıdaki çözüm, bir hesap üzerinde aynı saat içinde işlem yapılmaması durumunda geçerlidir. Bu koşul kalktığı zaman, tasarım işlem zamanı olarak sistem zamanını saklamalıdır;
hesap işlem ait
1 N
gün saat
GSS tip
miktar Not:
gün,saat,saniye partial key oluyor.
işlem ait
saniye
Farklı hesaplarda aynı saat içinde
aynı miktar ve tip işlem olabilir!!!
Farklı hesaplarda aynı saniye içinde
aynı miktar ve tip işlem olabilir!!!
c isimli“çizgi” geometrik nesnesi, şekilde görüldüğü gibi (x1,y1), (x2,y2) noktaları ile tanımlanmaktadır.
Sadece nokta ve çizgi varlıklarını tutan bir veri tabanı tasarlanmak isteniyor. (NOT: varlıkların ismi, özellikleri, ilişkilerin cardinality değerlerini belirmeyi unutmayın.., çizgilerin yönü yok!)
Example-7 (x1,y1)
(x2,y2)
NOKTA çizgi
N
N
x y
xy
isim
c
NOKTA1 NOKTA2
x y x y isim
1 N
1
1.yol:
2.yol:
ÇİZGİ çizgi çizgi ÇİZGİ
N
İsim, partial key oluyor
Example-8
Dünya üzerindeki gemi limanları ve gemi hareketlerini tutan bir veri tabanı tasarımı.
Limanların hangi ülkede hangi okyanusta olduğu saklanıyor. Limanların sadece isimleri saklanıyor. Farklı ülkelerde aynı isimde limanlar olabilir.
Gemilerin tipleri (denizaltı, yolcu, tekne, savaş gemisi gibi.) var, hangi tiplerin varolduğu listelenebilmeli..
Her geminin bağlı olduğu bir liman vardır. Bir geminin bir limanı ziyaret ile ilgili sadece giriş ve çıkış tarihleri saklanmalı. Gemi aynı limana çok defalar uğrayabilir.
Gemi hareketleri, geminin tarihçesi ile tutulmalı. Bu tarihçe bilgisi geminin hangi gün va zamanda hangi enlem boylamda bulunduğu bilgilerinden örnekler saklamaktadır. Gemi tabiki aynı noktada farklı zamanlarda bulunabilir.
FIGURE 7.7 from Elamsri/Navathe
An ER schema for a SHIP_TRACKING database.
Liman
ziyaret
gemi N
N
T_çıkış T_giriş
SHIP
history
Ship_movement
timestamp
long.
latt.
sname
owner
N
1
Liman
ziyaret
gemi N
1
T_çıkış Tarih(Giriş) N
Bir geminin ziyarete başlama tarihi sadece bir liman için olabilir. O zaman
VISITS varlık setinin bağlı olduğu güçlü varlık seti SHIP yeterli.
Example-9 Figure 3.17, Elmasri/Navathe book
Example 10 Adayların şirketler ile yaptıkları görüşme ve teklif edilen iş pozisyonunu takip etmek
istiyoruz.
Aday bir şirketin birden çok bölümü ile görüşme yapabilir ve her bölümden farklı pozisyon
teklifler alabilir; bir bölümden ancak 1 pozisyon teklifi alabilir.
Teklif edilen pozisyonun maasi, özellikleri gibi bir takım özellikler daha tutulmak isteniyor..
Görüşmenin yapıldığı gün (yıl/ay/gün) bilgisi tutluyor. Aynı bölüm ile görüşme ancak farklı
bir günde olabiliyor.
Gerek aday isimleri gerek şirket isimleri sistemde biricik olduğunu varsayabiliriz..
Bölüm hakkında bir bilgi tutulmuyor.
31
ADAY BÖLÜM mülakat
N
yaptı N Tarih(gün)
ŞİRKET
1
isim isim
N
isim pozisyon
MÜLAKAT
görüşme ADAY ŞİRKET
BT
Tarih(gün)
Bölüm
N
teklif N
POZİSYON
1
isim
maaş ......
1 1 isim isim