IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Syntax for Composite Foreign Key


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
View previous topic :: View next topic  
Author Message
syed-1919

New User


Joined: 26 Oct 2006
Posts: 12
Location: Chennai

PostPosted: Thu Oct 11, 2007 12:17 am
Reply with quote

Hi,

Could anyone please provide the ALTER statement syntax for a composite Foreign Key????

For ex. I have the following tables:

Table1 (Col1, Col2, Col3, Col4)
Table2 (Col1, Col2, Col3, Col4)
Table3 (Col1, Col2, Col3, Col4)

Now Col1 and Col2 of Table2 refer to the Col1 and Col2 of Table1. Insert into Table2 should pass only when the combination of Col1 and Col2 of table2 is equivalent to Col1 and Col2 of table1.

Similarly, Col1 and Col2 of Table1 should refer to the Col1 and Col2 of Table1 entirely.

Hope the problem is clear.

Cheers,
Syed
Back to top
View user's profile Send private message
syed-1919

New User


Joined: 26 Oct 2006
Posts: 12
Location: Chennai

PostPosted: Thu Oct 11, 2007 2:06 am
Reply with quote

Sorry there is a typo in my post:

Incorrect:
-----------

Similarly, Col1 and Col2 of Table1 should refer to the Col1 and Col2 of Table1 entirely.


Correct:
----------

Similarly, Col1 and Col2 of Table3 should refer to the Col1 and Col2 of Table1 entirely.
Back to top
View user's profile Send private message
nuthan

Active User


Joined: 26 Sep 2005
Posts: 146
Location: Bangalore

PostPosted: Thu Oct 11, 2007 3:27 pm
Reply with quote

ALTER TABLE TABLE2
ADD ( FOREIGN KEY (COL1, COL2, COL3)
REFERENCES TABLE1 (COL1, COL2 COL3));
Back to top
View user's profile Send private message
syed-1919

New User


Joined: 26 Oct 2006
Posts: 12
Location: Chennai

PostPosted: Thu Oct 11, 2007 10:33 pm
Reply with quote

Hi Nuthan,

Thanks a lot...I hope the statement would work for the following scenario

Table1
--------

Col1 Col2 Col3
----- ----- ------
aaa xxx 111
bbb yyy 222
ccc zzz 333

Now after the statment u specified is successful, we try to insert the following record in the Table2

Insert into Table2 Values (aaa,yyy,333);

I want this statement to fail...Though they are the values of the referred columns, the combination is invalid...So I believe the ALTER statement you specified would handle this...

Thanks again,
Syed
Back to top
View user's profile Send private message
nuthan

Active User


Joined: 26 Sep 2005
Posts: 146
Location: Bangalore

PostPosted: Fri Oct 12, 2007 9:49 am
Reply with quote

Try and let me know..
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts PARSE Syntax for not fix length word ... JCL & VSAM 7
No new posts CNTL= Command Syntax Documentation CA Products 3
This topic is locked: you cannot edit posts or make replies. issue with Syntax error SYNCSORT 2
No new posts Foreign character look different in m... COBOL Programming 3
No new posts DO we have alternate syntax for IFTHEN? Compuware & Other Tools 8
Search our Forums:

Back to Top