68
PERANCANGAN DAN PEMODELAN DATABASE-2 Albi Fitransyah, S.Si, M.T

Bab X Perancangan Dan Pemodelan Database-2

Embed Size (px)

Citation preview

Page 1: Bab X Perancangan Dan Pemodelan Database-2

PERANCANGAN DAN

PEMODELAN DATABASE-2

Albi Fitransyah, S.Si, M.T

Page 2: Bab X Perancangan Dan Pemodelan Database-2

WEAK ENTITY SETS An entity set that does not have a primary key is

referred to as a weak entity set. The existence of a weak entity set depends on the

existence of a identifying entity set it must relate to the identifying entity set via a total,

one-to-many relationship set from the identifying to the weak entity set

Identifying relationship depicted using a double diamond The discriminator (or partial key) of a weak entity

set is the set of attributes that distinguishes among all the entities of a weak entity set.

The primary key of a weak entity set is formed by the primary key of the strong entity set on which the weak entity set is existence dependent, plus the weak entity set’s discriminator.

Page 3: Bab X Perancangan Dan Pemodelan Database-2

WEAK ENTITY SETS (CONT.) We depict a weak entity set by double rectangles. We underline the discriminator of a weak entity set with a

dashed line. payment-number – discriminator of the payment entity set Primary key for payment – (loan-number, payment-

number)

Page 4: Bab X Perancangan Dan Pemodelan Database-2

WEAK ENTITY SETS (CONT.)

Note: the primary key of the strong entity set is not explicitly stored with the weak entity set, since it is implicit in the identifying relationship.

If loan-number were explicitly stored, payment could be made a strong entity, but then the relationship between payment and loan would be duplicated by an implicit relationship defined by the attribute loan-number common to payment and loan

Page 5: Bab X Perancangan Dan Pemodelan Database-2

MORE WEAK ENTITY SET EXAMPLES

In a university, a course is a strong entity and a course-offering can be modeled as a weak entity

The discriminator of course-offering would be semester (including year) and section-number (if there is more than one section)

If we model course-offering as a strong entity we would model course-number as an attribute. Then the relationship with course would be implicit in the course-number attribute

Page 6: Bab X Perancangan Dan Pemodelan Database-2

ATRIBUT ATAU HUBUNGAN

Page 7: Bab X Perancangan Dan Pemodelan Database-2

ATRIBUT ATAU HUBUNGAN

Page 8: Bab X Perancangan Dan Pemodelan Database-2

PEMODELAN DATA BERGANTUNG WAKTU

Page 9: Bab X Perancangan Dan Pemodelan Database-2

PEMODELAN DATA BERGANTUNG WAKTU

Page 10: Bab X Perancangan Dan Pemodelan Database-2

PEMODELAN DATA BERGANTUNG WAKTU

Page 11: Bab X Perancangan Dan Pemodelan Database-2

MASALAH MODEL E-R Fan Trap: jebakan yg membuat

hubungan antara instans-instans entitas menjadi rancu.

Chasm Trap: Jebakan yang membuat instans entitas tertentu kehilangan hubungan.

Page 12: Bab X Perancangan Dan Pemodelan Database-2

SPECIALIZATION Top-down design process; we designate subgroupings

within an entity set that are distinctive from other entities in the set.

These subgroupings become lower-level entity sets that have attributes or participate in relationships that do not apply to the higher-level entity set.

Depicted by a triangle component labeled ISA (E.g. customer “is a” person).

Attribute inheritance – a lower-level entity set inherits all the attributes and relationship participation of the higher-level entity set to which it is linked.

Page 13: Bab X Perancangan Dan Pemodelan Database-2

SPECIALIZATION EXAMPLE

Page 14: Bab X Perancangan Dan Pemodelan Database-2

GENERALIZATION A bottom-up design process – combine a number of

entity sets that share the same features into a higher-level entity set.

Specialization and generalization are simple inversions of each other; they are represented in an E-R diagram in the same way.

The terms specialization and generalization are used interchangeably.

Page 15: Bab X Perancangan Dan Pemodelan Database-2

SPECIALIZATION AND GENERALIZATION (CONTD.)

Can have multiple specializations of an entity set based on different features.

E.g. permanent-employee vs. temporary-employee, in addition to officer vs. secretary vs. teller

Each particular employee would be a member of one of permanent-employee or temporary-

employee, and also a member of one of officer, secretary, or teller

The ISA relationship also referred to as superclass - subclass relationship

Page 16: Bab X Perancangan Dan Pemodelan Database-2

DESIGN CONSTRAINTS ON A SPECIALIZATION/GENERALIZATION

Constraint on which entities can be members of a given lower-level entity set. condition-defined

E.g. all customers over 65 years are members of senior-citizen entity set; senior-citizen ISA person.

user-defined Constraint on whether or not entities may

