是否有索引查询?

时间:2013-05-22 11:57:10

标签: sybase sybase-ase

我想知道表的索引是什么。我正在使用ASE ISQL,我可以通过右键单击表并选择DDL来了解索引,但是,有一个开发人员在另一台机器上,他不熟悉sybase,我想知道他的数据库的索引(他使用ASE)。

是否有查询?

2 个答案:

答案 0 :(得分:2)

有许多不同的方法可以查看数据库中的索引。

sp_helpindex可用于显示与特定表关联的索引。

sp_helpindex TABLE_NAME

sp_help可用于列出表的所有属性,包括关联的索引。

sp_help TABLE_NAME

要获取所有索引及其关联表的列表,可以使用以下查询

use DATABASE_NAME
go

select sysobjects.name as TABLE_NAME, sysindexes.name as INDEX_NAME
from sysobjects,sysindexes where
sysobjects.type = 'U' and
sysobjects.id = sysindexes.id and
sysindexes.indid between 1 and 254
order by sysobjects.name,sysindexes.indid
go

答案 1 :(得分:2)

此代码将生成代码索引:

use [database]
go
declare @objectName sysname
declare @username sysname
declare @fullname sysname
declare @id int

select
  @id = o.id,
  @objectName = o.name,
  @username = u.name,
  @fullname =  u.name + '.' + o.name
from
  dbo.sysobjects o,
  dbo.sysusers u
where
  o.name = 'Table'
  and o.Type in ( 'U', 'S' )
  and u.uid = o.uid
  and u.name = 'Owner'

select
  [index] = 1000 * i.indid,
  text =
    'create' +
    case i.status & 2
      when 2 then ' unique '
      else ' '
    end +
    case ( i.status & 16 ) + ( i.status2 & 512 )
      when 0 then
        'nonclustered'
      else
        'clustered'
    end +
    ' index [' + i.name + '] on ' + @fullname + ' ('
from
  dbo.sysindexes i
where
  i.indid > 0
  and i.indid < 255
  and i.id = @id
  and i.status2 & 2 != 2

union all

select
  [index] = 1000 * i.indid + c.colid,
  text =
    ' ' + index_col ( @fullname, i.indid, c.colid ) +
  case
    when c.colid < i.keycnt - case when i.indid = 1 then 0 else 1 end then
      ', '
  end
from
  dbo.sysindexes i,
  dbo.syscolumns c
where
  i.indid > 0
  and i.indid < 255
  and i.id = @id
  and i.status2 & 2 != 2

  and c.id = @id
  and c.colid <= i.keycnt - case when i.indid = 1 then 0 else 1 end

union all

select
  [index] = 1000 * i.indid + 999,
  text =
    ' )' || char ( 13 ) || char ( 10 ) || 'go' || char ( 13 ) || char ( 10 )
from
  dbo.sysindexes i
where
  i.indid > 0
  and i.indid < 255
  and i.id = @id
  and i.status2 & 2 != 2

order by
  [index]
go