a4b33ds – database systems · 2015-05-04 · relational database technology foreign key: a set...

23
A4B33DS – Database systems Relational database technology

Upload: others

Post on 13-Apr-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

A4B33DS – Database systems

Relational database technology

Page 2: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Relational database technology

Key: a set (possibly a singleton) of attributes (columns), that uniquely identifies the given entity.

Remarks: 1.  An entity type (a table) may have multiple keys. E.g. (i) social security

number (SSN), (ii) personal employee number (PEN), (iii) a synthetized key.

2.  Every table has at least one key. A table of a (real) relational database cannot contain multiple identical rows.

PEN SSN Name Surname Date of birth

101 8811010033 Josef Novák 01/11/88

8811010033101

Name Surname Date of birth

Josef Novák 01/11/88

Josef Novák 01/11/88

Page 3: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Relational database technology

Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value of the key of another (foreign) table.

Remarks: 1.  Data type of each of the foreign key member attribute (column) has

to be compatible with the data type of the corresponding primary key attribute of the referenced table.

2.  The foreign key is not (need to be) a key of the table where it is defined.

3.  Relationship between two entities implemented by the associattion foreign key -> primary key.

4.  A table can have multiple keys => good practice is to choose one of them and use it systematically to implement relationship. primary key,

PEN SSN Name Surname Date of birth

101 8811010033 Josef Novák 01/11/88

PEN Description Calibre

101 Kalašnikov 7,65

SSN License table Brand

8811010033 BE-04-30 Škoda

Page 4: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

http://www.sybase.com/products/modelingmetadata/powerdesigner

Page 5: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Conversion conceptual model -> logical model

•  Entity type -> a table •  Entity type attribute -> column (of a table) •  Relationship:

–  1:1 or 1:N: •  relationship -> foreign key on the side N of the

relationship •  attribute of a relationship -> column of the table on

the side N of the relationship –  N:M:

•  Relationship -> „reference“ table •  Attribute of a relationship -> column of the

reference table

Page 6: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Conceptual model

Logical model

Person Employer

SSN

Name Surname

Name VAT-ID

SSN

Name Surname

Name VAT-ID

SSN VAT-ID

employed by

employed by

Person

Employer

Page 7: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Crow's Foot Notation

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaICO1Uûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi><pi>

DC9,0DC8,0A15A20

<M><M>

Weak entity type

has-lines

VAT-ID Name Phone_prefix

Line-no VAT-ID1 User_name User_surname

Company

Phone-lines

Page 8: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Crow's Foot Notation

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaICO1Uûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi><pi>

DC9,0DC8,0A15A20

<M><M>

Weak entity type

has-lines

VAT-ID Name Phone_prefix

Line-no VAT-ID1 User_name User_surname

Company

Phone-lines

11111111 Nemocnice Motol 224 43 11111111 1111 Ústředna22222222 Honda Motol 234 09 11111111 2920 Kardiocentrum

11111111 2101 Novorozenecké oddělení22222222 1111 Recepce22222222 6690 Prodej nových vozů

Cizí klíč Cizí Primární klíč

Page 9: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Crow's Foot Notation

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaICO1Uûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi><pi>

DC9,0DC8,0A15A20

<M><M>

Weak entity type

has-lines

VAT-ID Name Phone_prefix

Line-no VAT-ID1 User_name User_surname

Company

Phone-lines

11111111 Nemocnice Motol 224 43 11111111 1111 Ústředna22222222 Honda Motol 234 09 11111111 2920 Kardiocentrum

11111111 2101 Novorozenecké oddělení22222222 1111 Recepce22222222 6690 Prodej nových vozů

Cizí klíč Cizí Primární klíč

FK_LINKY_RELATIONS_ORGANIZA

Organizace

ICON· zevTelefon_prefix

DECIMAL(8,0)VARCHAR(30)DECIMAL(9,0)

<pk>

Linky

LinkaICO1ICOUûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

DECIMAL(9,0)DECIMAL(8,0)DECIMAL(8,0)CHAR(15)CHAR(20)

<pk><pk><fk>

Company

VAT-ID Name Phone_prefix

Line-no VAT-ID1 VAT-ID User_name User_surname

Phone-lines

Page 10: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Crow's Foot Notation

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaICO1Uûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi><pi>

DC9,0DC8,0A15A20

<M><M>

Weak entity type

has-lines

VAT-ID Name Phone_prefix

