在数据库中怎么查询出那些表没有设置主键

解决方案 »

  1.   

    create table #t(id int,colid int,datalen bigint)
    exec sp_msforeachtable @command1=N'
    declare @s1 nvarchar(4000),@s2 nvarchar(4000)
    select @s1='''',@s2=''''
    select @s1=@s1+'' union all select ''
    +rtrim(c.id)+'',''
    +rtrim(c.colid)+'',''
    +quotename(c.colid)+'' from #'',
    @s2=@s2+N'',''+quotename(c.colid)+''=max(''+case
    when t.name in(''sql_variant'',''image'',''money'')
    or t.name like ''%text'' 
    or t.name like ''%binary''
    then ''datalength'' else ''len'' end
    +''(''+quotename(c.name)+''))''
    from syscolumns c,systypes t
    where c.xusertype=t.xusertype
    and object_id(''?'')=c.id
    order by colid
    select @s1=stuff(@s1,1,10,''''),
    @s2=stuff(@s2,1,1,'''')
    exec(''select ''+@s2+'' into # from ? insert #t ''+@s1)'
    --,@whereand=' and o.name=''要查询的表'''  --如果只查询指定表,加上此条件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 '√'else '' end,
    主键=case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id 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'),
    已有数据的最大长度=data.datalen,
    小数位数=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
    inner join #t data on a.id=data.id and a.colid=data.colid
    left join systypes b on a.xusertype=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  
    left join sysproperties f on d.id=f.id and f.smallid=0
    --where d.name='要查询的表'    --如果只查询指定表,加上此条件
    order by a.id,a.colorder
    drop table #t
      

  2.   

    --csdn--coolingpipe(冷箫轻笛)提供
    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 '√'else '' end,
        主键       = case when exists(SELECT 1 FROM sysobjects where xtype='PK' and parent_obj=a.id 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.xusertype=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  
    left join 
        sysproperties f 
    on 
        d.id=f.id and f.smallid=0
    --where d.name='要查询的表'    --如果只查询指定表,加上此条件
    order by 
        d.name,a.colorder
      

  3.   

    select * from sysobjects a where xtype='U' and not exists(select 1 from sysobjects where xtype='PK' and parent_obj=a.id )
      

  4.   

    SELECT * FROM SYSOBJECTS A 
    WHERE XTYPE='U' 
       AND NOT EXISTS(SELECT 1 FROM SYSOBJECTS WHERE XTYPE='PK' AND PARENT_OBJ=A.ID )
      

  5.   

    SELECT * FROM SYSOBJECTS A 
    WHERE XTYPE='U' 
       AND NOT EXISTS(SELECT 1 FROM SYSOBJECTS WHERE XTYPE='PK' AND PARENT_OBJ=A.ID )樓上說的這個行。
      

  6.   

    select * from sysobjects a where xtype='U' and not exists(select 1 from sysobjects where xtype='PK' and parent_obj=a.id )