sql语句查询数据库中的表名/列名/主键/自动增长值实例
时间:2020-11-18 01:10:27 | 栏目:MsSql | 点击:次
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