sql語句查詢數據庫中的表名/列名/主鍵/自動增長值
----查詢數據庫中用戶創建的表
----jsj01 為數據庫名
select name tablename from jsj01..sysobjects where type='U' and name not in ('dtproperties')
--查詢表裡的字段信息
---docs為表名
---- select * from syscolumns where id = object_id('docs')
----查詢數據庫中所有類型
----select name,xtype from systypes
----兩表聯查,顯示表中所有字段和對應的數據類型
----syscolumns裡字段‘xtype' 對應 systypes裡的 ‘xusertype' ,systypes 裡的‘name'字段就是字段的數據類型
----docs 為表名
select a.name as fieldname,b.name as type from
syscolumns as a
join systypes as b
on a.xtype = b.xusertype
where id=object_id('docs')
----docs為數據表名 : 查詢表字段、類型、說明
select a.name fieldname,b.name type,c.value comment from
syscolumns as a
full join systypes as b
on a.xtype = b.xusertype
full join ::fn_listextendedproperty(NULL, 'user', 'dbo', 'table', 'docs', 'column', default) as c ----這是2000版本,2005把user改為schema
on a.name=c.objname COLLATE Chinese_PRC_CI_AS -----排序規則(有時不加也可以,如果兩表的排序規則不同,則會報錯)
--join sysproperties c
--on a.id=c.major_id
where id=object_id('docs')
----查詢表裡的主鍵,沒有主鍵為空,如果是多個組合主鍵就有多個值 pk為主鍵 fk為外鍵
--- jsj01 為數據庫名 docs為表名 fk表示外鍵
select column_name as primarykey,* from
[jsj01].INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where Table_name='docs' and constraint_name like 'fk_%'
--select * from sysobjects WHERE OBJECT_NAME(sysobjects.parent_obj)='docs' --and xtype='pk'
--select * from sysconstraints where id = object_id('docs')
--select * from syscolumns where id = object_id('docs')
--select * from sysindexes
--select * from sysindexkeys
----查詢表中自動增長的字段,沒有為空,如果有就只有一個
----docs為表名
SELECT a.name column_name,b.name data_type
FROM syscolumns a,systypes b
WHERE a.id=object_id('docs') and a.xtype = b.xusertype
AND a.autoval is not null
作者 pukuimin1226