Discover primary / unique keys in Sybase ASE

2020-04-24 17:16发布

问题:

In Sybase ASE, I would like to discover all primary and unique keys. I want to do something similar to what is explained in this answer:

Identifying Sybase tables, fields, keys, constraints

But unfortunately, this doesn't work for me. Somehow the syskeys table does return any rows for my own keys, only for system table keys.

What may I be doing wrong? Some missing grants? I have installed Sybase ASE 15.5 and I'm connecting with user dbo, login sa

回答1:

When you explicitly declare a key field - say in a CREATE TABLE statement - this doesn't populate the syskeys table. You would use (e.g.) sp_primarykeys to do that. This allows you to 'register' in the syskeys tables the primary, foreign keys, etc. that you would like to be discoverable by an application. You can declare a primary key in this way, even if there is no actual constraint enforced on the field in question.

The stored procedure sp_helpconstraint can be used to find all the keys (etc.) that are defined for a table.

To query for all tables in a database you'd need to use the sysindexes table and look at, e.g. the status field.