CONOCER LAS CONSTRAINTS FOREIGN KEY DE UNA TABLA - Knowing the foreign keys of a Informix table by Sql
Si queremos conocer las constraints de tipo Foreign Key que apuntan a una determinada tabla por SQL, podemos utilizar esta query en la que sólo debeis sustituir "my_table_name" por el nombre de la tabla que querais inspeccionar.
SELECT d.tabname primaria, i.colname columna,
a.tabname secundaria, f.colname columna,
b.constrname constraint, b.owner propietario, b.constrtype tipo,
CASE c.delrule
WHEN 'C' then 'SI'
WHEN 'R' then 'NO'
END cascade_delete
FROM systables a,
sysconstraints b,
sysreferences c,
systables d,
sysindexes e,
syscolumns f,
sysconstraints g,
sysindexes h,
syscolumns i
WHERE d.tabname = "my_table_name"
AND c.ptabid = d.tabid
AND b.constrid = c.constrid
AND g.constrid = c.primary
AND b.constrtype = 'R'
AND a.tabid = b.tabid
AND e.idxname = b.idxname -- Indice tabla secundaria
AND h.idxname = g.idxname -- Indice tabla primaria
AND i.tabid = g.tabid -- Columnas tabla primaria
AND f.tabid = b.tabid -- Columnas tabla secundaria
AND ((f.colno = e.part1 AND i.colno = h.part1) or
(f.colno = e.part2 AND i.colno = h.part2) or
(f.colno = e.part3 AND i.colno = h.part3) or
(f.colno = e.part4 AND i.colno = h.part4) or
(f.colno = e.part5 AND i.colno = h.part5) or
(f.colno = e.part6 AND i.colno = h.part6) or
(f.colno = e.part7 AND i.colno = h.part7) or
(f.colno = e.part8 AND i.colno = h.part8) or
(f.colno = e.part9 AND i.colno = h.part9) or
(f.colno = e.part10 AND i.colno = h.part10) or
(f.colno = e.part11 AND i.colno = h.part11) or
(f.colno = e.part12 AND i.colno = h.part12) or
(f.colno = e.part13 AND i.colno = h.part13) or
(f.colno = e.part14 AND i.colno = h.part14) or
(f.colno = e.part15 AND i.colno = h.part15) or
(f.colno = e.part16 AND i.colno = h.part16))
Los tipos de constrains reflejados en la columna constrtype de la tabla sysconstraints pueden ser:
- C Constraint de tipo Check
- P Constraint Primary Key
- R Constraint Reference (Foreign Key)
- U Constraint Unique
- N Constraint NOT NULL
Espero que os sea de utilidad.
Muy buna la query , muchas gracias
ResponderEliminar