zoukankan      html  css  js  c++  java
  • SQL语句判断某列是否是主键

    1 SELECT COUNT(1) AS Is_PK
    2 FROM syscolumns
    3 JOIN sysindexkeys
    4 ON syscolumns.id=sysindexkeys.id AND syscolumns.colid=sysindexkeys.colid
    5 JOIN sysindexes
    6 ON syscolumns.id=sysindexes.id AND sysindexkeys.indid=sysindexes.indid
    7 JOIN sysobjects
    8 ON sysindexes.name=sysobjects.name AND sysobjects.xtype='PK'
    9 WHERE syscolumns.name=COLUMN_NAME AND syscolumns.id=object_id(Table_name)



    完整代码:

    View Code
    1 SELECT a.Table_schema as 架构 ,a.Table_name as 表名 ,b.COLUMN_NAME as 栏位名称,b.DATA_TYPE as 数据类型,isnull(b.CHARACTER_MAXIMUM_LENGTH,'') as 长度
    2 ,COLUMNPROPERTY(OBJECT_ID(a.Table_name), b.COLUMN_NAME, 'IsIdentity') as 标识列,
    3 (
    4 select count(1) from syscolumns
    5 join sysindexkeys on syscolumns.id=sysindexkeys.id and syscolumns.colid=sysindexkeys.colid
    6 join sysindexes on syscolumns.id=sysindexes.id and sysindexkeys.indid=sysindexes.indid
    7 join sysobjects on sysindexes.name=sysobjects.name and sysobjects.xtype='PK'
    8 WHERE syscolumns.name=b.COLUMN_NAME AND syscolumns.id=object_id(a.Table_name )
    9 ) as 主键
    10 ,isnull(b.COLUMN_DEFAULT,'') as 预设值
    11 ,b.IS_NULLABLE as 是否允许空值
    12 ,( SELECT value
    13 FROM fn_listextendedproperty (NULL, 'schema', a.Table_schema, 'table', a.TABLE_NAME, 'column', default)
    14 WHERE name='MS_Description' and objtype='COLUMN'and objname Collate Chinese_Taiwan_Stroke_CI_AS = b.COLUMN_NAME
    15 ) as 栏位备注
    16 FROM INFORMATION_SCHEMA.TABLES a
    17 LEFT JOIN INFORMATION_SCHEMA.COLUMNS b
    18 ON a.TABLE_NAME = b.TABLE_NAME
    19 ORDER BY a.TABLE_NAME, b.ORDINAL_POSITION
    20
    21 GO
  • 相关阅读:
    DNS子域授权,区域传送
    DNS BIND配置 配置基本缓存服务器 DNS正向解析 DNS反向解析
    DNS服务概念
    【转】Red5流服务器搭建(实现在线直播,流媒体视频播放和在线视频会议)
    CMD命令
    PHP Mysql数据库连接
    Mysql数据库报错1264
    Linux下nginx的安装
    windows下nginx的安装
    【转载】详解 $_SERVER 函数中QUERY_STRING和REQUEST_URI区别
  • 原文地址:https://www.cnblogs.com/pfs1314/p/1970646.html
Copyright © 2011-2022 走看看