Frage

i tried to implement a is-a relation (WARENGRUPPE) in sql, that works now but i have a problem with inserting a product to this (WARENGRUPPE), because it throws an error that the key is not included in WARENGRUPPE although i inserted it to it's child RAUCHWARE: HERE IS THE CREATE PROCEDURE:

/* Tabelle fuer Warengruppe fuer die ISA Beziehung. GruppenID und Kurzbeschreibung */
create table WARENGRUPPE(
GruppenID integer,
Beschreibung varchar(200),
primary key (GruppenID) 
);

/* Tabelle fuer Rauchwarengruppe */
create table RAUCHWARE(
Typ varchar(200),
primary key (GruppenID) 
) INHERITS (WARENGRUPPE);

/* Tabelle fuer Schnupfwarengruppe */
create table SCHNUPFWARE(
Koernung integer,
primary key (GruppenID) 
) INHERITS (WARENGRUPPE);

/* Tabelle fuer Kauwarengruppe */
create table KAUWARE(
Konsistenz varchar(100),
primary key (GruppenID) 
) INHERITS (WARENGRUPPE);

/* Tabelle fuer PRODUKT */  
create table PRODUKT(
EAN varchar(100),
Preis integer,
VerfahrensNR integer,
MarkenName varchar(50),
WarenGruppenID integer, 
foreign key (WarenGruppenID) references WARENGRUPPE(GruppenID),
foreign key (VerfahrensNR) references HERSTELLUNGSVERFAHREN(NR),
foreign key (MarkenName) references MARKE(Name),
primary key (EAN)
);

And here you can see the LOAD procedure which throws an error:

INSERT INTO RAUCHWARE (GruppenID,Beschreibung,Typ)
VALUES (1,'Eine Ware die geraucht wird ','Zigarre');


 /* Insert von Produkten */
INSERT INTO PRODUKT (EAN,Preis,VerfahrensNR,MarkenName,WarenGruppenID)
VALUES ('Marlboro Light',5,1,'Marlboro',1);

It's saying that the ID=1 is not included in WARENGRUPPE, i interpreted the table WARENGRUPPE like an interface, am I wrong?

Maybe someone could help me

Best Regards Stefan Sprenger

EDIT:

Here is a link to what it should do, modelled by an ER DIAGRAMM https://www.dropbox.com/s/d8l8ry5hulx3u61/ERExample.jpg

War es hilfreich?

Lösung

I think you've got a bit confused how inheritance is supposed to work ...

Since you want the base table/class to be WARENGRUPPE and e.g. RAUCHWARE to be a sub-class of WARENGRUPPE you should only specify what is special/different in RAUCHWARE from WARENGRUPPE. The sub-class does not need to have is own primary key.

So it schould be somethind like this:

create table WARENGRUPPE(
GruppenID integer,
Beschreibung varchar(200),
primary key (GruppenID) 
);

create table RAUCHWARE(
Typ varchar(200)
) INHERITS (WARENGRUPPE);

INSERT INTO WARENGRUPPE VALUES (1,'Allgemeine Güter und Waren');
INSERT INTO RAUCHWARE VALUES (2,'Rauchwaren', 'Zigarren'); 

The table RAUCHWARE will have three columns, the two definied in WARENGRUPPE and the one definied in it's own create table statement.

An is-a relationship might be betwwen "person" and "cowboy". Here each cowboy is a person, but not every persom is a cowboy.

create table Person (
  ID integer,
  Name varchar(200),
  primary key (ID) 
);

create table Cowboy (
  HorsesName varchar(200),
  ID integer NOT NULL,
  primary key (ID),
  foreign key (ID) references Person(id)
);

INSERT into Person values (1, 'Jimmy');
INSERT into Person values (2, 'Timmy');

INSERT INTO Cowboy values ('Trigger',2);

Timmy is a cowboy with a horse called 'Trigger', where as Jimmy is a plain old person without a horse to his name.

Given the ER model in your picture you might want to do something like this:

create table WARENGRUPPE
(
  GruppenID integer,
  Beschreibung varchar(200),
  primary key (GruppenID) 
);

create table RAUCWARE
(
  GruppenID integer NOT NULL,
  Beschreibung varchar(200),
  FOREIGN key (GruppenID)  REFERENCES WARENGRUPPE (GruppenID)
);

create table SCHNUPFWARE
(
  GruppenID integer NOT NULL,
  Koernung varchar(200),
  FOREIGN key (GruppenID)  REFERENCES WARENGRUPPE (GruppenID)
);

create table KAUWARE
(
  GruppenID integer NOT NULL,
  Konsistenz varchar(200),
  FOREIGN key (GruppenID)  REFERENCES WARENGRUPPE (GruppenID)
);

create table PRODUKT
(
EAN varchar(100),
Preis integer,
VerfahrensNR integer,
MarkenName varchar(50),
WarenGruppenID integer, 
foreign key (WarenGruppenID) references WARENGRUPPE(GruppenID),
foreign key (VerfahrensNR) references HERSTELLUNGSVERFAHREN(NR),
foreign key (MarkenName) references MARKE(Name),
primary key (EAN)
);

You can try it here: http://sqlfiddle.com/#!10/2d229/1

By the way, i get the german names for things (native speaker), but you might get more ansewers by sticking to english names for things....

Andere Tipps

I agree with Mithrandir's comment. Take your time when asking a question to check what you are really doing/which configuration you use.

To the issue of the question: Tables are tables, not classes. They do not behave as classes. Table WARENGRUPPE is completely different from RAUCHWARE.

The inherits clause is just a way of telling "I want to have all the fields defined in WARENGRUPPE also in RAUCHWARE. It does not imply an "is an" relationship. You have TWO different tables with some common attributes.

Check the postgresql documentation

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top