473,486 Members | 2,394 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Foreign constraints DB2 UDB v8.1.7 Linux

Why is the following constraint invalid? I want to make sure that every
row in IS3.ANIMALS_PRIV_INDEXES matches one of those in
IS3.table_var_defn with part of the primary key fixed. Since this is
illegal with the following message, how do I achieve this result? The
last thing below is a working constraint from another table.

ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT
FOREIGN KEY(3, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION

DB2 SQL error: SQLCODE: -104, SQLSTATE: 42601, SQLERRMC: FOREIGN KEY;ADD
CONSTRAINT;NUMERIC
Message: An unexpected token "FOREIGN KEY" was found following "ADD
CONSTRAINT". Expected tokens may include: "NUMERIC".

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT
FOREIGN KEY(table_id, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Nov 12 '05 #1
13 3667
"Bob Stearns" <rs**********@charter.net> wrote in message
news:Nq*****************@fe03.lga...
Why is the following constraint invalid? I want to make sure that every
row in IS3.ANIMALS_PRIV_INDEXES matches one of those in IS3.table_var_defn
with part of the primary key fixed. Since this is illegal with the
following message, how do I achieve this result? The last thing below is a
working constraint from another table.

ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT
FOREIGN KEY(3, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION

ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT
FOREIGN KEY(table_id, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
Nov 12 '05 #2
Add constraint name:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT xxxxxx
FOREIGN KEY(table_id, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION

Nov 12 '05 #3
I'm sorry. I referenced different ALTER statement in your post.
I think your ALTER statement have two problem.
1) constraint-name is required after CONSTRAINT keyword.
Though, you wrote
The last thing below is a working constraint from another table.

But, in my DB2 UDB V8 FP9a on Windows, I got following message, if no
constraint-name was specified.
SQL0104N An unexpected token "FOREIGN KEY" was found following "loyee
ADD CONSTRAINT". Expected tokens may include: "<col_type>".
SQLSTATE=42601

2) FOREIGN KEY can be specified only column name. You specified
constant number 3.

ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT <constraint-name>
FOREIGN KEY(<column-name>, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION

Nov 12 '05 #4
Mark A wrote:
"Bob Stearns" <rs**********@charter.net> wrote in message
news:Nq*****************@fe03.lga...
Why is the following constraint invalid? I want to make sure that every
row in IS3.ANIMALS_PRIV_INDEXES matches one of those in IS3.table_var_defn
with part of the primary key fixed. Since this is illegal with the
following message, how do I achieve this result? The last thing below is a
working constraint from another table.

ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT
FOREIGN KEY(3, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION


ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT
FOREIGN KEY(table_id, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION

table_id is deliberately not a column of IS3.ANIMALS_PRIV_INDEXES, since
it is an unneeded, constant column.
Nov 12 '05 #5
Tonkuma wrote:
I'm sorry. I referenced different ALTER statement in your post.
I think your ALTER statement have two problem.
1) constraint-name is required after CONSTRAINT keyword.
Though, you wrote
The last thing below is a working constraint from another table.


But, in my DB2 UDB V8 FP9a on Windows, I got following message, if no
constraint-name was specified.
SQL0104N An unexpected token "FOREIGN KEY" was found following "loyee
ADD CONSTRAINT". Expected tokens may include: "<col_type>".
SQLSTATE=42601

2) FOREIGN KEY can be specified only column name. You specified
constant number 3.

ALTER TABLE IS3.ANIMALS_PRIV_INDEXES
ADD CONSTRAINT <constraint-name>
FOREIGN KEY(<column-name>, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION

I did miss the constraint name, which made the error message bad. But my
original question stands: How can I have an FK relationship where part
of the FK is constant without having a redundant, extraneous, constant
column in my table?
Nov 12 '05 #6
"Bob Stearns" <rs**********@charter.net> wrote in message
news:AV***************@fe06.lga...
table_id is deliberately not a column of IS3.ANIMALS_PRIV_INDEXES, since
it is an unneeded, constant column.


You cannot put a constant in foreign key definition. A foreign key must be
composed of column names (not constants) that are Primary Keys or Unique
Keys on a parent table.

I really don't know what you are trying to due, but maybe you need to create
a regular constraint where one of the columns is equal to the constant 3,
and not a foreign key.
Nov 12 '05 #7
If you don't want to add a column.
Only idea I could thoght was to create triggers for insert and update
on IS3.ANIMALS_PRIV_INDEXES.

Nov 12 '05 #8
For example:
CREATE TRIGGER ANIMALS_PRIV_IX_I
NO CASCADE BEFORE INSERT ON IS3.ANIMALS_PRIV_INDEXES
REFERENCING NEW AS n
FOR EACH ROW
WHEN (NOT EXISTS
(SELECT *
FROM IS3.table_var_defn vd
WHERE vd.table_id = 3
AND vd.variable_id = n.variable_id
)
) SIGNAL SQLSTATE 'APIST';

CREATE TRIGGER ANIMALS_PRIV_IX_U
NO CASCADE BEFORE UPDATE OF variable_id ON IS3.ANIMALS_PRIV_INDEXES
REFERENCING NEW AS n
FOR EACH ROW
WHEN (NOT EXISTS
(SELECT *
FROM IS3.table_var_defn vd
WHERE vd.table_id = 3
AND vd.variable_id = n.variable_id
)
) SIGNAL SQLSTATE 'APUPD';

Nov 12 '05 #9
Tonkuma wrote:
For example:
CREATE TRIGGER ANIMALS_PRIV_IX_I
NO CASCADE BEFORE INSERT ON IS3.ANIMALS_PRIV_INDEXES
REFERENCING NEW AS n
FOR EACH ROW
WHEN (NOT EXISTS
(SELECT *
FROM IS3.table_var_defn vd
WHERE vd.table_id = 3
AND vd.variable_id = n.variable_id
)
) SIGNAL SQLSTATE 'APIST';

CREATE TRIGGER ANIMALS_PRIV_IX_U
NO CASCADE BEFORE UPDATE OF variable_id ON IS3.ANIMALS_PRIV_INDEXES
REFERENCING NEW AS n
FOR EACH ROW
WHEN (NOT EXISTS
(SELECT *
FROM IS3.table_var_defn vd
WHERE vd.table_id = 3
AND vd.variable_id = n.variable_id
)
) SIGNAL SQLSTATE 'APUPD';

Thank you for the example.

It seems a long way to go for what appears to me to be a simple
declarative constraint.
Nov 12 '05 #10

Bob Stearns wrote:
....

It seems a long way to go for what appears to me to be a simple
declarative constraint.


Bob, foreign keys are used to enforce relationships between table
columns as prescribed by the logical model of your data. If you want to
restrict a value of a particular table column to a single constant, I
would suggest using a check constraint, which is best suitable for
enforcing domain constraints in your model.

So you could consider changing your constraint definition to the
following two:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr1
FOREIGN KEY(variable_id)
REFERENCES IS3.table_var_defn(variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr2 CHECK(table_id = 3);

Hope this helps,
Miro

Nov 12 '05 #11
mirof007 wrote:
Bob Stearns wrote:
...
It seems a long way to go for what appears to me to be a simple
declarative constraint.

Bob, foreign keys are used to enforce relationships between table
columns as prescribed by the logical model of your data. If you want to
restrict a value of a particular table column to a single constant, I
would suggest using a check constraint, which is best suitable for
enforcing domain constraints in your model.

So you could consider changing your constraint definition to the
following two:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr1
FOREIGN KEY(variable_id)
REFERENCES IS3.table_var_defn(variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr2 CHECK(table_id = 3);

Hope this helps,
Miro

But that has different semantics than the constraint I want. These two
constraints only guarantee that variable_id in animals_priv_labels
occurs somewhere in table_var_defn, not in that subset with table_id=3.
Nov 12 '05 #12
Bob Stearns wrote:
mirof007 wrote:
Bob Stearns wrote:
...
It seems a long way to go for what appears to me to be a simple
declarative constraint.

Bob, foreign keys are used to enforce relationships between table
columns as prescribed by the logical model of your data. If you want to
restrict a value of a particular table column to a single constant, I
would suggest using a check constraint, which is best suitable for
enforcing domain constraints in your model.

So you could consider changing your constraint definition to the
following two:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr1
FOREIGN KEY(variable_id)
REFERENCES IS3.table_var_defn(variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr2 CHECK(table_id = 3);

But that has different semantics than the constraint I want. These two
constraints only guarantee that variable_id in animals_priv_labels
occurs somewhere in table_var_defn, not in that subset with table_id=3.


Then just switch the definitions around:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr2 CHECK(table_id = 3);

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr1
FOREIGN KEY(table_id, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

You have to stick with the additional column, though.

--
Knut Stolze
DB2 Information Integration Development
IBM Germany
Nov 12 '05 #13
Knut Stolze wrote:
Bob Stearns wrote:

mirof007 wrote:
Bob Stearns wrote:
...
It seems a long way to go for what appears to me to be a simple
declarative constraint.
Bob, foreign keys are used to enforce relationships between table
columns as prescribed by the logical model of your data. If you want to
restrict a value of a particular table column to a single constant, I
would suggest using a check constraint, which is best suitable for
enforcing domain constraints in your model.

So you could consider changing your constraint definition to the
following two:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr1
FOREIGN KEY(variable_id)
REFERENCES IS3.table_var_defn(variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr2 CHECK(table_id = 3);


But that has different semantics than the constraint I want. These two
constraints only guarantee that variable_id in animals_priv_labels
occurs somewhere in table_var_defn, not in that subset with table_id=3.

Then just switch the definitions around:

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr2 CHECK(table_id = 3);

ALTER TABLE IS3.ANIMALS_PRIV_LABELS
ADD CONSTRAINT cnstr1
FOREIGN KEY(table_id, variable_id)
REFERENCES IS3.table_var_defn(table_id, variable_id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;

You have to stick with the additional column, though.

That is what I was objecting to all along. It should be possible to put
constants in the FOREIGN KEY part of the constraint. The presence of a
constant value column in the table offends my sense of redundancy. In my
case, it is a small thing, but in other cases, I could see it adding
significantly to the overall table size.
Nov 12 '05 #14

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
5249
by: Jason Madison | last post by:
We sometimes get very large databases that we want to cut down to use for testing. The information is all related to a central accounts table. The way I thought of doing this is to grab all...
0
1418
by: Scott Ribe | last post by:
I've got a problem which I think may be a bug in Postgres, but I wonder if I'm missing something. Two tables, A & B have foreign key relations to each other. A 3rd table C, inherits from A. A...
8
3508
by: wespvp | last post by:
I am using PostgreSQL 7.4.1 on RedHat 7.2. The query I am executing is something like (I replaced all the return values with 'count'): db=> explain select count(*) from messages m join (select...
3
10056
by: David Parker | last post by:
I would like to be able to truncate all of the tables in a schema without worrying about FK constraints. I tried issuing a "SET CONSTRAINTS ALL DEFERRED" before truncating, but I still get constraint...
11
10116
by: FreeToGolfAndSki | last post by:
Hi, I have an Orders Table that has Employee1 and Employee2 (one is the sales rep the other is the telemarketing rep) Both of these fields need to cascade update against the Employees table. I...
7
3578
by: BWill | last post by:
Hi, I'm writing a file browser, but I'm not sure how I could go about detecting the drives available on windows and linux systems (preferably using the standard modules if possible). I guess I...
0
2683
by: mrhodes02 | last post by:
I have two simple tables Employees (primary key table) Empid, empname,title MgrXrf (foreign key table) MgrId,Empid MgrID & Empid in MgrXrf have a foreign contstrant to EmpID in Employees....
0
2137
by: okonita | last post by:
DB2v8.2 LUW. Unable to drop Foreign Key constraints...although DROP constraints returns undefined name. Am I missing somrthing here? Hi all, I have a perplexing DB2 Drop command situation....
4
21950
by: Bobby Edward | last post by:
I have an xsd dataset. I created a simple query called GetDataByUserId. I can preview the data fine! I created a very simple BLL function that calls it and returns a datatable. When I run...
0
7105
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7180
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7341
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5439
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4564
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3076
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1381
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
600
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.