How do I find a default constraint using INFORMATION_SCHEMA?

前端 未结 14 717
猫巷女王i
猫巷女王i 2020-12-04 13:40

I\'m trying to test if a given default constraint exists. I don\'t want to use the sysobjects table, but the more standard INFORMATION_SCHEMA.

I\'ve used this to che

相关标签:
14条回答
  • 2020-12-04 14:26

    Object Catalog View : sys.default_constraints

    The information schema views INFORMATION_SCHEMA are ANSI-compliant, but the default constraints aren't a part of ISO standard. Microsoft SQL Server provides system catalog views for getting information about SQL Server object metadata.

    sys.default_constraints system catalog view used to getting the information about default constraints.

    SELECT so.object_id TableName,
           ss.name AS TableSchema,
           cc.name AS Name,
           cc.object_id AS ObjectID,              
           sc.name AS ColumnName,
           cc.parent_column_id AS ColumnID,
           cc.definition AS Defination,
           CONVERT(BIT,
                   CASE cc.is_system_named
                       WHEN 1
                       THEN 1
                       ELSE 0
                   END) AS IsSystemNamed,
           cc.create_date AS CreationDate,
           cc.modify_date AS LastModifiednDate
    FROM sys.default_constraints cc WITH (NOLOCK)
         INNER JOIN sys.objects so WITH (NOLOCK) ON so.object_id = cc.parent_object_id
         LEFT JOIN sys.schemas ss WITH (NOLOCK) ON ss.schema_id = so.schema_id
         LEFT JOIN sys.columns sc WITH (NOLOCK) ON sc.column_id = cc.parent_column_id
                                                   AND sc.object_id = cc.parent_object_id
    ORDER BY so.name,
             cc.name;
    
    0 讨论(0)
  • 2020-12-04 14:28

    Probably because on some of the other SQL DBMSs the "default constraint" is not really a constraint, you'll not find its name in "INFORMATION_SCHEMA.TABLE_CONSTRAINTS", so your best bet is "INFORMATION_SCHEMA.COLUMNS" as others have mentioned already.

    (SQLServer-ignoramus here)

    The only a reason I can think of when you have to know the "default constraint"'s name is if SQLServer doesn't support "ALTER TABLE xxx ALTER COLUMN yyy SET DEFAULT..." command. But then you are already in a non-standard zone and you have to use the product-specific ways to get what you need.

    0 讨论(0)
  • 2020-12-04 14:29

    Is the COLUMN_DEFAULT column of INFORMATION_SCHEMA.COLUMNS what you are looking for?

    0 讨论(0)
  • 2020-12-04 14:30

    The script below lists all the default constraints and the default values for the user tables in the database in which it is being run:

    SELECT  
            b.name AS TABLE_NAME,
            d.name AS COLUMN_NAME,
            a.name AS CONSTRAINT_NAME,
            c.text AS DEFAULT_VALUE
    FROM sys.sysobjects a INNER JOIN
            (SELECT name, id
             FROM sys.sysobjects 
             WHERE xtype = 'U') b on (a.parent_obj = b.id)
                          INNER JOIN sys.syscomments c ON (a.id = c.id)
                          INNER JOIN sys.syscolumns d ON (d.cdefault = a.id)                                          
     WHERE a.xtype = 'D'        
     ORDER BY b.name, a.name
    
    0 讨论(0)
  • 2020-12-04 14:33
    select c.name, col.name from sys.default_constraints c
        inner join sys.columns col on col.default_object_id = c.object_id
        inner join sys.objects o  on o.object_id = c.parent_object_id
        inner join sys.schemas s on s.schema_id = o.schema_id
    where s.name = @SchemaName and o.name = @TableName and col.name = @ColumnName
    
    0 讨论(0)
  • 2020-12-04 14:34

    Necromancing.
    If you only need to check if a default-constraint exists
    (default-constraint(s) may have different name in poorly-managed DBs),
    use INFORMATION_SCHEMA.COLUMNS (column_default):

    IF NOT EXISTS(
        SELECT * FROM INFORMATION_SCHEMA.COLUMNS
        WHERE (1=1) 
        AND TABLE_SCHEMA = 'dbo' 
        AND TABLE_NAME = 'T_VWS_PdfBibliothek' 
        AND COLUMN_NAME = 'PB_Text'
        AND COLUMN_DEFAULT IS NOT NULL  
    )
    BEGIN 
        EXECUTE('ALTER TABLE dbo.T_VWS_PdfBibliothek 
                    ADD CONSTRAINT DF_T_VWS_PdfBibliothek_PB_Text DEFAULT (N''image'') FOR PB_Text; 
        '); 
    END 
    

    If you want to check by the constraint-name only:

    -- Alternative way: 
    IF OBJECT_ID('DF_CONSTRAINT_NAME', 'D') IS NOT NULL 
    BEGIN
        -- constraint exists, deal with it.
    END 
    

    And last but not least, you can just create a view called
    INFORMATION_SCHEMA.DEFAULT_CONSTRAINTS:

    CREATE VIEW INFORMATION_SCHEMA.DEFAULT_CONSTRAINTS 
    AS 
    SELECT 
         DB_NAME() AS CONSTRAINT_CATALOG 
        ,csch.name AS CONSTRAINT_SCHEMA
        ,dc.name AS CONSTRAINT_NAME 
        ,DB_NAME() AS TABLE_CATALOG 
        ,sch.name AS TABLE_SCHEMA 
        ,syst.name AS TABLE_NAME 
        ,sysc.name AS COLUMN_NAME 
        ,COLUMNPROPERTY(sysc.object_id, sysc.name, 'ordinal') AS ORDINAL_POSITION 
        ,dc.type_desc AS CONSTRAINT_TYPE 
        ,dc.definition AS COLUMN_DEFAULT 
    
        -- ,dc.create_date 
        -- ,dc.modify_date 
    FROM sys.columns AS sysc -- 46918 / 3892 with inner joins + where 
    -- FROM sys.all_columns AS sysc -- 55429 / 3892 with inner joins + where 
    
    INNER JOIN sys.tables AS syst 
        ON syst.object_id = sysc.object_id 
    
    INNER JOIN sys.schemas AS sch
        ON sch.schema_id = syst.schema_id 
    
    INNER JOIN sys.default_constraints AS dc 
        ON sysc.default_object_id = dc.object_id
    
    INNER JOIN sys.schemas AS csch
        ON csch.schema_id = dc.schema_id 
    
    WHERE (1=1) 
    AND dc.is_ms_shipped = 0 
    
    /*
    WHERE (1=1) 
    AND sch.name = 'dbo'
    AND syst.name = 'tablename'
    AND sysc.name = 'columnname'
    */
    
    0 讨论(0)
提交回复
热议问题