查询SQL Server / Oracle数据库表数据 - 标识列

时间:2010-08-14 21:11:41

标签: sql-server oracle identity

我正在尝试查询SQL Server& Oracle数据库,用于获取表示列是否自动增加的表数据。我假设您会查看“information_schema.columns”和“user_tab_cols”表,但它似乎不包含此信息。有谁知道我可以在哪里找到这些信息?

2 个答案:

答案 0 :(得分:2)

检查SQL Server中的特定列:

select t.name as tableName, c.name as columnName, c.is_identity 
    from sys.columns c
        inner join sys.tables t
            on c.object_id = t.object_id
    where t.name = 'YourTable'
        and c.name = 'YourColumn'

或查找所有标识列

select t.name as tableName, c.name as columnName, c.is_identity 
    from sys.columns c
        inner join sys.tables t
            on c.object_id = t.object_id
    where c.is_identity = 1

答案 1 :(得分:1)

在sql server中,这将返回所有具有标识的列

    select OBJECT_NAME(id) as Tablename, name as ColumnName,*
    from syscolumns
   where COLUMNPROPERTY(id, name, 'IsIdentity') = 1

或使用information_schema

select TABLE_SCHEMA + '.' + TABLE_NAME,COLUMN_NAME
 from INFORMATION_SCHEMA.columns
where COLUMNPROPERTY(OBJECT_ID(TABLE_SCHEMA + '.' + TABLE_NAME), 
   COLUMN_NAME, 'IsIdentity') = 1