【问题标题】:Query in Oracle to get columns in a table with primary key info在 Oracle 中查询以获取具有主键信息的表中的列
【发布时间】:2015-06-05 14:10:16
【问题描述】:

什么是获取表描述的查询,其中的列带有标记的主键的一部分?例如,它会返回如下内容:

Table   Column       is_part_of_pk
-----   ------       -------------
ADDRESS  ID           1
ADDRESS  ADDR_LINE_1  0
ADDRESS  ADDR_CITY    0
STUDENT  FIRST_NAME   1
STUDENT  LAST_NAME    1
STUDENT  CLASS_NAME   0

所有表中的列都被列出,并且是或属于主键的列被“标记”。我试图针对 user_tab_columns、all_cons_columns 和 all_constraints 进行选择,但我得到了重复的列。谢谢。

【问题讨论】:

    标签: oracle primary-key


    【解决方案1】:

    试试这个

    select col.table_name , col.column_name, 
    case when exists(select 'x' from USER_CONSTRAINTS l 
    join USER_CONS_COLUMNS ll on LL.CONSTRAINT_NAME = L.CONSTRAINT_NAME
    where l.table_name = col.table_name and l.constraint_type = 'P' and ll.column_name = col.column_name) then 1 else 0 end is_part_of_pk
     from USER_TAB_COLUMNS col where table_name = :some_table_name
     order by column_id;
    

    【讨论】:

    • 也许还可以更好地添加方案:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-10-15
    • 1970-01-01
    • 1970-01-01
    • 2015-03-14
    • 1970-01-01
    相关资源
    最近更新 更多