简体   繁体   English

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

[英]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

I am really new to Stackoverflow apologies for any mistakes.对于任何错误,我对 Stackoverflow 真的很陌生。

I am working on Qlikview.我正在研究 Qlikview。 It doesn't allow the columns which are of same name in two different tables.它不允许两个不同表中的同名列。 I used to create aliases for each and every column every time when i need to import tables from oracle.我曾经在每次需要从 oracle 导入表时为每一列创建别名。 Now i want to deal with large database.现在我想处理大型数据库。 So i decided to create a procedure that takes all the non constraint column as input and append there table name with column names.所以我决定创建一个过程,将所有非约束列作为输入,并在表名中附加列名。

I have written a sql query with basic knowledge that returns now primary columns but when coming to foreign keys doesn't work (it retrieving the columns which are having foreign keys)我已经编写了一个具有基本知识的 sql 查询,该查询现在返回主列,但是在使用外键时不起作用(它检索具有外键的列)

My query is as follows我的查询如下

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%';

Any suggestions will be appreciated任何建议将不胜感激

Thank you谢谢

This should give you all columns in all tables for a given schema_name where those columns are not part of a primary or foreign key这应该为您提供给定 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
/

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

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