473,402 Members | 2,064 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,402 software developers and data experts.

recursive sql

My data model has many tables in many hierarchies.
I believe that by using recursion, I can check for all invalid codes in
the working tables that do not exist in the code tables.
For example, I would like to check the EMPLOYEE table for state_codes
that do not exist in the STATE_CT table where
EMPLOYEE.state_code=STATE_CT.state_code.
Another more complex example is ensuring PROJECT.project_code exists in
PROJECT_CT where EMPLOYEE.employee_id = PROJECT.employee_id and
PROJECT.project_code = PROJECT_CT.project_code.

My plan is to dynamically create a sql statement based on a recursive
query. I am willing to write a .ksh script in conjunction with the sql,
but I'd like to get as far as possible with just sql.
The strong point to this approach is simply adding code tables to a
table and have the script run each month to report invalid codes.

Question: I can get a nice table listing, but I am stumped on the
columns - I would like to have all the join columns in a row to create
a WHERE clause. Run this to see the results.
drop table RITables;
create table RITables
(TABLE_ID INTEGER NOT NULL,
TABNAME VARCHAR(50) NOT NULL,
TABSCHEMA VARCHAR(20) NOT NULL,
APPLICATION VARCHAR(20) NOT NULL
) IN STG_MCR_08_01;

insert into RITables
values
(1, 'COMPANY','SCHEMA','ORG_CHART'),
(2, 'EMPLOYEE','SCHEMA','ORG_CHART'),
(3, 'STATE_CT','SCHEMA','ORG_CHART'),
(4, 'EMPLOYEETYPE_CT','SCHEMA','ORG_CHART'),
(5, 'PROJECT','SCHEMA','ORG_CHART'),
(6, 'PROJECT_TYPE_CT','SCHEMA','ORG_CHART');

select * from RITables;

----

drop table RIColumns;
create table RIColumns
(COLUMN_ID INTEGER NOT NULL,
TABLE_ID INTEGER NOT NULL,
COLNAME VARCHAR(50) NOT NULL,
KEYCOLUMN CHAR(1) NOT NULL
) IN STG_MCR_08_01;

insert into RIColumns
values
(100,1,'EMPLOYEE_ID','Y'),
(101,2,'EMPLOYEE_ID','Y'),
(102,2,'STATE_CODE','N'),
(103,2,'EMPLOYEETYPE_CODE','N'),
(104,3,'STATE_CODE','Y'),
(105,4,'EMPLOYEETYPE_CODE','Y'),
(106,5,'EMPLOYEE_ID','Y'),
(107,5,'PROJECT_TYPE_CODE','N'),
(108,6,'PROJECT_TYPE_CODE','Y');

select * from RIColumns;

----

drop table RIRelationships;
create table RIRelationships
(RELATIONSHIP_ID INTEGER NOT NULL,
CHILDTABLE_ID INTEGER NOT NULL,
PARENTTABLE_ID INTEGER ,
PARENTCOLUMN_ID INTEGER NOT NULL,
CHILDCOLUMN_ID INTEGER ,
ENFORCE_RI_FLAG CHAR(1) NOT NULL
) IN STG_MCR_08_01;

insert into RIRelationships
values
(999, 1,null,100,null,'N'),
(1000, 2,1,100,101,'N'),
(1001, 3,2,102,104,'Y'),
(1002, 4,2,103,105,'Y'),
(1003, 5,2,101,106,'N'),
(1004, 6,5,107,108,'Y');

drop view RIRelationships_V ;
create view RIRelationships_V
(RELATIONSHIP_ID,
CHILDTABLE_ID, CHILDTABLE_NAME, CHILDCOLUMN_ID, CHILDCOLUMNNAME,
PARENTTABLE_ID, PARENTTABLE_NAME, PARENTCOLUMN_ID,PARENTCOLUMNNAME,
ENFORCE_RI_FLAG )
as
select RELATIONSHIP_ID,
CHAR(CHILDTABLE_ID) AS CHILDTABLE_ID, t2.tabname as CHILDTABLE_NAME,
char(childcolumn_id) as CHILDCOLUMN_ID, c2.colname as
CHILDCOLUMNNAME,
CHAR(PARENTTABLE_ID) AS PARENTTABLE_ID,t1.tabname as PARENTTABLE_NAME,
char(parentcolumn_id) as PARENTCOLUMN_ID, c1.colname as
PARENTCOLUMNNAME,
ENFORCE_RI_FLAG
from RIRelationships r, RITables t1, RITables t2, RIColumns c1,
RIColumns c2
where t1.table_id = r.parenttable_id
and t2.table_id = r.childtable_id
and (c1.column_id = r.parentcolumn_id and
c1.table_id=r.parenttable_id)
and (c2.column_id = r.childcolumn_id and c2.table_id =
r.childtable_id);

select * from RIRelationships_V;

