Typy relací

Zde je uveden příklad pro lepší porozumění problematice relací.
 

Identifikační relace

Primární klíč z parent entity migruje do child entity, a tam je součástí primárního klíče. Používá se tehdy, pokud primární klíč cizí entity není schopen zajistit jednoznačnou identifikaci. Entita, která je child entitou propojenou přes identifikační relaci, je tzv. závislou (dependent) entitou , a je v modelu zobrazena se zaoblenými rohy.
 
 
Příklad:
 
// parent entity
Create table Invoice (
InvoiceNo Integer NOT NULL,
ExportDate Date,
BLN_Supplier Char(10) NOT NULL) ;
 
// child entity
Create table InvItem (
InvoiceNo Integer NOT NULL,
InvLineNo Integer NOT NULL,
Amount Integer) ;
 
Alter table Invoice add primary key (InvoiceNo);
Alter table InvItem add primary key (InvoiceNo,InvLineNo);
Alter table InvItem add foreign key (InvoiceNo) references Invoice (InvoiceNo);
Číslo řádku je jako jedinečný identifikátor záznamu nedostatečný, child entita si musí vypomoci Číslem faktury (InvoiceNo). Proto je použita identifikační relace. Položka faktury nemůže existovat bez samotné faktury, proto je položka faktury závislou entitou.

Neidentifikační relace

Primární klíč z parent entity migruje do child entity, a tam neni součásti primárního klíče.
 
Příklad:
Create table Invoice (
InvoiceNo Integer NOT NULL,
ExportDate Date, BLN_Supplier Char(10) NOT NULL);
 
Create table AddressBook (
BLN_Supplier Char(10) NOT NULL,
CompanyName Char(50),
BLN_Customer Char(10) NOT NULL);
 
Alter table Invoice add primary key (InvoiceNo);
Alter table AddressBook add primary key (BLN_Supplier);
Alter table Invoice add foreign key (BLN_Supplier) references AddressBook (BLN_Supplier);
Alter table AddressBook add foreign key (BLN_Customer) references AddressBook (BLN_Supplier) on delete cascade;
Číslo faktury je jako jedinečná identifikace faktury zcela postačující. Proto je použita neidentifikační relace, ičo je pouze cizím klíčem. Faktura může existovat bez návaznosti na firmu, proto je faktura nezávislou entitou.
 

Relace M:N

 
Příklad:
Create table Sup_Price (
BLN_Supplier Char(10) NOT NULL,
ArticleId Integer NOT NULL) ;
 
Alter table Sup_Price add primary key (BLN_Supplier,ArticleId);
Alter table Sup_Price add foreign key (BLN_Supplier) references AddressBook (BLN_Supplier);
Alter table Sup_Price add foreign key (ArticleId) references PriceList (ArticleId) ;

Informativní Relace

Primární klíč nikam nemigruje, negeneruje se žádná referenční integrita ani triggery, jedná se pouze o informaci, že nějaký vztah existuje.
 

Self Relace

K vytvoření Self relace je zapotřebí kliknout pravým tlačítkem myši na entitu a vybrat "Přidat Self Relaci"
 
 
 
Příklad:
Create table AddressBook (
BLN_Supplier Char(10) NOT NULL,
CompanyName Char(50),
BLN_Customer Char(10) NOT NULL) ;
 
Alter table AddressBook add primary key (BLN_Supplier);
Alter table AddressBook add foreign key (BLN_Customer) references AddressBook (BLN_Supplier) on delete cascade;

Doplňující informace

Generated SQL Script

/* Database Oracle 8 */
 
Create table Invoice (
InvoiceNo Integer NOT NULL,
ExportDate Date,
BLN_Supplier Char(10) NOT NULL) ;
 
Create table InvItem (
InvoiceNo Integer NOT NULL,
InvLineNo Integer NOT NULL,
Amount Integer) ;
 
Create table AddressBook (
BLN_Supplier Char(10) NOT NULL,
CompanyName Char(50),
BLN_Customer Char(10) NOT NULL) ;
 
Create table PriceList (
ArticleId Integer NOT NULL,
Artice Char(20),
Price Number(10,2)) ;
 
Create table Sup_Price (
BLN_Supplier Char(10) NOT NULL,
ArticleId Integer NOT NULL) ;
 
Alter table Invoice add primary key (InvoiceNo);
Alter table InvItem add primary key (InvoiceNo,InvLineNo);
Alter table AddressBook add primary key (BLN_Supplier);
Alter table PriceList add primary key (ArticleId);
Alter table Sup_Price add primary key (BLN_Supplier,ArticleId);
Alter table InvItem add foreign key (InvoiceNo) references Invoice (InvoiceNo) ;
Alter table Sup_Price add foreign key (BLN_Supplier) references AddressBook (BLN_Supplier);
Alter table AddressBook add foreign key (BLN_Customer) references AddressBook (BLN_Supplier) on delete cascade;
Alter table Invoice add foreign key (BLN_Supplier) references AddressBook (BLN_Supplier);
Alter table Sup_Price add foreign key (ArticleId) references PriceList (ArticleId);
 
/* Update trigger for AddressBook */
Create Trigger tu_AddressBook after update
of BLN_Supplier,BLN_Customer
on AddressBook
referencing new as new_upd old as old_upd for each row
declare numrows integer;
begin
/* cascade child AddressBook update when parent AddressBook changed */
if (:old_upd.BLN_Supplier != :new_upd.BLN_Supplier) then
begin update AddressBook
set BLN_Customer = :new_upd.BLN_Supplier
where AddressBook.BLN_Customer = :old_upd.BLN_Supplier ;
end;
end if;
end;
/
/* Roles permissions */
/* Users permissions */

Nastavení referenční integrity

Identifying Relationship 1

Parent Update - Restrict
Parent Delete - Restrict

Non Identifying Relationship 1

Parent Update - Restrict
Parent Delete - Restrict

Relationship M:N 1

Parent Update - Restrict
Parent Delete - Restrict

Relationship M:N 2

Parent Update - Restrict
Parent Delete - Restrict

Self Relationship 1

Parent Update - Cascade
Parent Delete - Cascade

Jak nadefinovat pravidla pro referenční integritu?

Pravým tlačítkem myši klikněte na čáru zobrazující relaci a nastavte příslušné hodnoty v záložce "Referenční integrita"
viz Pravidla referenční intergrity
 
Nepodporované typy referenční integrity?
CASE Studio 2 Vám umožňuje generovat také nepodporované typy referenční integrity za pomocí triggerů.
Viz také: Editace relací