我正在尝试运行查询以检查列是否自动递增。我可以检查类型,默认值,它是否可以为空等等,但我无法弄清楚如何自动递增测试。以下是我测试其他内容的方法:
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 AUTO_INCREMENTS = 'YES'
很遗憾,没有AUTO_INCREMENTS
列可供比较。那么如何测试列自动增量?
答案 0 :(得分:43)
对于MySql,请检入EXTRA
列:
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%'
对于Sql Server,请使用sys.columns
和is_identity
列:
SELECT
is_identity
FROM sys.columns
WHERE
object_id = object_id('my_table')
AND name = 'my_column'
答案 1 :(得分:8)
假设MySQL,EXTRA
列将指示它是否为AUTO_INCREMENT
。
| TABLE_CATALOG | TABLE_SCHEMA | ... | EXTRA | ... | ------------------------------------------------------------- | def | db_2_00314 | ... | auto_increment | ... |
对于MSSQL,see here。
答案 2 :(得分:3)
运行:describe'table_name';在EXTRA栏中您正在寻找
答案 3 :(得分:1)
这适用于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