如何从特定数据库中的所有表中检索不属于主键和外键的所有列

How to retrieve all the columns from all of the tables in a particular database which are not part of primary keys and foreign keys

我对 Whosebug 很陌生,如有任何错误,我深表歉意。

我正在研究 Qlikview。它不允许在两个不同的 table 中具有相同名称的列。每次需要从 oracle 导入 tables 时,我都会为每一列创建别名。现在我想处理大型数据库。所以我决定创建一个过程,将所有非约束列作为输入,并在 table 名称后附加列名称。

我写了一个 sql 查询,其基本知识是 returns 现在是主列,但是在使用外键时不起作用(它检索具有外键的列)

我的查询如下

SELECT C.table_name,
        C.column_name
FROM user_constraints a,
  user_cons_columns b,
  ALL_TAB_COLUMNS C
WHERE a.OWNER          =b.owner
AND a.OWNER            =C.owner
AND C.COLUMN_NAME      !=b.COLUMN_NAME
AND a.CONSTRAINT_NAME  =b.CONSTRAINT_NAME
AND a.table_name       =b.table_name
AND a.table_name       =C.table_name
AND a.constraint_type IN('P','R')
AND a.table_name NOT LIKE 'BIN%'
AND A.TABLE_NAME NOT LIKE 'DEF%'
AND b.table_name NOT LIKE 'BIN%'
AND b.TABLE_NAME NOT LIKE 'DEF%'
AND C.table_name NOT LIKE 'BIN%'
AND C.TABLE_NAME NOT LIKE 'DEF%';

如有任何建议,我们将不胜感激

谢谢

这应该会为您提供给定 schema_name 所有表中的所有列,其中这些列不属于主键或外键

SELECT  atc.owner,
    atc.table_name,
    atc.column_name 
FROM
    all_tab_columns atc
WHERE
    NOT EXISTS
    (
        SELECT  acc.owner,
            acc.table_name,
            acc.column_name
        FROM
            all_cons_columns acc
        LEFT
        JOIN    all_constraints ac ON acc.owner = ac.owner AND ac.constraint_name = acc.constraint_name AND ac.constraint_type IN ('P', 'R')
        WHERE
            atc.owner = atc.owner
        AND acc.table_name = atc.table_name
        AND acc.column_name = atc.column_name
    )
AND atc.owner = 'YOUR_SCHEMA_NAME'
ORDER
BY  1, 2
/