Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / January 2006

Tip: Looking for answers? Try searching our database.

Cascading Updates In Derby Query?

Thread view: 
Bloggs - 24 Jan 2006 01:31 GMT
Hi All,

I have created the following tables:

------------------------------------------------------------------------------------------------------------------
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,
                           FOREIGN KEY (FOLDER) REFERENCES
FOLDERNAMETABLE(FOLDER));

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),
                            FOREIGN KEY (FOOD) REFERENCES
FOODNAMETABLE(FOOD) ON DELETE CASCADE);
------------------------------------------------------------------------------------------------------------------

If I attempt to to update the NAME column in the FOODNAMETABLE I can't
because of Derby's referential integrity rules, also I can't use
cascading updates because Derby does not appear to have them although
it has cascading deletes.

If I write a trigger to perform a cascading update can I use it without
first redefining the tables or do I need to remove the constraints I
have already defined on these columns before my trigger will work.

Thanks in advance,

Regards,

MrFred.
Dyreatnews@sun.com - 24 Jan 2006 14:04 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
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;


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.