Vous êtes sur la page 1sur 3

Obtener las tablas y vistas

SELECT
t1.table_name AS tabla_nombre,
pg_catalog.obj_description(t2.oid, 'pg_class') AS
tabla_descripcion
FROM
information_schema.tables t1
INNER JOIN pg_class t2 ON (t2.relname = t1.table_name)
WHERE
t1.table_schema='[NOMBRE_ESQUEMA]'
ORDER BY
t1.table_name;

Obtener las columnas de una tabla o vista


SELECT
t1.column_name AS columna_nombre,
t1.column_default AS columna_defecto,
t1.is_nullable AS columna_nulo,
t1.data_type AS columna_tipo_dato,
COALESCE(t1.numeric_precision,
t1.character_maximum_length) AS columna_longitud,
pg_catalog.col_description(t2.oid,
t1.dtd_identifier::int) AS columna_descripcion,
domain_name AS columna_dominio
FROM
information_schema.columns t1
INNER JOIN pg_class t2 ON (t2.relname = t1.table_name)
WHERE
t1.table_schema = '[NOMBRE_ESQUEMA]' AND
t1.table_name = '[NOMBRE_TABLA]'
ORDER BY
t1.table_name,
t1.ordinal_position;

Obtener las restricciones de la columna de una tabla


SELECT
t1.constraint_type AS tipo_restriccion,
t4.table_name AS tabla_referencia,
t4.column_name AS columna_referencia
FROM information_schema.table_constraints t1
LEFT JOIN information_schema.key_column_usage t2
ON t1.constraint_catalog = t2.constraint_catalog
AND t1.constraint_schema = t2.constraint_schema
AND t1.constraint_name = t2.constraint_name
LEFT JOIN information_schema.referential_constraints t3
ON t1.constraint_catalog = t3.constraint_catalog
AND t1.constraint_schema = t3.constraint_schema
AND t1.constraint_name = t3.constraint_name
LEFT JOIN information_schema.constraint_column_usage t4
ON t3.unique_constraint_catalog = t4.constraint_catalog
AND t3.unique_constraint_schema = t4.constraint_schema
AND t3.unique_constraint_name = t4.constraint_name
WHERE
t1.table_schema = '[NOMBRE_ESQUEMA]' AND
t1.table_name = '[NOMBRE_TABLA]' AND
t2.column_name = '[NOMBRE_COLUMNA]'
ORDER BY
t1.constraint_type DESC;

Obtener las tablas y vistas


SELECT
t1.table_name AS tabla_nombre,
pg_catalog.obj_description(t2.oid, 'pg_class') AS
tabla_descripcion
FROM
information_schema.tables t1
INNER JOIN pg_class t2 ON (t2.relname = t1.table_name)
WHERE
t1.table_schema='[NOMBRE_ESQUEMA]'
ORDER BY
t1.table_name;

Obtener las columnas de una tabla o vista


SELECT
t1.column_name AS columna_nombre,
t1.column_default AS columna_defecto,
t1.is_nullable AS columna_nulo,
t1.data_type AS columna_tipo_dato,
COALESCE(t1.numeric_precision,
t1.character_maximum_length) AS columna_longitud,
pg_catalog.col_description(t2.oid,
t1.dtd_identifier::int) AS columna_descripcion,
domain_name AS columna_dominio
FROM
information_schema.columns t1
INNER JOIN pg_class t2 ON (t2.relname = t1.table_name)
WHERE
t1.table_schema = '[NOMBRE_ESQUEMA]' AND
t1.table_name = '[NOMBRE_TABLA]'
ORDER BY
t1.table_name,
t1.ordinal_position;

Obtener las restricciones de la columna de una tabla


SELECT
t1.constraint_type AS tipo_restriccion,
t4.table_name AS tabla_referencia,
t4.column_name AS columna_referencia
FROM information_schema.table_constraints t1
LEFT JOIN information_schema.key_column_usage t2
ON t1.constraint_catalog = t2.constraint_catalog
AND t1.constraint_schema = t2.constraint_schema
AND t1.constraint_name = t2.constraint_name
LEFT JOIN information_schema.referential_constraints t3
ON t1.constraint_catalog = t3.constraint_catalog
AND t1.constraint_schema = t3.constraint_schema
AND t1.constraint_name = t3.constraint_name
LEFT JOIN information_schema.constraint_column_usage t4
ON t3.unique_constraint_catalog = t4.constraint_catalog
AND t3.unique_constraint_schema = t4.constraint_schema
AND t3.unique_constraint_name = t4.constraint_name
WHERE
t1.table_schema = '[NOMBRE_ESQUEMA]' AND
t1.table_name = '[NOMBRE_TABLA]' AND
t2.column_name = '[NOMBRE_COLUMNA]'
ORDER BY
t1.constraint_type DESC;