English 中文(简体)
列出与特定模式相匹配的栏目表
原标题:Find a table with a column name matching a given pattern

这可能听起来不现实,但我最近不得不研究一个系统的数据库结构,而我却对我感到有些不安,因为我想找到与某个表格有联系的所有栏目(见原始杰夫没有建立明确的关系,而是把关系列入一栏!)

例如,假定指<代码> 括号>的所有栏目。 作为记录两个领域之间关系的手段,有<代码>table.somefield_will,我如何利用标准指挥/内部设计的职能,发现所有这些关系(和有关表格)?

欢迎任何针对MySQL或PogreSQL的解决办法。

注:

  • am not looking for patterns in the data, but in the meta-data.
  • I know I can dump the db structure and search for the results there, but I don t want to go out of the db-shell to find these relationships (assume am working in say psql, and don t want to exit)
最佳回答

PostgreSQL,请查询系统数据表。

你还可以询问Schema的信息,该信息包括提供显示信息的标准方式的意见。 记录系统数据集通常要更快得多,尽管如此。

寻找一个特定孩子:<代码>/代码>。 页: 1

SELECT n.nspname AS schema_name
     , c.relname AS table_name
     , a.attname AS column_name
FROM   pg_catalog.pg_attribute a
JOIN   pg_catalog.pg_class c ON c.oid = a.attrelid
JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
WHERE  a.attname ~~ E %\_parent 
AND    NOT a.attisdropped
AND    c.relkind =  r 
AND    nspname !~~ E pg\_% ;

根据说明查找<>all相关栏目:

SELECT n.nspname AS parrent_schema
     , c.relname AS parrent_table
     , a.attname AS parrent_column
     , n1.nspname AS child_schema
     , c1.relname AS child_table
     , a1.attname AS child_column
FROM   pg_catalog.pg_attribute a
JOIN   pg_catalog.pg_class c ON c.oid = a.attrelid
JOIN   pg_catalog.pg_namespace n ON n.oid = c.relnamespace
JOIN   pg_catalog.pg_attribute a1 ON a.attname = substring(a1.attname,  _(.*?)$ )
JOIN   pg_catalog.pg_class c1 ON c1.oid = a1.attrelid
JOIN   pg_catalog.pg_namespace n1 ON n1.oid = c1.relnamespace
WHERE  c.relkind =  r 
AND    c1.relkind =  r 
AND    n.nspname !~~ E pg\_% 
AND    n1.nspname !~~ E pg\_% 
AND    NOT a.attisdropped
AND    NOT a1.attisdropped
-- AND    n.nspname =  public    -- to narrow it down to a specific schema
-- AND    n1.nspname =  public    -- to narrow it down to a specific schema
ORDER  BY 1,2,3,4,5,6

This assumes that the parrent columns don t have _ in the name.
The key element is the join condition:

a.attname = substring(a1.attname,  _(.*?)$ )
问题回答

Try query querying the information_schema.columns view. 它在MySQL工作,但我没有在Pogres进行审判。 <代码>信息_schema 该表是ANSIQ标准的一部分,因此,邮政局很可能也会这样做。

Other views in information_schema include information_schema.tables, which is useful as well

如果你使用MySQL,你可以为此利用数据库信息。 该数据库载有该服务器数据库的所有元数据。 就我所知,每个数据库用户都必须能够查阅该数据库。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签