sql语句查询数据库中的表名/列名/主键/自动增长值实例
程序员文章站
2023-12-14 10:16:40
sql语句查询数据库中的表名/列名/主键/自动增长值 ----查询数据库中用户创建的表 ----jsj01 为数据库名 select name tablename from...
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
----查询数据库中用户创建的表
----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