简体   繁体   English

如何选择“ user_id”列所在的所有表名?

[英]How to select all table names where a 'user_id' column is?

I'd like to select all table names where one of columns has a name user_id . 我想选择其中一列名称为user_id的所有表名称。

Is it possible in PostgreSQL? 在PostgreSQL中可以吗?

Try this: 尝试这个:

select table_name
from information_schema.columns
where column_name = 'user_id'

It should work OK. 应该可以。

Make sure to include the schema name . 确保包括架构名称 Your table name can be ambiguous without it. 没有它,您的表名可能会模棱两可。 May even lead to harmful confusion: 甚至可能导致有害的混乱:

SELECT table_schema, table_name
FROM information_schema.columns
WHERE column_name = 'user_id';

The information schema is only good if you need cross-RDBMS portability, which is rarely needed and rarely works. 只有需要跨RDBMS的可移植性(很少需要且很少起作用)时,信息模式才是好的。 I'd rather query the catalog tables directly. 我宁愿直接查询目录表。 Faster and shorter: 更快更短:

SELECT attrelid::regclass
FROM   pg_attribute
WHERE  attname = 'user_id';

And be sure that you are connected to the same database. 并确保您已连接到同一数据库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM