tin*_*lyx 4 postgresql metadata union
我想知道是否有一种既定的方法来测试两个 PostgreSQL 表/关系是否具有相同的结构。换句话说,如何测试它们是否彼此兼容,因为我可以对它们执行诸如UNION ALL
和 之类的集合操作?EXCEPT
我在 DBA.SE 和其他地方搜索,只能找到有关查找两个表的内容是否不同的问题(例如,检查两个表在 PostgreSQL 中是否具有相同的内容),或者当兼容性已知时(例如,比较两个表)具有相同的结构,但成员数量不同)。但我有兴趣检查表结构的兼容性。
我使用的是 PostgreSQL 10.3,但符合标准的方式当然更好。
具有如下示例架构:
create table table1 (
id integer,
txt text,
col1 integer,
col2 integer);
create table table2 (
id integer,
txt text,
col1 text,
colx integer);
Run Code Online (Sandbox Code Playgroud)
假设您不关心列名,您可以使用查询检查联合列中的潜在冲突:
(select data_type, ordinal_position
from information_schema.columns
where table_name = 'table1'
except
select data_type, ordinal_position
from information_schema.columns
where table_name = 'table2')
union all
(select data_type, ordinal_position
from information_schema.columns
where table_name = 'table2'
except
select data_type, ordinal_position
from information_schema.columns
where table_name = 'table1'
)
order by 2;
Run Code Online (Sandbox Code Playgroud)
结果将显示违规列在表结构和数据类型中的位置。
data_type ordinal_position
integer 3
text 3
Run Code Online (Sandbox Code Playgroud)
如果您有这个缩短的列表,您可以检查 information_schema.columns 词典中的详细信息,以检查有关每列的详细信息。
归档时间: |
|
查看次数: |
6613 次 |
最近记录: |