ORACLE数据库对⽐表结构
有时候会有某种需求:需要对⽐两个表的表结构是否⼀致,有时候甚⾄是整个数据库所有表的表结构对⽐......。表结构对⽐⽆⾮就是字段名、字段类型、字段数据类型、以及字段的顺序的对⽐。如果需要对⽐表结构,可以通过下⾯简单的脚本实现:
SELECT M.OWNER
,M.TABLE_NAME
,M.COLUMN_ID
,M.COLUMN_NAME
,M.DATA_TYPE
,M.DATA_LENGTH
,N.OWNER
,N.TABLE_NAME
,N.COLUMN_ID
,N.COLUMN_NAME
,N.DATA_TYPE
,N.DATA_LENGTH
FROM
(
SELECT OWNER
,TABLE_NAME
,COLUMN_ID
,COLUMN_NAME
,DATA_TYPE
,
DATA_LENGTH
FROM DBA_TAB_COLUMNS
WHERE OWNER=&OWNER_A
AND TABLE_NAME=&TABLE_NAME_A
) M LEFT JOIN
(
SELECT OWNER
,TABLE_NAME
,COLUMN_ID
,COLUMN_NAME
,DATA_TYPE
,
DATA_LENGTH
FROM DBA_TAB_COLUMNS
WHERE OWNER=&OWNER_B
AND TABLE_NAME=&TABLE_NAME_B
) N ON  M.COLUMN_ID =N.COLUMN_ID
AND M.COLUMN_NAME = N.COLUMN_NAME
AND M.DATA_TYPE = N.DATA_TYPE
AND M.DATA_LENGTH= N.DATA_LENGTH
ORDER BY M.TABLE_NAME, M.COLUMN_ID;
但是如果A表与B表前⾯的字段⼀致,然⽽B表有⼀些额外的字段,那么上 ⾯的SQL(左连接)就会查不出两者之间结构的不同。此时需要使⽤右连接才能对⽐出真正的表结构。所以如果需要对⽐两者的表结构是否⼀致,最好左连接查⼀ 次,右连接查⼀次。才能真正的确认两者的表结构的异同。
SELECT M.OWNER
,M.TABLE_NAME
,M.COLUMN_ID
,M.COLUMN_NAME
,M.DATA_TYPE
,M.DATA_LENGTH
,N.OWNER
,N.TABLE_NAME
,N.COLUMN_ID
,N.COLUMN_NAME
,N.DATA_TYPE
,
N.DATA_LENGTH
FROM
(
SELECT OWNER
,TABLE_NAME
,COLUMN_ID
,COLUMN_NAME
,DATA_TYPE
,DATA_LENGTH
FROM DBA_TAB_COLUMNS WHERE OWNER=&OWNER_A
AND TABLE_NAME=&TABLE_NAME_A ) M RIGHT JOIN
(
SELECT OWNER
,TABLE_NAME
,COLUMN_ID
,COLUMN_NAME
,DATA_TYPE
,DATA_LENGTH
FROM DBA_TAB_COLUMNS
WHERE OWNER=&OWNER_Boracle数据库表结构怎么看
AND TABLE_NAME=&TABLE_NAME_B
) N ON  M.COLUMN_ID =N.COLUMN_ID
AND M.COLUMN_NAME = N.COLUMN_NAME AND M.DATA_TYPE = N.DATA_TYPE
AND M.DATA_LENGTH= N.DATA_LENGTH ORDER BY M.TABLE_NAME, M.COLUMN_ID;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。