博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
sql语句查询数据库中的表名/列名/主键/自动增长值
阅读量:5154 次
发布时间:2019-06-13

本文共 4267 字,大约阅读时间需要 14 分钟。

----查询数据库中用户创建的表

----jsj01 为数据库名
select name tablename from jsj01..sysobjects where type='U' and name not in ('dtproperties')

--查询表里的字段信息

exec sp_help  对象名

---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表示外键 

----sql2000

select column_name as primarykey,*  from
[jsj01].INFORMATION_SCHEMA.KEY_COLUMN_USAGE
where Table_name='docs' and constraint_name like 'fk_%'

-----sql2005

SELECT @PrimaryKey =  CCU.COLUMN_NAME

FROM     INFORMATION_SCHEMA.TABLE_CONSTRAINTS TC
    INNER JOIN INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE CCU ON TC.CONSTRAINT_NAME = CCU.CONSTRAINT_NAME
WHERE TC.TABLE_NAME = 'docs' AND TC.CONSTRAINT_TYPE='PRIMARY KEY'

 

--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为表名  -- sql2000
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

 

-------- sql2005

select

    a.Name
from
    syscolumns a
left join
    sysobjects b on a.iD=b.parent_obj and b.xtype='PK'
where
    a.ID=object_id('docs') and a.status=0x80

----------------------------------------------------------------------------------------------------------------------------------

 

 

----For mssql2005

SELECT
表名=case when a.colorder=1 then d.name else '' end,
表说明=case when a.colorder=1 then isnull(f.value,'') else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY(a.id,a.name,'IsIdentity')=1 then '1' else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in(
SELECT name FROM sysindexes WHERE indid in(
SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then '1' else '' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then '1' else '' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sys.extended_properties g on a.id=g.major_id and a.colid=g.minor_id
left join sys.extended_properties f on d.id=f.major_id and f.minor_id =0
--where d.name='classinfo'         --如果只查询指定表,加上此条件  
order by a.id,a.colorder

/*

----For mssql2000
SELECT
表名=d.name,--case when a.colorder=1 then d.name else '' end,
字段序号=a.colorder,
字段名=a.name,
标识=case when COLUMNPROPERTY( a.id,a.name,'IsIdentity')=1 then '√'else '' end,
主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and name in (
SELECT name FROM sysindexes WHERE indid in(
   SELECT indid FROM sysindexkeys WHERE id = a.id AND colid=a.colid
))) then '√' else '' end,
类型=b.name,
占用字节数=a.length,
长度=COLUMNPROPERTY(a.id,a.name,'PRECISION'),
小数位数=isnull(COLUMNPROPERTY(a.id,a.name,'Scale'),0),
允许空=case when a.isnullable=1 then '√'else '' end,
默认值=isnull(e.text,''),
字段说明=isnull(g.[value],'')
FROM syscolumns a
left join systypes b on a.xtype=b.xusertype
inner join sysobjects d on a.id=d.id and d.xtype='U' and d.name<>'dtproperties'
left join syscomments e on a.cdefault=e.id
left join sysproperties g on a.id=g.id and a.colid=g.smallid
order by a.id,a.colorder

*/

 

 

 

转载于:https://www.cnblogs.com/pukuimin/archive/2012/06/24/2988117.html

你可能感兴趣的文章
HTTP 错误 403.6 - Forbidden 解决方案
查看>>
一个小例子介绍Obj-C的函数命名方式
查看>>
关于Bootstrap的理解
查看>>
十种基本排序算法
查看>>
hdu 2089 数位dp入门
查看>>
I/O的一些简单操作
查看>>
Handbook之012:函数类别构型
查看>>
php取整函数ceil,floor,round,intval的区别
查看>>
局部富文本
查看>>
例题6-7 树的层次遍历
查看>>
2019-2-15 日记
查看>>
那些年我们跳过的 IE坑
查看>>
产生式模型和判别式模型
查看>>
2015.10.13课堂
查看>>
国内最火5款Java微服务开源项目
查看>>
[国嵌攻略][038][时钟初始化]
查看>>
C#格式化字符串
查看>>
剑指offer——二叉搜索树的后序遍历序列
查看>>
2016集训测试赛(二十四)Problem C: 棋盘控制
查看>>
稳定土厂拌设备控制系统-基本介绍(图)
查看>>