Oracle需要像INFORMATION_SCHEMA为MySQL提供的查询

时间:2017-07-01 22:57:59

标签: mysql sql oracle

我需要一个产生类似于INFORMATION_SCHEMA为MySQL提供的输出的单个查询。

例如,在MySQL中,我有:

SELECT 
  TABLE_SCHEMA, TABLE_NAME, COLUMN_NAME, IS_NULLABLE, COLUMN_TYPE, DATA_TYPE, CHARACTER_MAXIMUM_LENGTH, COLUMN_KEY
FROM INFORMATION_SCHEMA.COLUMNS WHERE
TABLE_NAME='person' AND TABLE_SCHEMA='test';

对于Oracle,我很难获得一个列,指明COLUMN_NAME是否是该表的主键。我有:

SELECT 
  utc.COLUMN_ID, utc.TABLE_NAME, utc.COLUMN_NAME, utc.DATA_TYPE, utc.DATA_LENGTH, utc.NULLABLE
FROM USER_TAB_COLUMNS utc
WHERE TABLE_NAME = 'PERSON';

有没有人有办法通过LEFT JOIN或类似方法修改查询?

谢谢!

1 个答案:

答案 0 :(得分:2)

供将来参考......

SELECT 
  utc.COLUMN_ID, utc.TABLE_NAME, utc.COLUMN_NAME, utc.DATA_TYPE, utc.DATA_LENGTH, utc.NULLABLE,
  (SELECT cons.status
   FROM all_constraints cons, all_cons_columns cols
   WHERE 
     cols.table_name = 'PERSON'
     AND cols.column_name = utc.COLUMN_NAME
     AND cons.constraint_type = 'P'
     AND cons.constraint_name = cols.constraint_name
     AND cons.owner = cols.owner) AS IS_PRIMARY_KEY
FROM USER_TAB_COLUMNS utc
WHERE TABLE_NAME = 'PERSON';