belong to more than one lower-level entity set within a single generalization. Disjoint

an entity can belong to only one lower-level entity set Noted in E-R diagram by writing disjoint next to the ISA

triangle Overlapping

an entity can belong to more than one lower-level entity set

Page 17: Bab X Perancangan Dan Pemodelan Database-2

DESIGN CONSTRAINTS ON A SPECIALIZATION/GENERALIZATION (CONTD.)

Completeness constraint -- specifies whether or not an entity in the higher-level entity set must belong to at least one of the lower-level entity sets within a generalization. total : an entity must belong to one of the lower-level

entity sets partial: an entity need not belong to one of the lower-

level entity sets

Page 18: Bab X Perancangan Dan Pemodelan Database-2

AGGREGATION Consider the ternary relationship works-on, which we saw earlier

Suppose we want to record managers for tasks performed by an employee at a branch

Page 19: Bab X Perancangan Dan Pemodelan Database-2

AGGREGATION (CONT.) Relationship sets works-on and manages represent

overlapping information Every manages relationship corresponds to a works-on

relationship However, some works-on relationships may not correspond to

any manages relationships So we can’t discard the works-on relationship

Eliminate this redundancy via aggregation Treat relationship as an abstract entity Allows relationships between relationships Abstraction of relationship into new entity

Without introducing redundancy, the following diagram represents: An employee works on a particular job at a particular branch An employee, branch, job combination may have an associated

manager

Page 20: Bab X Perancangan Dan Pemodelan Database-2

E-R DIAGRAM WITH AGGREGATION

Page 21: Bab X Perancangan Dan Pemodelan Database-2

E-R DESIGN DECISIONS The use of an attribute or entity set to

represent an object. Whether a real-world concept is best

expressed by an entity set or a relationship set.

The use of a ternary relationship versus a pair of binary relationships.

The use of a strong or weak entity set. The use of specialization/generalization –

contributes to modularity in the design. The use of aggregation – can treat the

aggregate entity set as a single unit without concern for the details of its internal structure.

Page 22: Bab X Perancangan Dan Pemodelan Database-2

E-R DIAGRAM FOR A BANKING ENTERPRISE

Page 23: Bab X Perancangan Dan Pemodelan Database-2

MODEL RELASIONAL Model Relasional: model tabel Dikemukakan pertama kali oleh E. F.

Codd tahun 1970. Muncul RDBMS: System R, Ingress,

Access, FoxPro, SQL Server, Paradox, Visual dBase

Komponen Model Data Relasional:Struktur DataPemanipulasi Data Integritas Data

Page 24: Bab X Perancangan Dan Pemodelan Database-2

ISTILAH-ISTILAH MODEL DATA RELASIONAL Relasi Atribut Tuple Domain Derajat Kardinalitas Candidate key Primary key Foreign key Alternatif

Pokok bahasan SQL

Page 25: Bab X Perancangan Dan Pemodelan Database-2

SIFAT RELASI Setiap relasi dalam sebuah database

harus memiliki nama yang unik. Setiap sel dalam relasi harus bersifat

atomik. Atomik: bernilai tunggal. Setiap atribut dalam tabel harus memiliki

nama yang unik. Nilai setiap atribut harus berdomain sama Urutan atribut dalam relasi tidak penting. Setiap baris harus bisa dibedakan secara

unik melalui primary key Urutan baris dalam relasi tidak penting

Page 26: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI MODEL E-R KE RELASI Transformasi Tipe Entitas Kuat Transformasi Tipe Entitas Lemah Transformasi Hubungan Binary Transformasi Entitas Asosiatif Transformasi Hubungan Unary Transformasi Hubungan Tertiary

Page 27: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI TIPE ENTITAS KUAT Tipe entitas kuat: tipe entitas yg

keberadaannya tidak bergantung pada tipe entitas lain.

Dinyatakan dengan tanda kotak. Bentuk: atribut sederhana, atribut

komposit, atribut bernilai ganda, atribut turunan, gabungan beberapa atribut.

Page 28: Bab X Perancangan Dan Pemodelan Database-2

ATRIBUT SEDERHANA

Page 29: Bab X Perancangan Dan Pemodelan Database-2

ATRIBUT KOMPOSIT

Page 30: Bab X Perancangan Dan Pemodelan Database-2

ATRIBUT BERNILAI BANYAK

Page 31: Bab X Perancangan Dan Pemodelan Database-2

ATRIBUT TURUNAN

Page 32: Bab X Perancangan Dan Pemodelan Database-2

GABUNGAN BERBAGAI ATRIBUT

Page 33: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI TIPE ENTITAS LEMAH Tipe entitas TANGGUNGAN berisi data anak para

pegawai yg ditanggung oleh perusahaan. Tipe entitas PEGAWAI dinamakan identifying owner Buat relasi yang namanya sesuai dengan nama