Line-no VAT-ID1 User_name User_surname

Company

Phone-lines

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaUûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi> DC9,0A15A20

<M>

Company

has-lines

Phone-lines

VAT-ID Name Phone_prefix

Line-no User_name User_surname

Page 11: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Crow's Foot Notation

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaICO1Uûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi><pi>

DC9,0DC8,0A15A20

<M><M>

Weak entity type

has-lines

VAT-ID Name Phone_prefix

Line-no VAT-ID1 User_name User_surname

Company

Phone-lines

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaUûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi> DC9,0A15A20

<M>

Company

has-lines

Phone-lines

VAT-ID Name Phone_prefix

Line-no User_name User_surname

Identifying entity type

Weak entity type

Identifying relationship

Page 12: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Crow's Foot Notation

Weak entity type

ma-linky

Organizace

ICON· zevTelefon_prefix

<pi> DC8,0VA30DC9,0

<M><M>

Linky

LinkaUûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

<pi> DC9,0A15A20

<M>

Company

has-lines

Phone-lines

VAT-ID Name Phone_prefix

Line-no User_name User_surname

Conceptual model: Logical model:

FK_LINKY_MA-LINKY_ORGANIZA

Organizace

ICON· zevTelefon_prefix

DECIMAL(8,0)VARCHAR(30)DECIMAL(9,0)

<pk>

Linky

ICOLinkaUûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

DECIMAL(8,0)DECIMAL(9,0)CHAR(15)CHAR(20)

<pk,fk><pk>

VAT-ID Name Phone_prefix

VAT_ID Line-no User_name User_surname

Company

Phone-lines

Page 13: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Physical model

create table Phone-lines ( VAT-ID DECIMAL(8,0) not null, Line-no DECIMAL(9,0) not null, User_name CHAR(15) null, User_surname CHAR(20) null, constraint PK_LINKY primary key (VAT_ID, Line-no)

); create table Company (

VAT-ID DECIMAL(8,0) not null, Name VARCHAR(30) not null, Phone_prefix DECIMAL(9,0) null, constraint PK_ORGANIZACE primary key (VAT-ID)

); alter table Phone-lines add constraint "FK_LINKY_MA-LINKY_ORGANIZA" foreign key (VAT-ID) references Company (VAT-ID) on delete restrict on update restrict;

FK_LINKY_MA-LINKY_ORGANIZA

Organizace

ICON· zevTelefon_prefix

DECIMAL(8,0)VARCHAR(30)DECIMAL(9,0)

<pk>

Linky

ICOLinkaUûivatel-jmÈnoUûivatel-p Ì̄jmenÌ

DECIMAL(8,0)DECIMAL(9,0)CHAR(15)CHAR(20)

<pk,fk><pk>

VAT-ID Name Phone_prefix

VAT_ID Line-no User_name User_surname

Company

Phone-lines

Page 14: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Physical model

create table Phone-lines ( VAT-ID DECIMAL(8,0) not null, Line-no DECIMAL(9,0) not null, User_name CHAR(15) null, User_surname CHAR(20) null, constraint PK_LINKY primary key (VAT_ID, Line-no)

); create table Company (

VAT-ID DECIMAL(8,0) not null, Name VARCHAR(30) not null, Phone_prefix DECIMAL(9,0) null, constraint PK_ORGANIZACE primary key (VAT-ID)

); alter table Phone-lines add constraint "FK_LINKY_MA-LINKY_ORGANIZA" foreign key (VAT-ID) references Company (VAT-ID) on delete restrict on update restrict;

Page 15: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Physical model

create table Linky ( ICO DECIMAL(8,0) not null, Linka DECIMAL(9,0) not null, Uzivatel-jmeno CHAR(15) null, Uzivatel-prijmeni CHAR(20) null, constraint PK_LINKY primary key (ICO, Linka)

); create table Organizace (

ICO DECIMAL(8,0) not null, Nazev VARCHAR(30) not null, Telefon_prefix DECIMAL(9,0) null, constraint PK_ORGANIZACE primary key (ICO)

); alter table Linky add constraint "FK_LINKY_MA-LINKY_ORGANIZA" foreign key (ICO) references Organizace (ICO) on delete restrict on update restrict;

Page 16: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

ma-matku

matka

dÌtÏ

Osoba

Rodne-cisloJmenoPrijemni

<pi> DC10,0A15A20

<M>

Reflexive (recursive) relationship

