判断指定table的指定列是否是primary,是否是自增标示
大家好,
已知一个表叫table,其中有一个列叫column,我想用sql语句来判断:
1 column是否是主键
2 column是否是自增标识
谢谢~
[解决办法]
是否自增列:
select a.name,a.is_identity from sys.columns a inner join sys.objects b on a.object_id=b.object_id where b.name='table' and a.name='column'
[解决办法]
2. select * from sys.identity_columns where object_id = object_id('Sales.SalesOrderDetail')1. select COLUMN_NAME,* from INFORMATION_SCHEMA.TABLE_CONSTRAINTS A JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE B ON A.CONSTRAINT_NAME = B.CONSTRAINT_NAMEWHERE A.CONSTRAINT_TYPE = 'PRIMARY KEY' AND A.TABLE_NAME = 'SalesOrderDetail'
[解决办法]
是否主键:
select a.name,c.is_primary_key from sys.columns a inner join sys.index_columns b on a.object_id=b.object_id and a.column_id=b.column_idinner join sys.indexes c on a.object_id=c.object_id where a.object_id=object_id('table') and a.name='column'
[解决办法]
自增列:
if columnproperty(object_id('tableName'),'columnName','IsIdentity')=1select '自增列'elseselect '不是自增列'