pada tipe entitas Masukkan primary key milik tipe entitas yang

bertindak sebagai identifying owner ke relasi tersebut

Pindahkan atribut-atribut tipe entitas lemah ke dalam relasi tersebut dengan mengikuti aturan seperti pada tipe entitas kuat.

Jadikan atribut yang berasal dari identifying owner dan pengenal parsial sebagai primary key yang bersifat komposit.

Page 34: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI TIPE ENTITAS LEMAH

Nomor_Pegawai

Page 35: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI HUBUNGAN BINARY Hubungan binary: hubungan yang

melibatkan 2 buah tipe entitas. Hubungan 1:1 Hubungan 1:M Hubungan M:N

Page 36: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN 1:1 Transformasikan masing-masing tipe

entitas ke dalam bentuk relasi, termasuk dengan primary key. Aturan-aturan yang berlaku di depan bisa digunakan untuk menyusun pembentukan relasi.

Tambahkan primary key salah satu relasi ke relasi yang lain untuk membentuk hubungan foreign key dan primary key.

Page 37: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN 1:1

Page 38: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN 1:1 Mengapa yang perlu ditambahi adalah

relasi CABANG bukan relasi PEGAWAI? Jwb: Jika PEGAWAI ditambahi dengan

atribut berasal dari primary key CABANG, maka akan banyak baris yang Kode_Cabang-nya tidak berisi (NULL).

Catatan: Relasi yang terkait dengan tipe entitas yang ditandai dengan kardinalitas minimum berupa 0 pada sisinya-lah yang akan diberi tambahan atribut primary key milik relasi pasangannya.

Page 39: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN 1:M

PEGAWAI adalah: tipe entitas yang berisi “banyak”. Sehingga, relasi yg terkait dengan tipe entitas inilah yg mendapat tambahan atribut Kode_Cabang.

Page 40: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN M:N Bentuk relasi-relasi yang sesuai dengan

kedua tipe entitas dengan mengikuti aturan-aturan yang telah dibahas di depan. Primary key kedua relasi = atribut yang menjadi primary key pada masing-masing tipe entitas.

Bentuk relasi baru yang mewakili hubungan tipe entitas dan kemudian lakukanlah langkah-langkah: Tambahkan ke dalam relasi, atribut yg merupakan

primary key kedua entitas. Jadikan kedua atribut tersebut sebagai composite

key Pindahkan atribut-atribut dalam hubungan ke

relasi

Page 41: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN M:NTanggal_Penugasa

n

Page 42: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI ENTITAS ASOSIATIF Bentuk 2 buah relasi yang terkait

dengan masing-masing tipe entitas Bentuk relasi untuk mewakili entitas

asosiatif.

Page 43: Bab X Perancangan Dan Pemodelan Database-2

PRIMARY KEY BELUM TERSEDIA

Page 44: Bab X Perancangan Dan Pemodelan Database-2

PRIMARY KEY SUDAH TERSEDIA

Page 45: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI HUBUNGAN UNARY Hubungan unary 1:1 Hubungan unary 1:M Hubungan unary M:N

Page 46: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN UNARY 1:1 Bentuk relasi dengan nama sama

dengan tipe entitas Isikan semua atribut dalam tipe entitas

ke dalam relasi Jadikan atribut yang menjadi primary

key dalam tipe entitas menjadi primary key relasi

Tambahkan atribut dalam relasi tersebut yang bertindak sebagai foreign key yang merujuk ke primary key.

Page 47: Bab X Perancangan Dan Pemodelan Database-2
Page 48: Bab X Perancangan Dan Pemodelan Database-2
Page 49: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN 1:M

Page 50: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN M:N

Page 51: Bab X Perancangan Dan Pemodelan Database-2

HUBUNGAN M:N

Page 52: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI HUBUNGAN TERTIARY Hubungan yang bersifat tertiary bisa

diterjemahkan dalam bentuk entitas asosiatif.

Setelah dinyatakan dalam bentuk entitas asosiatif, diagram E-R dapat diterjemahkan ke relasi dengan beberapa cara sbb.

Page 53: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI HUBUNGAN TERTIARY Transformasikan masing-masing tipe

entitas ke dalam relasi Bentuk relasi untuk mewakili entitas

asosiatif. Kemudian:Masukkan primary key ketiga relasi ke relasi

ini Jadikan ketiga atribut tersebut sebagai

kunci komposit.Tambahkan semua atribut yg melekat dlm

entitas asosiatif ke relasi ini

Page 54: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI HUBUNGAN TERTIARY Contoh hubungan tertiary ditunjukkan

pada Gambar 4.27, yaitu:Seorang staf menangani satu atau banyak

klien dan seorang klien ditangani oleh satu staf

