473,396 Members | 1,840 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

NOT foreign key in UDB V8.2 LINUX

For good and sufficient reasons I wish to insure that a primary key of
table 1 is not a primary key of table 2. The following does not work:

ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CATEGORIES
where auction_id=auction_id))

Is there any way other than a trigger (or program checks) to write this?
Mar 8 '07 #1
5 2477
On Thu, 08 Mar 2007 18:28:49 -0500, Bob Stearns
<rs**********@charter.netwrote:
>For good and sufficient reasons I wish to insure that a primary key of
table 1 is not a primary key of table 2. The following does not work:

ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CATEGORIES
where auction_id=auction_id))

Is there any way other than a trigger (or program checks) to write this?
Here's a pseudo-answer.

Instead of doing INSERT/UPDATE on the TABLE, use a VIEW instead. The
VIEW could use WITH CHECK OPTION to force the business rule.

B.
Mar 9 '07 #2
On Thu, 08 Mar 2007 18:28:49 -0500, Bob Stearns
<rs**********@charter.netwrote:
>For good and sufficient reasons I wish to insure that a primary key of
table 1 is not a primary key of table 2. The following does not work:

ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CATEGORIES
where auction_id=auction_id))

Is there any way other than a trigger (or program checks) to write this?
Another action would be to CREATE a TABLE with an FK, always LOAD data
into it, and throw exceptions into a second TABLE. That second TABLE
would be the real one.

Yes, yes, not an answer, i was just thinking about it. :)

B.
Mar 9 '07 #3
>For good and sufficient reasons I wish to insure that a primary key of table 1 is not a primary key of table 2. <<

> The following does not work:
ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CATEGORIES
where auction_id=auction_id));
<<

What **kind of code** did you mean? You need ISO-11179 names here.
To be is to be something in particular. And shouldn't you qualify
auction_id? Then let's follow Mother Celko's formattign rules about
key words in uppercase.

ALTER TABLE IS3.auction_super_categories
ADD CONSTRAINT vague_code_not_fk
CHECK(vague_code NOT IN
(SELECT vague_code
FROM IS3.auction_categories AS A
WHERE auction_super_categories.auction_id =
A.auction_id));

It still has problems, but at least it looks nice now :)

The classic scenario calls for a root class with all the common
attributes and then specialized sub-classes under it. As an example,
let's take the class of Vehicles and find an industry standard
identifier (VIN), and add two mutually exclusive sub-classes, Sport
utility vehicles and sedans ('SUV', 'SED').

CREATE TABLE Vehicles
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) NOT NULL
CHECK(vehicle_type IN ('SUV', 'SED')),
UNIQUE (vin, vehicle_type),
..);

Notice the overlapping candidate keys. I then use a compound candidate
key (vin, vehicle_type) and a constraint in each sub-class table to
assure that the vehicle_type is locked and agrees with the Vehicles
table. Add some DRI actions and you are done:

CREATE TABLE SUV
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT 'SUV' NOT NULL
CHECK(vehicle_type = 'SUV'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Vehicles(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

CREATE TABLE Sedans
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT 'SED' NOT NULL
CHECK(vehicle_type = 'SED'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Vehicles(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

I can continue to build a hierarchy like this. For example, if I had
a Sedans table that broke down into two-door and four-door sedans, I
could a schema like this:

CREATE TABLE Sedans
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT 'SED' NOT NULL
CHECK(vehicle_type IN ('2DR', '4DR', 'SED')),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Vehicles(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

CREATE TABLE TwoDoor
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT '2DR' NOT NULL
CHECK(vehicle_type = '2DR'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Sedans(vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

CREATE TABLE FourDoor
(vin CHAR(17) NOT NULL PRIMARY KEY,
vehicle_type CHAR(3) DEFAULT '4DR' NOT NULL
CHECK(vehicle_type = '4DR'),
UNIQUE (vin, vehicle_type),
FOREIGN KEY (vin, vehicle_type)
REFERENCES Sedans (vin, vehicle_type)
ON UPDATE CASCADE
ON DELETE CASCADE,
..);

The idea is to build a chain of identifiers and types in a UNIQUE()
constraint that go up the tree when you use a REFERENCES constraint.
Obviously, you can do variants of this trick to get different class
structures.

If an entity doesn't have to be exclusively one subtype, you play with
the root of the class hierarchy:

CREATE TABLE Vehicles
(vin CHAR(17) NOT NULL,
vehicle_type CHAR(3) NOT NULL
CHECK(vehicle_type IN ('SUV', 'SED')),
PRIMARY KEY (vin, vehicle_type),
..);

Now start hiding all this stuff in VIEWs immediately and add an
INSTEAD OF trigger to those VIEWs.

Another approach is to design a Dewey Decimal hierarchical
auction_categories and put restrictions on it based on ranges.

Mar 10 '07 #4
On Mar 9, 8:28 am, Bob Stearns <rstearns1...@charter.netwrote:
For good and sufficient reasons I wish to insure that a primary key of
table 1 is not a primary key of table 2. The following does not work:

ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CATEGORIES
where auction_id=auction_id))

Is there any way other than a trigger (or program checks) to write this?
What is the reason that you don't want to use trigger?
DB2 CHECK constraint can't be refer to columns of another table.
I feel that using trigger is natural and easy for this problem.

Mar 12 '07 #5
Tonkuma wrote:
On Mar 9, 8:28 am, Bob Stearns <rstearns1...@charter.netwrote:
>For good and sufficient reasons I wish to insure that a primary key of
table 1 is not a primary key of table 2. The following does not work:

ALTER TABLE IS3.AUCTION_SUPER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CATEGORIES
where auction_id=auction_id))

Is there any way other than a trigger (or program checks) to write this?
What is the reason that you don't want to use trigger?
DB2 CHECK constraint can't be refer to columns of another table.
I feel that using trigger is natural and easy for this problem.
Moreover, a CHECK constraint can operate on a single (the current) row only
(plus constants). It is more a "row check constraint" instead of a "table
check constraint".

--
Knut Stolze
DB2 z/OS Admin Enablement
IBM Germany
Mar 12 '07 #6

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

Similar topics

5
by: Olivier Crèvecoeur | last post by:
Hello, Excuse me for my poor english. I would kike know if create index on the foreign key it's necessary or if Oracle, are optimized for using foreign key whithout index. Best regards ...
0
by: Ron | last post by:
Mandatories: Ver 7.3.4, Redhat Linux 8.0, P4, 2GB RAM I want to add a 'nullable' foreign key to a column in a table. I have tables "company" and "project" which may be related by...
0
by: Jeremiah Jacks | last post by:
I just upgraded to MySQL 4.0.14-standard for RedHat Linux and am using = the pre-compiled binaries. I have a database with INNODB tables. When I insert a row into one of the child tables, I get...
1
by: Andrew DeFaria | last post by:
I created the following .sql file to demonstrate a problem I'm having. According to the manual: If |ON DELETE CASCADE| is specified, and a row in the parent table is deleted, then InnoDB...
13
by: Xah Lee | last post by:
the Journey of Foreign Characters thru Internet Xah Lee, 20051101 There's a bunch of confusions about the display of non-ascii characters such as the bullet "•". These confusions are...
10
by: D. Dante Lorenso | last post by:
I'd like to run a clean up command on my tables to eliminate rows that I'm no longer using in the database. I want to do something like this: DELETE FROM tablename WHERE...
6
by: Tony | last post by:
Hi, I'm still new to this so if I'm sounding dumb or my premise is flawed please forgive me. I have a DB design which contains a table which has categories, each category has a parent category,...
13
by: Bob Stearns | last post by:
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...
5
pradeepjain
by: pradeepjain | last post by:
i have 1st table created like this create table mobiles(property1 varchar(100) NOT NULL,property2 varchar(100) NOT NULL,property3 varchar(100) NOT NULL,property4 varchar(100) NOT NULL,property5...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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
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...

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.