--MAIN SQL
WITH parent (pname, pkey, pcolkey, pcolname, cname,ckey, colkey,
ccolname, lvl, path ,cols,flag ) AS
(SELECT DISTINCT PARENTTABLE_NAME,parenttable_id, parentcolumn_id,
parentcolumnname, PARENTTABLE_NAME,parenttable_id,parentcolumn_id,
parentcolumnname, 0 ,
varchar(PARENTTABLE_NAME ,100),
varchar(PARENTTABLE_NAME || '.' || parentcolumnname,500),
ENFORCE_RI_FLAG
FROM RIRelationships_V
WHERE PARENTTABLE_ID = '1'
UNION ALL
SELECT C.PARENTTABLE_NAME, C.parenttable_id , C.parentcolumn_id,
C.parentcolumnname, C.CHILDTABLE_NAME,C.childtable_id,C.childcolumn_id ,
C.childcolumnname, P.lvl + 1 ,
rtrim(P.path) || ',' || C.CHILDTABLE_NAME,
rtrim(P.cols) || ' = ' || C.CHILDTABLE_NAME || '.' || C.childcolumnname
,
ENFORCE_RI_FLAG
FROM RIRelationships_V C
,parent P
WHERE P.ckey = C.parenttable_id
AND P.lvl + 1 < 6
)
SELECT *
FROM parent
where flag='Y';

Oct 23 '06 #1
1 2169
I think you have bigger problems than figuring out how to validate
codes. You have a data model where you will need to be adding new
validation tables every month?? What are you going to do with the data
rows that don't match new tables?

RI constraints are one option to validate codes. Check constraints can
also be used. When the database manager validates the content, data is
always consistent and does not need regular scrubbing to guarantee that
it is valid.

Good design identifies ALL of the data relationships that exist at
design time. Extensive research is needed to locate the relationships
that are "known" but not documented so they can be included in the
design. The best designers also bring their personal knowledge of
business applications to the design table to enhance the design process.

Your approach is more like a dairy barn with a latching door at one end
and a missing wall at the other end. The cows have to be rounded up
every day, a time consuming process that could have been eliminated by
building the last wall before starting to use the barn.

Phil Sherman

a_***************@hotmail.com wrote:
My data model has many tables in many hierarchies.
I believe that by using recursion, I can check for all invalid codes in
the working tables that do not exist in the code tables.
For example, I would like to check the EMPLOYEE table for state_codes
that do not exist in the STATE_CT table where
EMPLOYEE.state_code=STATE_CT.state_code.
Another more complex example is ensuring PROJECT.project_code exists in
PROJECT_CT where EMPLOYEE.employee_id = PROJECT.employee_id and
PROJECT.project_code = PROJECT_CT.project_code.

My plan is to dynamically create a sql statement based on a recursive
query. I am willing to write a .ksh script in conjunction with the sql,
but I'd like to get as far as possible with just sql.
The strong point to this approach is simply adding code tables to a
table and have the script run each month to report invalid codes.

Question: I can get a nice table listing, but I am stumped on the
columns - I would like to have all the join columns in a row to create
a WHERE clause. Run this to see the results.
drop table RITables;
create table RITables
(TABLE_ID INTEGER NOT NULL,
TABNAME VARCHAR(50) NOT NULL,
TABSCHEMA VARCHAR(20) NOT NULL,
APPLICATION VARCHAR(20) NOT NULL
) IN STG_MCR_08_01;

insert into RITables
values
(1, 'COMPANY','SCHEMA','ORG_CHART'),
(2, 'EMPLOYEE','SCHEMA','ORG_CHART'),
(3, 'STATE_CT','SCHEMA','ORG_CHART'),
(4, 'EMPLOYEETYPE_CT','SCHEMA','ORG_CHART'),
(5, 'PROJECT','SCHEMA','ORG_CHART'),
(6, 'PROJECT_TYPE_CT','SCHEMA','ORG_CHART');

select * from RITables;

----

drop table RIColumns;
create table RIColumns
(COLUMN_ID INTEGER NOT NULL,
TABLE_ID INTEGER NOT NULL,
COLNAME VARCHAR(50) NOT NULL,
KEYCOLUMN CHAR(1) NOT NULL
) IN STG_MCR_08_01;

insert into RIColumns
values
(100,1,'EMPLOYEE_ID','Y'),
(101,2,'EMPLOYEE_ID','Y'),
(102,2,'STATE_CODE','N'),
(103,2,'EMPLOYEETYPE_CODE','N'),
(104,3,'STATE_CODE','Y'),
(105,4,'EMPLOYEETYPE_CODE','Y'),
(106,5,'EMPLOYEE_ID','Y'),
(107,5,'PROJECT_TYPE_CODE','N'),
(108,6,'PROJECT_TYPE_CODE','Y');

select * from RIColumns;

----

drop table RIRelationships;
create table RIRelationships
(RELATIONSHIP_ID INTEGER NOT NULL,
CHILDTABLE_ID INTEGER NOT NULL,
PARENTTABLE_ID INTEGER ,
PARENTCOLUMN_ID INTEGER NOT NULL,
CHILDCOLUMN_ID INTEGER ,
ENFORCE_RI_FLAG CHAR(1) NOT NULL
) IN STG_MCR_08_01;

