SQL - Check if a column auto increments

后端 未结 4 1705
轻奢々
轻奢々 2020-12-14 02:29

I am trying to run a query to check if a column auto increments. I can check type, default value, if it\'s nullable or not, etc. but I can\'t figure out how to test if it au

相关标签:
4条回答
  • 2020-12-14 02:49

    Run: describe 'table_name'; In column EXTRA is what you looking for

    0 讨论(0)
  • 2020-12-14 02:58

    Assuming MySQL, the EXTRA column will indicate whether it is AUTO_INCREMENT.

    | TABLE_CATALOG | TABLE_SCHEMA | ... |          EXTRA | ... |
    -------------------------------------------------------------
    |           def |   db_2_00314 | ... | auto_increment | ... |

    And for MSSQL, see here.

    0 讨论(0)
  • 2020-12-14 03:04

    For MySql, Check in the EXTRA column:

    SELECT *
    FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = 'my_table'
        AND COLUMN_NAME = 'my_column'
        AND DATA_TYPE = 'int'
        AND COLUMN_DEFAULT IS NULL
        AND IS_NULLABLE = 'NO'
        AND EXTRA like '%auto_increment%'
    

    For Sql Server, use sys.columns and the is_identity column:

    SELECT 
        is_identity
    FROM sys.columns
    WHERE 
        object_id = object_id('my_table')
        AND name = 'my_column'
    
    0 讨论(0)
  • 2020-12-14 03:05

    this works for sql server:

        Select COLUMN_NAME, TABLE_NAME
        from INFORMATION_SCHEMA.COLUMNS
        where TABLE_SCHEMA = 'dbo'
        and COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') = 1
        order by TABLE_NAME
    
    0 讨论(0)
提交回复
热议问题