If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > General > Database Concepts & Design > isa-realtionship

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 03-18-04, 05:03
julla27 julla27 is offline
Registered User
 
Join Date: Feb 2004
Location: Germany
Posts: 136
isa-realtionship

Hello!

i want to display the following isa-relationship:

see ISA.jpg

Now i think that this code is wrong:

CREATE TABLE T(ID_T INTEGER, NAME VARCHAR2(10), CATEGORY VARCHAR(10));
ALTER TABLE T ADD (CONSTRAINT PK_T PRIMARY KEY (ID_T));

CREATE TABLE A(ID_T INTEGER, ID_A INTEGER, NAME VARCHAR2(10));
ALTER TABLE A ADD (CONSTRAINT PK_A PRIMARY KEY (ID_T, ID_A));
ALTER TABLE A ADD (CONSTRAINT FK_A FOREIGN KEY (ID_T) REFERENCES T (ID_T));

CREATE TABLE B(ID_T INTEGER, ID_B INTEGER, NAME VARCHAR2(10));
ALTER TABLE B ADD (CONSTRAINT PK_B PRIMARY KEY (ID_T, ID_B));
ALTER TABLE B ADD (CONSTRAINT FK_B FOREIGN KEY (ID_T) REFERENCES T (ID_T));


CREATE TABLE C(ID_T INTEGER, ID_C INTEGER, NAME VARCHAR2(10));
ALTER TABLE C ADD (CONSTRAINT PK_C PRIMARY KEY (ID_T, ID_C));
ALTER TABLE C ADD (CONSTRAINT FK_C FOREIGN KEY (ID_T) REFERENCES T (ID_T));



It would be great if anybody could help me!
Attached Images
File Type: jpg isa.jpg (12.4 KB, 96 views)
__________________
Regards,
Julia
Reply With Quote
  #2 (permalink)  
Old 03-18-04, 05:11
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: isa-realtionship

It should be:

CREATE TABLE T(ID_T INTEGER, NAME VARCHAR2(10), CATEGORY VARCHAR(10));
ALTER TABLE T ADD (CONSTRAINT PK_T PRIMARY KEY (ID_T));

CREATE TABLE A(ID_T INTEGER, {a_specific_columns});
ALTER TABLE A ADD (CONSTRAINT PK_A PRIMARY KEY (ID_T));
ALTER TABLE A ADD (CONSTRAINT FK_A FOREIGN KEY (ID_T) REFERENCES T (ID_T));

CREATE TABLE B(ID_T INTEGER, {b_specific_columns});
ALTER TABLE B ADD (CONSTRAINT PK_B PRIMARY KEY (ID_T));
ALTER TABLE B ADD (CONSTRAINT FK_B FOREIGN KEY (ID_T) REFERENCES T (ID_T));

CREATE TABLE C(ID_T INTEGER, {c_specific_columns});
ALTER TABLE C ADD (CONSTRAINT PK_C PRIMARY KEY (ID_T));
ALTER TABLE C ADD (CONSTRAINT FK_C FOREIGN KEY (ID_T) REFERENCES T (ID_T));

i.e. the ID for the supertype is also the ID for the subtype.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews

Last edited by andrewst; 03-18-04 at 05:13.
Reply With Quote
  #3 (permalink)  
Old 03-18-04, 06:27
julla27 julla27 is offline
Registered User
 
Join Date: Feb 2004
Location: Germany
Posts: 136
Thank You!
But what can i do if i need the following construction later:
Attached Images
File Type: jpg isa1.jpg (10.1 KB, 82 views)
__________________
Regards,
Julia
Reply With Quote
  #4 (permalink)  
Old 03-18-04, 06:51
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
That would require an intersection table:

create table ab (a_id_t references a, b_id_t references b,
constraint pk_ab primary key ( a_id_t, b_id_t));
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
  #5 (permalink)  
Old 03-18-04, 09:15
julla27 julla27 is offline
Registered User
 
Join Date: Feb 2004
Location: Germany
Posts: 136
Thank You!
__________________
Regards,
Julia
Reply With Quote
  #6 (permalink)  
Old 03-18-04, 19:14
byrmol byrmol is offline
Registered User
 
Join Date: Mar 2003
Location: Australia
Posts: 59
Be sure to enforce a constraint that prohibits the supertype to be more than one subtype (If that is desired)...

ie: With the current schema this is possible.....

insert into T values (1)
insert into A Values (1)
insert into B Values (1)
Reply With Quote
  #7 (permalink)  
Old 03-19-04, 09:53
julla27 julla27 is offline
Registered User
 
Join Date: Feb 2004
Location: Germany
Posts: 136
New question

Hello!
Now i have an advanced question.

###################################

table T
==============================
ID integer, (e.g. 1, 2, 3)
CATEGORY (e.g., A, B, C)
==============================

table A
===========
ID integer,
...

relat_table AB
==============
ID_A integer
ID_B integer


table B
============
ID integer,
...

table C
===========
ID integer,
...

###################################


Now i want to create the following trigger:

if (insert into T ( CATEGORY) values ('A') )
then (insert the same ID into table A)

if ................ 'B'..............
then ................ 'B'.........

and so on.

These operations must be atomic.

How can i realize that?

################################

CREATE OR REPLACE TRIGGER TRG_T_BEF_INS
BEFORE INSERT ON T
REFERENCING NEW AS NEW OLD AS OLD
FOR EACH ROW
DECLARE
tmpVar1 NUMBER;

BEGIN
tmpVar1 := 0;

SELECT SEQ_T.NEXTVAL INTO tmpVar1 FROM dual;
:NEW.ID := tmpVar1;
END;


It would be great if anybody could help me!
__________________
Regards,
Julia
Reply With Quote
  #8 (permalink)  
Old 03-19-04, 10:14
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 5,171
Re: New question

You would want an AFTER INSERT trigger something like this:
PHP Code:
create or replace trigger trg_t_aft_ins
after insert on t
for each row
begin
  
if :new.type 'A' then
    insert into a 
(idvalues (:new.id);
  
elsif :new.type 'B' then
    insert into b 
(idvalues (:new.id);
  ...
  
end if;
end
However, this only works if all the non-key columns in A, B, ... are defined to allow NULLs. I would more likely go for an approach like this:

PHP Code:
create or replace view a_view as
select a.ida..., t...
from at
where a
.id t.id;

create or replace trigger a_view_ins_trg 
instead of insert on a_view
begin
  insert into t
(id, ...) values (seq_t.nextval, ...);
  
insert into a(id, ...) values (seq_t.currval, ...);
end
Now the user inserts into view_a, and a record gets created in T and A with all columns populated.
__________________
Tony Andrews
http://tinyurl.com/tonyandrews
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On