主键列获取(难度系数大)

liulei68211 2016-11-21 10:53:59
在Oracel中 想得到以下结果,判断所有字段,是主键的话在主键列下显示‘True’,不是的话显示‘’
现在得到以下结果,指找到主键列,其他列没有

各位大神该怎么改?
...全文
119 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liulei68211 2016-11-21
  • 打赏
  • 举报
回复
感觉比SQL SERVE简单许多啊
卖水果的net 2016-11-21
  • 打赏
  • 举报
回复
PS:你自己关联一下就可以了,比我还懒。。
卖水果的net 2016-11-21
  • 打赏
  • 举报
回复


SQL> 
SQL> create table test(c1 int, c2 int, c3 int);
Table created
SQL> alter table test add constraint PK_TEST primary key (c1);
Table altered
SQL> select table_name, column_name
  2  from user_cons_columns where constraint_name = 'PK_TEST';
TABLE_NAME                     COLUMN_NAME
------------------------------ -----------------------------------------------
TEST                           C1
SQL> alter table test drop primary key ;
Table altered
SQL> alter table test add constraint PK_TEST primary key (c1,c2);
Table altered
SQL> select c1.colno , c1.cname, nvl2(c2.column_name,'true','false') PK
  2  from col c1
  3  left join user_cons_columns c2
  4  on c1.tname = c2.table_name
  5  and c1.cname = c2.column_name
  6  and constraint_name = 'PK_TEST'
  7  where c1.tname ='TEST'  ;
     COLNO CNAME                          PK
---------- ------------------------------ -----
         1 C1                             true
         2 C2                             true
         3 C3                             false
SQL> drop table test purge ;
Table dropped

SQL> 
liulei68211 2016-11-21
  • 打赏
  • 举报
回复
liulei68211 2016-11-21
  • 打赏
  • 举报
回复
大哥 你这个是知道哪个是主键 我想表达的是 在不知道表中是否有主键,哪个字段是主键的前提下 利用系统表查询主键列,是主键的标记‘true’,不是的话显示‘null’ 下面的是SQL serve的
卖水果的net 2016-11-21
  • 打赏
  • 举报
回复

SQL> 
SQL> create table test(c1 int, c2 int, c3 int);
Table created
SQL> alter table test add constraint PK_TEST primary key (c1);
Table altered
SQL> select table_name, column_name
  2  from user_cons_columns where constraint_name = 'PK_TEST';
TABLE_NAME                     COLUMN_NAME
------------------------------ ---------------------------------------------------------
TEST                           C1
SQL> alter table test drop primary key ;
Table altered
SQL> alter table test add constraint PK_TEST primary key (c1,c2);
Table altered
SQL> select table_name, column_name
  2  from user_cons_columns where constraint_name = 'PK_TEST';
TABLE_NAME                     COLUMN_NAME
------------------------------ ------------------------------------------------------
TEST                           C1
TEST                           C2
SQL> drop table test purge ;
Table dropped

SQL> 

17,377

社区成员

发帖
与我相关
我的任务
社区描述
Oracle 基础和管理
社区管理员
  • 基础和管理社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