insert into RIRelationships
values
(999, 1,null,100,null,'N'),
(1000, 2,1,100,101,'N'),
(1001, 3,2,102,104,'Y'),
(1002, 4,2,103,105,'Y'),
(1003, 5,2,101,106,'N'),
(1004, 6,5,107,108,'Y');

drop view RIRelationships_V ;
create view RIRelationships_V
(RELATIONSHIP_ID,
CHILDTABLE_ID, CHILDTABLE_NAME, CHILDCOLUMN_ID, CHILDCOLUMNNAME,
PARENTTABLE_ID, PARENTTABLE_NAME, PARENTCOLUMN_ID,PARENTCOLUMNNAME,
ENFORCE_RI_FLAG )
as
select RELATIONSHIP_ID,
CHAR(CHILDTABLE_ID) AS CHILDTABLE_ID, t2.tabname as CHILDTABLE_NAME,
char(childcolumn_id) as CHILDCOLUMN_ID, c2.colname as
CHILDCOLUMNNAME,
CHAR(PARENTTABLE_ID) AS PARENTTABLE_ID,t1.tabname as PARENTTABLE_NAME,
char(parentcolumn_id) as PARENTCOLUMN_ID, c1.colname as
PARENTCOLUMNNAME,
ENFORCE_RI_FLAG
from RIRelationships r, RITables t1, RITables t2, RIColumns c1,
RIColumns c2
where t1.table_id = r.parenttable_id
and t2.table_id = r.childtable_id
and (c1.column_id = r.parentcolumn_id and
c1.table_id=r.parenttable_id)
and (c2.column_id = r.childcolumn_id and c2.table_id =
r.childtable_id);

select * from RIRelationships_V;

--MAIN SQL
WITH parent (pname, pkey, pcolkey, pcolname, cname,ckey, colkey,
ccolname, lvl, path ,cols,flag ) AS
(SELECT DISTINCT PARENTTABLE_NAME,parenttable_id, parentcolumn_id,
parentcolumnname, PARENTTABLE_NAME,parenttable_id,parentcolumn_id,
parentcolumnname, 0 ,
varchar(PARENTTABLE_NAME ,100),
varchar(PARENTTABLE_NAME || '.' || parentcolumnname,500),
ENFORCE_RI_FLAG
FROM RIRelationships_V
WHERE PARENTTABLE_ID = '1'
UNION ALL
SELECT C.PARENTTABLE_NAME, C.parenttable_id , C.parentcolumn_id,
C.parentcolumnname, C.CHILDTABLE_NAME,C.childtable_id,C.childcolumn_id ,
C.childcolumnname, P.lvl + 1 ,
rtrim(P.path) || ',' || C.CHILDTABLE_NAME,
rtrim(P.cols) || ' = ' || C.CHILDTABLE_NAME || '.' || C.childcolumnname
,
ENFORCE_RI_FLAG
FROM RIRelationships_V C
,parent P
WHERE P.ckey = C.parenttable_id
AND P.lvl + 1 < 6
)
SELECT *
FROM parent
where flag='Y';
Oct 24 '06 #2

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

Similar topics

19
by: Carlos Ribeiro | last post by:
Hello all, Here I am using some deeply nested, tree-like data structures. In some situations I need to traverse the tree; the old-style way to do it is to write a recursive method on the node...
10
by: Steve Goldman | last post by:
Hi, I am trying to come up with a way to develop all n-length permutations of a given list of values. The short function below seems to work, but I can't help thinking there's a better way. ...
2
by: | last post by:
OK: Purpose: Using user's input and 3 recursive functions, construct an hour glass figure. Main can only have user input, loops and function calls. Recursive function 1 takes input and displays...
7
by: Jon Slaughter | last post by:
#pragma once #include <vector> class empty_class { }; template <int _I, int _J, class _element, class _property> class RDES_T {
1
by: Jon Slaughter | last post by:
I've managed to put together a template class that basicaly creates a recursive tree that lets you easily specify the "base" class of that tree and and ending notes and lets you stop the recursive...
4
by: Victor | last post by:
Hello, I've got a situation in which the number of (valid) recursive calls I make will cause stack overflow. I can use getrlimit (and setrlimit) to test (and set) my current stack size. ...
9
by: seberino | last post by:
I'm a compiler newbie and curious if Python grammar is able to be parsed by a recursive descent parser or if it requires a more powerful algorithm. Chris
0
by: champ1979 | last post by:
I wrote an algorithm to get all the relatives of a person in a family tree. I'm basically getting all the users from the DB and am doing the recursive logic in code, so that there is only 1 call...
18
by: Just Another Victim of the Ambient Morality | last post by:
Is pyparsing really a recursive descent parser? I ask this because there are grammars it can't parse that my recursive descent parser would parse, should I have written one. For instance: ...
3
by: from.future.import | last post by:
Hi, I encountered garbage collection behaviour that I didn't expect when using a recursive function inside another function: the definition of the inner function seems to contain a circular...
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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...

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.