You are more likely to get answers to your questions if you subscribe
to derby-user@db.apache.org. You can find more info about Derby and
Derby mailing lists here
http://db.apache.org/derby/derby_comm.html
>>>>> "B" == Bloggs <MrFredBloggs@hotmail.com> writes:
B> Hi All,
B> I have created the following tables:
B> ------------------------------------------------------------------------------------------------------------------
B> DROP TABLE FREQUENCYTABLE;
B> DROP TABLE FOODNAMETABLE;
B> CREATE TABLE FOODNAMETABLE (ID SMALLINT GENERATED ALWAYS AS IDENTITY,
B> FOOD VARCHAR(40) NOT NULL PRIMARY KEY,
B> FOLDER VARCHAR(40) NOT NULL,
B> FOREIGN KEY (FOLDER) REFERENCES
B> FOLDERNAMETABLE(FOLDER));
B> CREATE TABLE FREQUENCYTABLE (FREQUENCY VARCHAR(40) NOT NULL,
B> FOOD VARCHAR(40) NOT NULL,
B> AMOUNT SMALLINT NOT NULL DEFAULT 0,
B> INCLUDE SMALLINT NOT NULL DEFAULT 0 CHECK
B> (INCLUDE BETWEEN 0 AND 1),
B> FOREIGN KEY (FOOD) REFERENCES
B> FOODNAMETABLE(FOOD) ON DELETE CASCADE);
B> ------------------------------------------------------------------------------------------------------------------
B> If I attempt to to update the NAME column in the FOODNAMETABLE I can't
B> because of Derby's referential integrity rules, also I can't use
B> cascading updates because Derby does not appear to have them although
B> it has cascading deletes.
B> If I write a trigger to perform a cascading update can I use it without
B> first redefining the tables or do I need to remove the constraints I
B> have already defined on these columns before my trigger will work.
B> Thanks in advance,
B> Regards,
B> MrFred.

Signature
dt
However, experience shows that for many people and many applications a
dose of paranoia is reasonable - Bjarne Stroustrup
MrFred - 25 Jan 2006 00:13 GMT
> You are more likely to get answers to your questions if you subscribe
> to derby-user@db.apache.org. You can find more info about Derby and
[quoted text clipped - 46 lines]
> However, experience shows that for many people and many applications a
> dose of paranoia is reasonable - Bjarne Stroustrup
I have now modified my tables, my problem now is getting my own
*cascading update* trigger to work. I don't think this problem is
specific to Derby.
I recreated my tables as:
-------------------------------------------------------
DROP TABLE FREQUENCYTABLE;
DROP TABLE FOODNAMETABLE;
CREATE TABLE FOODNAMETABLE (ID SMALLINT GENERATED ALWAYS AS IDENTITY,
FOOD VARCHAR(40) NOT NULL PRIMARY KEY,
FOLDER VARCHAR(40) NOT NULL);
CREATE TABLE FREQUENCYTABLE (FREQUENCY VARCHAR(40) NOT NULL,
FOOD VARCHAR(40) NOT NULL,
AMOUNT SMALLINT NOT NULL DEFAULT 0,
INCLUDE SMALLINT NOT NULL DEFAULT 0 CHECK
(INCLUDE BETWEEN 0 AND 1));
CREATE TRIGGER UPDATEFOOD
AFTER UPDATE OF FOOD ON FOODNAMETABLE
REFERENCING OLD AS PREVIOUSROW
FOR EACH ROW MODE DB2SQL
UPDATE FREQUENCYTABLE SET FOOD = FOODNAMETABLE.FOOD
WHERE FREQUENCYTABLE.FOOD = PREVIOUSROW.FOOD;
-------------------------------------------------------
The above trigger runs automatically whenever my application updates
the FOOD column
in FOODNAMETABLE, for example when the value in FOOD is changed from
*Oranges*
to *Lemons*. When this happens the trigger is supposed to change any
row in FREQUENCYTABLE
that also has *Oranges* in its FOOD column to *Lemons*, but this
doesn't happen.
By testing I have proved that the trigger runs and that it makes a
change but my
problem is it changes it to the wrong value. It seems that whereas
PREVIOUSROW.FOOD
equals *Oranges*, as expected (i.e. because that was its value before
the UPDATE on that
table), FOODNAMETABLE.FOOD doesn't equal *Lemons* as I, seemingly
incorrectly, expect.
What's wrong with the trigger?
Thanks,
MrFred.
MrFred - 25 Jan 2006 03:03 GMT
> > You are more likely to get answers to your questions if you subscribe
> > to derby-user@db.apache.org. You can find more info about Derby and
[quoted text clipped - 97 lines]
>
> MrFred.
Solved my own problem. The trigger should have read:
CREATE TRIGGER UPDATEFOOD
AFTER UPDATE OF FOOD ON FOODNAMETABLE
REFERENCING OLD AS PREVIOUSROW NEW AS NEWROW
FOR EACH ROW MODE DB2SQL
UPDATE FREQUENCYTABLE SET FOOD = NEWROW.FOOD
WHERE FREQUENCYTABLE.FOOD = PREVIOUSROW.FOOD;