FK_OSOBA_RELATIONS_OSOBAdÌtÏ

matkaOsoba

Rodne-cisloOso_Rodne-cisloJmenoPrijemni

DECIMAL(10,0)DECIMAL(10,0)CHAR(15)CHAR(20)

<pk><fk>

SSN Name Surname

mother

children

has-mother

Person

child

mother SSN Mother_SSN Name Surname

Person

Page 17: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

ma-matku

matka

dÌtÏ

Osoba

Rodne-cisloJmenoPrijemni

<pi> DC10,0A15A20

<M>

Reflexive (recursive) relationship

FK_OSOBA_RELATIONS_OSOBAdÌtÏ

matkaOsoba

Rodne-cisloOso_Rodne-cisloJmenoPrijemni

DECIMAL(10,0)DECIMAL(10,0)CHAR(15)CHAR(20)

<pk><fk>

SSN Name Surname

mother

children

has-mother

Person

child

mother SSN Mother_SSN Name Surname

Reflexive mandatory reference

A reflexive reference exists should not have a mandatory parent which could lead to inconsistent joins.

Person

Page 18: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

ma-matku

dÌtÏ

matka

Osoba

Rodne-cisloJmenoPrijemni

<pi> DC10,0A15A20

<M>

Reflexive (recursive) relationship

FK_OSOBA_RELATIONS_OSOBAdÌtÏ

matkaOsoba

Rodne-cisloOso_Rodne-cisloJmenoPrijemni

DECIMAL(10,0)DECIMAL(10,0)CHAR(15)CHAR(20)

<pk><fk>

SSN Name Surname

mother

children

has-mother

Person

child

mother SSN Mother_SSN Name Surname

FIX: Make the foreign key an

optional column

Person

Page 19: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Reflexive (recursive) relationship

FK_OSOBA_RELATIONS_OSOBAdÌtÏ

matkaOsoba

Rodne-cisloOso_Rodne-cisloJmenoPrijemni

DECIMAL(10,0)DECIMAL(10,0)CHAR(15)CHAR(20)

<pk><fk>

child

mother SSN Mother_SSN Name Surname

create table Person ( SSN DECIMAL(10,0) not null, Mother_SSN DECIMAL(10,0) null, Name CHAR(15) null, Surname CHAR(20) null, constraint PK_PERSON primary key (SSN) ); alter table Person add constraint FK_OSOBA_RELATIONS_OSOBA foreign key (Mother_SSN) references Person (SSN) on delete restrict on update restrict;

Person

Page 20: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Referential integrity 1

Let us have 2 tables A a B such that table B contains a foreign key referencing table A.

It must not happen that there is a row in table B that refrers to a non-existing row of table A.

Page 21: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Referential integrity

SSN Name Surname SSN (Owner) Serial_numer Producer Type Descriptin1 Josef Novák 1 101 Zbrojovka Vzduchovka Slavie2 Jaroslav Novotný 2 202 Zbrojovka Kalashnikov Vzor 57

Person Weapon

How referential integrity can be violated? a)  We delete a record in table Perosn => foreign key of the respective row in Weapon table will point to a non-existing record in table Person.

a)  We modify the value of the primary key of a record in table Person => foreign key of the respective row in Weapon table will point to a non-existing record in table Person.

Page 22: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Referential integrity 3

drzitel

Osoba

Rodne_cisloJmenoPrijmeni

<pi> <UNDEF><UNDEF><UNDEF>

<M>

Strelna_zbran

Vyrobni_cisloVyrobceTypPopis

<pi><pi>

<UNDEF><UNDEF><UNDEF><UNDEF>

<M><M>

create table Weapon ( Serial_number CHAR(10) not null, Producer CHAR(10) not null, SSN CHAR(10) not null, Type CHAR(10) null, Description CHAR(10) null, constraint PK_WEAPON primary key (Serial_number, Producer) constraint FK_WEAPON_OWNER_PERSON foreign key (SSN) references Person (SSN) on delete restrict on update restrict; );

Person Weapon

SSN Name Surname

Serial number Producer Type Description

SSN Name Surname

owner

Page 23: A4B33DS – Database systems · 2015-05-04 · Relational database technology Foreign key: a set (possibly a singleton) of attributes (columns), the tuple of their values is a value

Referential integrity 4

constraint FK_WEAPON_OWNER_PERSON foreign key (SSN) references Person (SSN) on delete restrict on update restrict;