If you ever get lucky to find the answer, please, post it here.
Most of us are rarely faced those specific features, but it may be useful to better know them.
FK constraint with colname <> in parent & child table
- sergeyken
- Posts: 458
- Joined: Wed Jul 24, 2019 10:12 pm
- Skillset: Assembler, JCL, Utilities, PL/I, C/C++, DB2, SQL, REXX, COBOL, etc. etc. etc.
- Referer: Internet search
Re: FK constraint with colname <> in parent & child table
Javas and Pythons come and go, but JCL and SORT stay forever.
-
- Posts: 17
- Joined: Fri Jul 08, 2022 7:29 pm
- Skillset: leblesmoutilabiskouti
- Referer: leblesmoutilabiskouti
Re: FK constraint with colname <> in parent & child table
I finally found the solution
You have to go through SYSIBM.SYSKEYS to retrieve the information from the index referenced in SYSRELS. IXNAME, for the columns in the order of SYSFOREIGNKEYS.COLSEQ
To make it more meaningful, I created a multi-column FK constraint in the following tables :
With the following query (note the addition of the SYSKEYS table) compared to my previous queries :
With the following query:
I finally get the expected result :


You have to go through SYSIBM.SYSKEYS to retrieve the information from the index referenced in SYSRELS. IXNAME, for the columns in the order of SYSFOREIGNKEYS.COLSEQ
To make it more meaningful, I created a multi-column FK constraint in the following tables :
Code: Select all
CREATE TABLE SCH0.T020
( T20C01 SMALLINT NOT NULL PRIMARY KEY
, T20C02 CHAR(8) NOT NULL
, T20C03 DATE NOT NULL
, T20C04 INTEGER NOT NULL
, T20C05 SMALLINT NOT NULL
, UNIQUE (T20C04, T20C05)
)
;
CREATE UNIQUE INDEX SCH0.UT020
ON SCH0.T020 (T20C04 ASC, T20C05 ASC)
USING STOGROUP
[. . .]
;
CREATE TABLE SCH0.T021
( T21C01 SMALLINT NOT NULL PRIMARY KEY
, T21C02 INTEGER
, T21C03 SMALLINT
, T21C04 DATE NOT NULL
, FOREIGN KEY FK21AK20(T21C02, T21C03)
REFERENCES SCH0.T020(T20C04, T20C05)
ON DELETE SET NULL
)
;
With the following query (note the addition of the SYSKEYS table) compared to my previous queries :
Code: Select all
set current schema = 'SYSIBM'
;
with CT1(SCH1, TAB1) as
(select 'SCH0', 'T020' from SYSIBM.SYSDUMMY1 union all
select 'SCH0', 'T021' from SYSIBM.SYSDUMMY1
)
select distinct
substr(RL.CREATOR, 01, 4) as "Schm"
, substr(RL.REFTBNAME, 01, 08) as "ParentTb"
, substr(RL.TBNAME, 01, 08) as "Child Tb"
, substr(RL.RELNAME, 01, 20) as "Constr"
, RL.DELETERULE as "OnDel"
, RL.ENFORCED as "Enf"
, RL.CHECKEXISTINGDATA as "Chk"
, substr(KY.COLNAME, 01, 08) as "ParenCol"
, KY.COLNO as "PColNo"
, substr(FK.COLNAME, 01, 08) as "ChildCol"
, FK.COLNO as "CColNo"
from SYSRELS RL
inner join CT1 CT
on (RL.CREATOR = CT.SCH1 and RL.TBNAME = CT.TAB1)
or (RL.REFTBCREATOR = CT.SCH1 and RL.REFTBNAME = CT.TAB1)
inner join SYSFOREIGNKEYS FK
on FK.CREATOR = RL.CREATOR
and FK.RELNAME = RL.RELNAME
left join SYSKEYS KY
on KY.IXCREATOR=CT.SCH1
and KY.IXNAME =RL.IXNAME
and KY.COLSEQ =FK.COLSEQ
order by "ParentTb"
, "Child Tb"
, "Constr"
, "CColNo"
;
With the following query:
I finally get the expected result :
Code: Select all
---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+------
Schm ParentTb Child Tb Constr OnDel Enf Chk ParenCol PColNo ChildCol CColNo
---------+---------+---------+---------+---------+---------+---------+---------+---------+---------+------
SCH0 T020 T021 FK21AK20 N Y I T20C04 4 T21C02 2
SCH0 T020 T021 FK21AK20 N Y I T20C05 5 T21C03 3

- sergeyken
- Posts: 458
- Joined: Wed Jul 24, 2019 10:12 pm
- Skillset: Assembler, JCL, Utilities, PL/I, C/C++, DB2, SQL, REXX, COBOL, etc. etc. etc.
- Referer: Internet search
Re: FK constraint with colname <> in parent & child table
Thank you for this submission!
It's good to know this, for the future.
It's good to know this, for the future.

Javas and Pythons come and go, but JCL and SORT stay forever.
-
- Similar Topics
- Replies
- Views
- Last post
-
-
FORMAT THE FILE SO ALL CHILD UNDER SAME PARENT IN ONE LINE
by azhar » Wed May 31, 2023 4:27 pm » in JCL - 1
- 1753
-
by sergeyken
View the latest post
Thu Jun 01, 2023 3:06 am
-
-
- 3
- 1199
-
by sergeyken
View the latest post
Wed May 18, 2022 4:40 pm
-
- 0
- 3055
-
by dylson
View the latest post
Sat Aug 01, 2020 11:09 pm
-
- 5
- 2725
-
by sergeyken
View the latest post
Thu Feb 25, 2021 8:19 pm
-
-
Array processing and Table handling with packed decimal
by rogerstrycova » Tue Oct 26, 2021 3:55 pm » in IBM Cobol - 2
- 1706
-
by Robert Sample
View the latest post
Wed Oct 27, 2021 1:12 am
-