SQL query to get primary keys for all tables in sybase ase 15.x along with column names

[亡魂溺海] 提交于 2020-01-06 23:43:07

问题


I'm using Sybase ASE 15.5 and a stranger to this database. Straight to the point--> I'm looking for a sql query that would help me get the primary keys for all tables in sybase along with the column names on which the primary key is declared. For example, if I have the following tables, organization having primary key PK_org_id on the column org_id org_alias having primary key PK_alias_id on the column alias_id org_temp having primary key PK_org_temp_id on the columns (org_id,org_name)

then the query should return me with:

  • Table_Name PK_Name Column_name
  • Organization PK_org_id org_id
  • Org_alias PK_alias_id alias_id
  • Org_temp PK_org_temp_id org_id,org_name

I've tried the below query:

select  o.name , i.name
from   sysobjects  o,   sysindexes   i   
where o.id=i.id  
and i.indid = 1  
and o.type = 'U'

but it only returns me the table name with its primary key. I want to have the column name too.

Please help!


回答1:


Use the built_in function index_col(object_name, indexid, N [,owner_id]). This lets you retrieve the Nth column of a particular index. Call it multiple times with different values for N, for example by joining it with master..spt_values where type = 'P' and supplying the number column as N.




回答2:


If you don't want to code your own query, then look at the catalog procedures that are included with ASE. 'sp_pkeys' should give you what you want.



来源:https://stackoverflow.com/questions/27967142/sql-query-to-get-primary-keys-for-all-tables-in-sybase-ase-15-x-along-with-colum

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!