调整属于PK的列的大小。找出列长度

时间:2014-09-09 16:25:49

标签: sql sql-server

我必须调整两个varchar列的大小 - 一个是PK的一部分,另一个是UNIQUE约束。 由于CONSTRAINT,简单的resize *不起作用。所以我想我将不得不放弃约束,改变表格,然后再次添加约束。或者是否有更简单的解决方案?

如果没有,如果仅在列尚未具有正确尺寸时才进行更好的睡眠。如何找出varchar列支持的最大长度?

编辑:
*我尝试使用ALTER TABLE users ALTER COLUMN name VARCHAR(50)将支持的大小更改为50个字符而不是30个字符,这有点短。此操作失败,并显示一条错误消息,指出name是UNIQUE约束UQ_users下的列。

1 个答案:

答案 0 :(得分:1)

这将为您提供整个数据库所需的任何架构信息。您可以根据需要选择它。

SELECT 
    dbo.sysobjects.name AS tblName, dbo.syscolumns.name AS fldName, dbo.systypes.name AS Type, sys.extended_properties.[value] AS Descript, dbo.syscolumns.xprec, dbo.syscolumns.xscale, dbo.syscolumns.length, PK.cnt AS PrmryKey, FK.cnt AS FrnKey, dbo.syscolumns.isnullable, dbo.syscomments.text AS DefaultVal 
FROM dbo.sysobjects 
    INNER JOIN dbo.syscolumns 
        ON dbo.sysobjects.id = dbo.syscolumns.id 
    INNER JOIN dbo.systypes 
        ON dbo.syscolumns.xtype = dbo.systypes.xusertype 
    LEFT OUTER JOIN dbo.syscomments 
        ON dbo.syscolumns.cdefault = dbo.syscomments.id 
    LEFT OUTER JOIN sys.extended_properties 
        ON dbo.syscolumns.id = sys.extended_properties.major_id 
            AND dbo.syscolumns.colid = sys.extended_properties.minor_id 
    LEFT OUTER JOIN (
            SELECT 1 as CNT, ic.OBJECT_ID,ic.column_id 
            FROM sys.indexes AS i 
                INNER JOIN sys.index_columns AS ic 
                    ON i.OBJECT_ID = ic.OBJECT_ID 
                        AND i.index_id = ic.index_id 
            WHERE i.is_primary_key = 1 ) PK 
        on dbo.syscolumns.id = PK.object_id 
            AND dbo.syscolumns.colid = PK.column_id 
    LEFT OUTER JOIN (
            SELECT fkeyid, fkey, COUNT(*) AS cnt 
            FROM sysforeignkeys 
            GROUP BY fkeyid, fkey) FK 
        ON dbo.syscolumns.id = FK.fkeyid 
            AND dbo.syscolumns.colid = FK.fkey  
WHERE  (dbo.sysobjects.xtype = 'U' OR dbo.sysobjects.xtype = 'V')
    AND (dbo.sysobjects.name <> N'dtproperties') 
    AND (dbo.sysobjects.name <> N'sysconstraints') 
    AND (dbo.sysobjects.name <> N'syssegments') 
--ORDER BY dbo.sysobjects.name, dbo.syscolumns.name