I have setup the
following database. It contains two tables with two columns each. The first
table (VrstaPrevoza) is a parent table to the second one (VozniRedi) via foreign
key on field VrstaId. I want the changes (updates) to the
VrstaPrevoza.VrstaId to be propagated to the child table VozniRedi.
The UPDATE statement
results in an error.
------------------------
My
code:
DRIVER
'org.apache.derby.jdbc.EmbeddedDriver';
CONNECT 'jdbc:derby:database/test;create=true';
CONNECT 'jdbc:derby:database/test;create=true';
-- VrstaPrevoza
DROP TABLE VrstaPrevoza;
CREATE TABLE VrstaPrevoza (VrstaId CHAR(2) NOT NULL,
Opis VARCHAR(20) NOT NULL,
PRIMARY KEY (VrstaId));
INSERT INTO VrstaPrevoza VALUES ('MK','bla bla');
INSERT INTO VrstaPrevoza VALUES ('PR','bla bla');
INSERT INTO VrstaPrevoza VALUES ('MN','bla bla');
DROP TABLE VrstaPrevoza;
CREATE TABLE VrstaPrevoza (VrstaId CHAR(2) NOT NULL,
Opis VARCHAR(20) NOT NULL,
PRIMARY KEY (VrstaId));
INSERT INTO VrstaPrevoza VALUES ('MK','bla bla');
INSERT INTO VrstaPrevoza VALUES ('PR','bla bla');
INSERT INTO VrstaPrevoza VALUES ('MN','bla bla');
-- VozniRedi
DROP TABLE VozniRedi;
CREATE TABLE VozniRedi (VozniRedId VARCHAR(50) NOT NULL,
VrstaId CHAR(2) NOT NULL,
PRIMARY KEY (VozniRedId),
FOREIGN KEY (VrstaId) REFERENCES VrstaPrevoza(VrstaId));
INSERT INTO VozniRedi VALUES ('MK1110010R','MK');
INSERT INTO VozniRedi VALUES ('MK1110020R','MK');
DROP TABLE VozniRedi;
CREATE TABLE VozniRedi (VozniRedId VARCHAR(50) NOT NULL,
VrstaId CHAR(2) NOT NULL,
PRIMARY KEY (VozniRedId),
FOREIGN KEY (VrstaId) REFERENCES VrstaPrevoza(VrstaId));
INSERT INTO VozniRedi VALUES ('MK1110010R','MK');
INSERT INTO VozniRedi VALUES ('MK1110020R','MK');
ij>
UPDATE VrstaPrevoza SET VrstaId='AB' WHERE
VrstaId='MK';
ERROR 23503: UPDATE on table 'VRSTAPREVOZA' caused a violation of foreign key co
nstraint 'SQL050403042438881' for key (MK). The statement has been rolled back.
ERROR 23503: UPDATE on table 'VRSTAPREVOZA' caused a violation of foreign key co
nstraint 'SQL050403042438881' for key (MK). The statement has been rolled back.
-------------------------------
I would expect to be
able to define the foreign key like
FOREIGN KEY
(VrstaId) REFERENCES VrstaPrevoza(VrstaId) ON UPDATE
CASCADE;
but this doesn't
work in Derby.
How to implement
"UPDATE CASCADE"??
Many
thanks!
Andrej
