sql语句查询数据库中的表名/列名/主键/自动增长值实例

前端之家收集整理的这篇文章主要介绍了sql语句查询数据库中的表名/列名/主键/自动增长值实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

sql语句查询数据库中的表名/列名/主键/自动增长值 ----查询数据库用户创建的表 ----jsj01 为数据库名 select name tablename from jsj01..sysobjects where type='U' and name not in ('dtproperties') --查询表里的字段信息 ---docs为表名 @H_404_0@---- select from syscolumns where id = object_id('docs') ----查询数据库中所有类型 @H_404_0@----select name,xtype from systypes ----两表联查,显示表中所有字段和对应的数据类型 ----syscolumns里字段‘xtype' 对应 systypes里的 ‘xusertype',systypes 里的‘name'字段就是字段的数据类型 @H_4040@----docs 为表名 @H4040@select a.name as fieldname,b.name as type from @H4040@syscolumns as a @H4040@join systypes as b @H4040@on a.xtype = b.xusertype @H404_0@where id=object_id('docs') ----docs为数据表名 : 查询表字段、类型、说明 select a.name fieldname,b.name type,c.value comment from @H_4040@syscolumns as a @H4040@full join systypes as b @H4040@on a.xtype = b.xusertype @H404_0@full join ::fnlistextendedproperty(NULL,'user','dbo','table','docs','column',default) as c ----这是2000版本,2005把user改为schema @H404_0@on a.name=c.objname COLLATE Chinese_PRC_CIAS -----排序规则(有时不加也可以,如果两表的排序规则不同,则会报错) @H4040@--join sysproperties c @H404_0@--on a.id=c.majorid @H404_0@where id=object_id('docs') ----查询表里的主键,没有主键为空,如果是多个组合主键就有多个值 pk为主键 fk为外键 @H_404_0@--- jsj01 为数据库名 docs为表名 fk表示外键 select column_name as primarykey, from @H_404_0@[jsj01].INFORMATION_SCHEMA.KEY_COLUMNUSAGE @H404_0@where Table_name='docs' and constraintname like 'fk%' --select from sysobjects WHERE OBJECT_NAME(sysobjects.parentobj)='docs' --and xtype='pk' @H404_0@--select from sysconstraints where id = objectid('docs') @H404_0@--select from syscolumns where id = objectid('docs') @H404_0@--select from sysindexes @H_404_0@--select * from sysindexkeys ----查询表中自动增长的字段,没有为空,如果有就只有一个 @H_404_0@----docs为表名 SELECT a.name column_name,b.name datatype @H4040@FROM syscolumns a,systypes b @H404_0@WHERE a.id=objectid('docs') and a.xtype = b.xusertype @H4040@AND a.autoval is not null @H404_0@作者 pukuimin1226

猜你在找的MsSQL相关文章