加入收藏 | 设为首页 | 会员中心 | 我要投稿 南京站长网 (https://www.025zz.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > MySql教程 > 正文

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

发布时间:2022-10-20 14:32:24 所属栏目:MySql教程 来源:转载
导读: sql语句查询数据库中的表名/列名/主键/自动增长值
----查询数据库中用户创建的表
----jsj01 为数据库名
select name tablename from jsj01..sysobjects where type='U' and name not in ('

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

(编辑:南京站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章