Query to check index on a table

后端 未结 10 1706
南方客
南方客 2020-12-13 06:06

I need a query to see if a table already has any indexes on it.

相关标签:
10条回答
  • 2020-12-13 06:38

    Simply you can find index name and column names of a particular table using below command

    SP_HELPINDEX 'tablename'

    It work's for me

    0 讨论(0)
  • 2020-12-13 06:39

    If you're using MySQL you can run SHOW KEYS FROM table or SHOW INDEXES FROM table

    0 讨论(0)
  • 2020-12-13 06:39

    Most modern RDBMSs support the INFORMATION_SCHEMA schema. If yours supports that, then you want either INFORMATION_SCHEMA.TABLE_CONSTRAINTS or INFORMATION_SCHEMA.KEY_COLUMN_USAGE, or maybe both.

    To see if yours supports it is as simple as running

    select count(*) from INFORMATION_SCHEMA.TABLE_CONSTRAINTS

    EDIT: SQL Server does have INFORMATION_SCHEMA, and it's easier to use than their vendor-specific tables, so just go with it.

    0 讨论(0)
  • 2020-12-13 06:41

    check this as well This gives an overview of associated constraints across a database. Please also include facilitating where condition with table name of interest so gives information faster.

       select 
    a.TABLE_CATALOG as DB_name,a.TABLE_SCHEMA as tbl_schema, a.TABLE_NAME as tbl_name,a. CONSTRAINT_NAME as constraint_name,b.CONSTRAINT_TYPE
     from INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE a
    join INFORMATION_SCHEMA.TABLE_CONSTRAINTS b on
     a.CONSTRAINT_NAME=b.CONSTRAINT_NAME
    
    0 讨论(0)
提交回复
热议问题