473,732 Members | 2,227 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_SUP ER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CAT EGORIES
where auction_id=auct ion_id))

Is there any way other than a trigger (or program checks) to write this?
Mar 8 '07 #1
5 2498
On Thu, 08 Mar 2007 18:28:49 -0500, Bob Stearns
<rs**********@c harter.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_SUP ER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CAT EGORIES
where auction_id=auct ion_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**********@c harter.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_SUP ER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CAT EGORIES
where auction_id=auct ion_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_SUP ER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CAT EGORIES
where auction_id=auct ion_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_sup er_categories
ADD CONSTRAINT vague_code_not_ fk
CHECK(vague_cod e NOT IN
(SELECT vague_code
FROM IS3.auction_cat egories AS A
WHERE auction_super_c ategories.aucti on_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_t ype 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_t ype = '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_t ype = '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_t ype 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_t ype = '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_t ype = '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_t ype 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_categor ies and put restrictions on it based on ranges.

Mar 10 '07 #4
On Mar 9, 8:28 am, Bob Stearns <rstearns1...@c harter.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_SUP ER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CAT EGORIES
where auction_id=auct ion_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...@c harter.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_SUP ER_CATEGORIES
ADD CONSTRAINT code_not_fk
check(code not in
(select code from IS3.AUCTION_CAT EGORIES
where auction_id=auct ion_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
49731
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 Olivier
0
4528
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 company.companyID <-> project.companyID. project.companyID is allowed to be null. However, when someone tries to delete a company which is still referenced in "project" I want a constraint restricting deletion. I tried:
0
2632
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 the following = MySQL error: INSERT INTO product_access_level (product_id,access_level_id) VALUES
1
8900
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 automatically deletes also all those rows in the child table whose foreign key values are equal to the referenced key value in the parent row. However:
13
2501
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 justifiable, because the underlying stuff is technology, computing technologies, are in a laymen sense, extremely complex. In order to be able to type the bullet char, post it to a newsgroup,
10
17850
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 IS_REFERENCED_BY_FOREIGN_KEY IS FALSE; Does anyone know how something like this could be done in PostgreSQL? I know I can search all the tables that
6
2661
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, and is recursed until the top category is reached, in order to create breadcrumbs. Is there any problem with using foreign keys to reference the same table? So a when category is added the CatParent MUST be present as a CatID CatID - Serial
13
3700
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 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...
5
4031
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 varchar(100) NOT NULL,property6 varchar(100) NOT NULL,property7 varchar(100) NOT NULL,property8 varchar(100) NOT NULL,property9 varchar(100) NOT NULL,property10 varchar(100) NOT NULL,property11 varchar(100) NOT NULL,property12 varchar(100) NOT...
0
8774
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9447
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9181
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6735
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6031
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4550
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3261
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 we have to send another system
2
2721
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.