Seorang staf menangani satu atau banyak pewawancara dan satu pewawancara berhubungan dengan hanya satu staf.

Seorang klien diwawancarai oleh satu atau banyak pewawancara dan seorang pewawancara bisa mewawancarai satu atau banyak klien.

Page 55: Bab X Perancangan Dan Pemodelan Database-2

TRANSFORMASI HUBUNGAN TERTIARY

Page 56: Bab X Perancangan Dan Pemodelan Database-2

EXAMPLE OF A RELATION

Page 57: Bab X Perancangan Dan Pemodelan Database-2

BASIC STRUCTURE Formally, given sets D1, D2, …. Dn a relation r is a subset

of D1 x D2 x … x Dn

Thus a relation is a set of n-tuples (a1, a2, …, an) where each ai Di

Example: ifcustomer-name = {Jones, Smith, Curry, Lindsay}customer-street = {Main, North, Park}customer-city = {Harrison, Rye, Pittsfield}

Then r = { (Jones, Main, Harrison), (Smith, North, Rye), (Curry, North, Rye), (Lindsay, Park, Pittsfield)} is a relation over customer-name x customer-street x customer-city

Page 58: Bab X Perancangan Dan Pemodelan Database-2

ATTRIBUTE TYPES Each attribute of a relation has a name The set of allowed values for each attribute is

called the domain of the attribute Attribute values are (normally) required to be

atomic, that is, indivisible E.g. multivalued attribute values are not atomic E.g. composite attribute values are not atomic

The special value null is a member of every domain

The null value causes complications in the definition of many operations we shall ignore the effect of null values in our

main presentation and consider their effect later

Page 59: Bab X Perancangan Dan Pemodelan Database-2

RELATION SCHEMA A1, A2, …, An are attributes R = (A1, A2, …, An ) is a relation schema

E.g. Customer-schema = (customer-name, customer-street, customer-city)

r(R) is a relation on the relation schema R

E.g. customer (Customer-schema)

Page 60: Bab X Perancangan Dan Pemodelan Database-2

RELATION INSTANCE The current values (relation instance) of a

relation are specified by a table An element t of r is a tuple, represented

by a row in a table

JonesSmithCurry

Lindsay

customer-name

MainNorthNorthPark

customer-street

HarrisonRyeRye

Pittsfield

customer-city

customer

attributes(or columns)

tuples(or rows)

Page 61: Bab X Perancangan Dan Pemodelan Database-2

RELATIONS ARE UNORDERED Order of tuples is irrelevant (tuples may be stored in an arbitrary order) E.g. account relation with unordered tuples

Page 62: Bab X Perancangan Dan Pemodelan Database-2

DATABASE A database consists of multiple relations Information about an enterprise is broken up into parts,

with each relation storing one part of the information

E.g.: account : stores information about accounts depositor : stores information about which customer owns which account customer : stores information about customers

Storing all information as a single relation such as bank(account-number, balance, customer-name, ..)results in repetition of information (e.g. two customers own an account) the need for null values (e.g. represent a customer without an

account) Normalization theory (Chapter 7) deals with how to

design relational schemas

Page 63: Bab X Perancangan Dan Pemodelan Database-2

THE CUSTOMER RELATION

Page 64: Bab X Perancangan Dan Pemodelan Database-2

THE DEPOSITOR RELATION

Page 65: Bab X Perancangan Dan Pemodelan Database-2

E-R DIAGRAM FOR THE BANKING ENTERPRISE

Page 66: Bab X Perancangan Dan Pemodelan Database-2

KEYS Let K R K is a superkey of R if values for K are sufficient

to identify a unique tuple of each possible relation r(R) by “possible r” we mean a relation r that could exist in

the enterprise we are modeling. Example: {customer-name, customer-street} and

{customer-name} are both superkeys of Customer, if no two customers can possibly have the same name.

K is a candidate key if K is minimalExample: {customer-name} is a candidate key for Customer, since it is a superkey (assuming no two customers can possibly have the same name), and no subset of it is a superkey.

Page 67: Bab X Perancangan Dan Pemodelan Database-2

DETERMINING KEYS FROM E-R SETS Strong entity set. The primary key of the entity

set becomes the primary key of the relation. Weak entity set. The primary key of the relation

consists of the union of the primary key of the strong entity set and the discriminator of the weak entity set.

Relationship set. The union of the primary keys of the related entity sets becomes a super key of the relation. For binary many-to-one relationship sets, the primary key

of the “many” entity set becomes the relation’s primary key.

For one-to-one relationship sets, the relation’s primary key can be that of either entity set.

For many-to-many relationship sets, the union of the primary keys becomes the relation’s primary key

Page 68: Bab X Perancangan Dan Pemodelan Database-2

SCHEMA DIAGRAM FOR THE BANKING ENTERPRISE