Subject | Extract Fk dependant columns |
---|---|
Author | mourad@bilog.fr |
Post date | 2009-11-17T15:44:37Z |
Hello everybody,
I've developped an application whitch updates the data of my database. Fore that, I must drop the Foreign keys and when I finish I create them.
From http://www.alberton.info/firebird_sql_meta_info.html, I've taken inforation how to get the Foreign key details but it wasen't very helpfull.
Sothat I've traied to divise my operations and I've the following queries :
1) To have the table's FK :
SELECT rc.RDB$CONSTRAINT_NAME AS fk_name, refc.RDB$DELETE_RULE AS on_del, refc.RDB$UPDATE_RULE AS on_upd
FROM RDB$RELATION_CONSTRAINTS rc
LEFT JOIN RDB$REF_CONSTRAINTS refc ON rc.RDB$CONSTRAINT_NAME = refc.RDB$CONSTRAINT_NAME
WHERE rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
AND rc.RDB$RELATION_NAME = 'MY_TABLE'
2) To have the column on whitch the FK is applied :
select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = 'MY_TABLE' order by RDB$FIELD_POSITION
3)To have dependant table of every FK :
ELECT rc.RDB$RELATION_NAME AS PARENT
FROM RDB$RELATION_CONSTRAINTS a
JOIN RDB$INDICES b ON (b.RDB$INDEX_NAME=a.RDB$INDEX_NAME)
JOIN RDB$RELATION_CONSTRAINTS rc ON rc.rdb$index_name=b.rdb$foreign_key
WHERE a.RDB$CONSTRAINT_TYPE='FOREIGN KEY'
AND a.RDB$RELATION_NAME = 'MY_TABLE'
and a.RDB$INDEX_NAME = 'MY_FK'
4)And for having the dependant column(s) :
SELECT DISTINCT d1.RDB$FIELD_NAME AS ref_field
FROM RDB$RELATION_CONSTRAINTS rc
JOIN RDB$INDICES b ON (b.RDB$INDEX_NAME = rc.RDB$INDEX_NAME)
JOIN RDB$RELATION_CONSTRAINTS a ON (a.rdb$index_name = b.rdb$foreign_key)
inner JOIN RDB$DEPENDENCIES d1 ON d1.RDB$DEPENDED_ON_NAME = a.RDB$RELATION_NAME
WHERE rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
and d1.RDB$DEPENDENT_TYPE = 2 "
AND rc.RDB$RELATION_NAME = 'MY_TABLE' "
AND rc.RDB$CONSTRAINT_NAME = 'MY_FK' "
AND a.RDB$RELATION_NAME = 'MY_DEPENDENT_TABLE'
My problem is that the last query dont work fine. Can anyone tell me whete I'm wrong.
Many thanks for your usefull help.
Best regards.
Mourad
I've developped an application whitch updates the data of my database. Fore that, I must drop the Foreign keys and when I finish I create them.
From http://www.alberton.info/firebird_sql_meta_info.html, I've taken inforation how to get the Foreign key details but it wasen't very helpfull.
Sothat I've traied to divise my operations and I've the following queries :
1) To have the table's FK :
SELECT rc.RDB$CONSTRAINT_NAME AS fk_name, refc.RDB$DELETE_RULE AS on_del, refc.RDB$UPDATE_RULE AS on_upd
FROM RDB$RELATION_CONSTRAINTS rc
LEFT JOIN RDB$REF_CONSTRAINTS refc ON rc.RDB$CONSTRAINT_NAME = refc.RDB$CONSTRAINT_NAME
WHERE rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
AND rc.RDB$RELATION_NAME = 'MY_TABLE'
2) To have the column on whitch the FK is applied :
select RDB$FIELD_NAME from RDB$INDEX_SEGMENTS where RDB$INDEX_NAME = 'MY_TABLE' order by RDB$FIELD_POSITION
3)To have dependant table of every FK :
ELECT rc.RDB$RELATION_NAME AS PARENT
FROM RDB$RELATION_CONSTRAINTS a
JOIN RDB$INDICES b ON (b.RDB$INDEX_NAME=a.RDB$INDEX_NAME)
JOIN RDB$RELATION_CONSTRAINTS rc ON rc.rdb$index_name=b.rdb$foreign_key
WHERE a.RDB$CONSTRAINT_TYPE='FOREIGN KEY'
AND a.RDB$RELATION_NAME = 'MY_TABLE'
and a.RDB$INDEX_NAME = 'MY_FK'
4)And for having the dependant column(s) :
SELECT DISTINCT d1.RDB$FIELD_NAME AS ref_field
FROM RDB$RELATION_CONSTRAINTS rc
JOIN RDB$INDICES b ON (b.RDB$INDEX_NAME = rc.RDB$INDEX_NAME)
JOIN RDB$RELATION_CONSTRAINTS a ON (a.rdb$index_name = b.rdb$foreign_key)
inner JOIN RDB$DEPENDENCIES d1 ON d1.RDB$DEPENDED_ON_NAME = a.RDB$RELATION_NAME
WHERE rc.RDB$CONSTRAINT_TYPE = 'FOREIGN KEY'
and d1.RDB$DEPENDENT_TYPE = 2 "
AND rc.RDB$RELATION_NAME = 'MY_TABLE' "
AND rc.RDB$CONSTRAINT_NAME = 'MY_FK' "
AND a.RDB$RELATION_NAME = 'MY_DEPENDENT_TABLE'
My problem is that the last query dont work fine. Can anyone tell me whete I'm wrong.
Many thanks for your usefull help.
Best regards.
Mourad