Subject Re: [ib-support] Re: going beyond FOREIGN KEY ... REFERENCES
Author Arno Brinkman
Hi,

> >What you want to do can perfectly be done with foreign keys
>
> mmm...

Yes !

> >you add a STAR table above your SYSTEM table, like:
>
> could you please expand on this ?
>
> I am afraid I didn't understand.
>
> We start with this database:
>
> create table Stars (
> Star_ID integer not null,
> System_ID integer,
> primary key (Star_ID)
> )
>
> create table Systems (
> System_ID integer not null,
> Planet_ID integer not null,
> primary key (System_ID,Planet_ID)
> )
>
> Are you suggesting to insert a 3th table btw Stars and Systems ?
>
> Please write the code of the new database.

A star can only be inside 1 solar-system right ?
Isn't your system 1-1 with a star ?

I suggest the following metadata :

-----
CREATE TABLE Stars (
Star_ID integer not null,
Description varChar(50),
PRIMARY KEY (Star_ID)
);

CREATE TABLE SolarSystems (
SolarSystem_ID integer not null,
Description varChar(50),
Star_ID integer not null,
PRIMARY KEY (SolarSystem_ID),
CONSTRAINT FK_SolarSystems_Stars
FOREIGN KEY (Star_ID) REFERENCES Stars (Star_ID)
ON DELETE CASCADE
ON UPDATE CASCADE
);

CREATE TABLE Planets (
Planet_ID integer not null,
Description varChar(50),
System_ID integer,
PRIMARY KEY (Planet_ID) ,
CONSTRAINT FK_Planets_SolarSystems
FOREIGN KEY (System_ID) REFERENCES SolarSystems
(SolarSystem_ID)
ON DELETE CASCADE
ON UPDATE CASCADE
);
-----
INSERT INTO Stars(STAR_ID, DESCRIPTION) VALUES (1, 'Sun');

INSERT INTO SolarSystems(SOLARSYSTEM_ID, DESCRIPTION, STAR_ID) VALUES (1,
'Our Solar System', 1);

INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (1, 'Mercury',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (2, 'Venus',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (3, 'Earth',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (4, 'Mars',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (5, 'Jupiter',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (6, 'Saturn',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (7, 'Uranus',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (8, 'Neptune',
1);
INSERT INTO Planets(PLANET_ID, DESCRIPTION, SYSTEM_ID) VALUES (9, 'Pluto',
1);
----

Regards,
Arno Brinkman