Find constraint disabled in SQL Server





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}







0















I'm doing an audit of constraints in SQL Server.



How to find all constraints that are not enabled?



This, after using the next declaration:



alter table mytable nocheck constraint all









share|improve this question































    0















    I'm doing an audit of constraints in SQL Server.



    How to find all constraints that are not enabled?



    This, after using the next declaration:



    alter table mytable nocheck constraint all









    share|improve this question



























      0












      0








      0








      I'm doing an audit of constraints in SQL Server.



      How to find all constraints that are not enabled?



      This, after using the next declaration:



      alter table mytable nocheck constraint all









      share|improve this question
















      I'm doing an audit of constraints in SQL Server.



      How to find all constraints that are not enabled?



      This, after using the next declaration:



      alter table mytable nocheck constraint all






      sql-server sql-server-2008 constraint alter-table ddl






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 15 '18 at 14:28









      Aaron Bertrand

      154k18298493




      154k18298493










      asked Jun 8 '15 at 19:38









      Diego FloresDiego Flores

      1471313




      1471313






















          3 Answers
          3






          active

          oldest

          votes


















          3














          Use the system views for this:



          select * from sys.check_constraints
          where is_disabled = 1;





          share|improve this answer































            3














            SELECT 
            [object] = QUOTENAME(s.name) + N'.' + QUOTENAME(t.name),
            [disabled_constraint] = c.name
            FROM sys.tables AS t
            INNER JOIN sys.schemas AS s
            ON t.schema_id = s.schema_id
            INNER JOIN sys.check_constraints AS c
            ON t.object_id = c.parent_object_id
            WHERE c.is_disabled = 1;





            share|improve this answer

































              0














              Mike and Aaron are both on the right track - but the question asks for ALL constraints that are disabled - not just CHECK constraints (i.e., FKs can be disabled too).



              Moreover, FKs can also be UNTRUSTED - which will yield the potential for not just bad/invalid DATA but can/will also lead to perf issues (as SQL Server can't/won't trust these FKs when creating execution plans) as per:
              https://sqlserverfast.com/blog/hugo/2007/03/can-you-trust-your-constraints/



              As such, I tend to use the following:



                  WITH constraints AS ( 
              SELECT
              QUOTENAME(SCHEMA_NAME(t.[schema_id])) + N'.' + QUOTENAME(t.[name]) [table] ,
              c.[type_desc] [constraint_type],
              c.[name] [constraint_name]
              FROM
              sys.tables AS t
              INNER JOIN sys.check_constraints AS c ON t.[object_id] = c.parent_object_id
              WHERE
              c.is_disabled = 1

              UNION

              SELECT
              QUOTENAME(SCHEMA_NAME([schema_id])) + N'.' + QUOTENAME(OBJECT_NAME([parent_object_id])) [table],
              CASE WHEN is_disabled = 1 THEN 'FOREIGN_KEY (DISABLED)' ELSE 'FOREIGN_KEY (UNTRUSTED)' END [constraint_type],
              [name] [constraint_name]
              FROM
              sys.[foreign_keys]
              WHERE
              [is_disabled] = 1 OR [is_not_trusted] = 1
              )

              SELECT
              [table],
              [constraint_type],
              [constraint_name]
              FROM
              constraints
              ORDER BY
              [constraint_type], [table];




              share
























                Your Answer








                StackExchange.ready(function() {
                var channelOptions = {
                tags: "".split(" "),
                id: "182"
                };
                initTagRenderer("".split(" "), "".split(" "), channelOptions);

                StackExchange.using("externalEditor", function() {
                // Have to fire editor after snippets, if snippets enabled
                if (StackExchange.settings.snippets.snippetsEnabled) {
                StackExchange.using("snippets", function() {
                createEditor();
                });
                }
                else {
                createEditor();
                }
                });

                function createEditor() {
                StackExchange.prepareEditor({
                heartbeatType: 'answer',
                autoActivateHeartbeat: false,
                convertImagesToLinks: false,
                noModals: true,
                showLowRepImageUploadWarning: true,
                reputationToPostImages: null,
                bindNavPrevention: true,
                postfix: "",
                imageUploader: {
                brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
                contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
                allowUrls: true
                },
                onDemand: true,
                discardSelector: ".discard-answer"
                ,immediatelyShowMarkdownHelp:true
                });


                }
                });














                draft saved

                draft discarded


















                StackExchange.ready(
                function () {
                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f103528%2ffind-constraint-disabled-in-sql-server%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                3 Answers
                3






                active

                oldest

                votes








                3 Answers
                3






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                3














                Use the system views for this:



                select * from sys.check_constraints
                where is_disabled = 1;





                share|improve this answer




























                  3














                  Use the system views for this:



                  select * from sys.check_constraints
                  where is_disabled = 1;





                  share|improve this answer


























                    3












                    3








                    3







                    Use the system views for this:



                    select * from sys.check_constraints
                    where is_disabled = 1;





                    share|improve this answer













                    Use the system views for this:



                    select * from sys.check_constraints
                    where is_disabled = 1;






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 8 '15 at 19:51









                    Mike FalMike Fal

                    10.5k13353




                    10.5k13353

























                        3














                        SELECT 
                        [object] = QUOTENAME(s.name) + N'.' + QUOTENAME(t.name),
                        [disabled_constraint] = c.name
                        FROM sys.tables AS t
                        INNER JOIN sys.schemas AS s
                        ON t.schema_id = s.schema_id
                        INNER JOIN sys.check_constraints AS c
                        ON t.object_id = c.parent_object_id
                        WHERE c.is_disabled = 1;





                        share|improve this answer






























                          3














                          SELECT 
                          [object] = QUOTENAME(s.name) + N'.' + QUOTENAME(t.name),
                          [disabled_constraint] = c.name
                          FROM sys.tables AS t
                          INNER JOIN sys.schemas AS s
                          ON t.schema_id = s.schema_id
                          INNER JOIN sys.check_constraints AS c
                          ON t.object_id = c.parent_object_id
                          WHERE c.is_disabled = 1;





                          share|improve this answer




























                            3












                            3








                            3







                            SELECT 
                            [object] = QUOTENAME(s.name) + N'.' + QUOTENAME(t.name),
                            [disabled_constraint] = c.name
                            FROM sys.tables AS t
                            INNER JOIN sys.schemas AS s
                            ON t.schema_id = s.schema_id
                            INNER JOIN sys.check_constraints AS c
                            ON t.object_id = c.parent_object_id
                            WHERE c.is_disabled = 1;





                            share|improve this answer















                            SELECT 
                            [object] = QUOTENAME(s.name) + N'.' + QUOTENAME(t.name),
                            [disabled_constraint] = c.name
                            FROM sys.tables AS t
                            INNER JOIN sys.schemas AS s
                            ON t.schema_id = s.schema_id
                            INNER JOIN sys.check_constraints AS c
                            ON t.object_id = c.parent_object_id
                            WHERE c.is_disabled = 1;






                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jun 8 '15 at 20:11

























                            answered Jun 8 '15 at 19:51









                            Aaron BertrandAaron Bertrand

                            154k18298493




                            154k18298493























                                0














                                Mike and Aaron are both on the right track - but the question asks for ALL constraints that are disabled - not just CHECK constraints (i.e., FKs can be disabled too).



                                Moreover, FKs can also be UNTRUSTED - which will yield the potential for not just bad/invalid DATA but can/will also lead to perf issues (as SQL Server can't/won't trust these FKs when creating execution plans) as per:
                                https://sqlserverfast.com/blog/hugo/2007/03/can-you-trust-your-constraints/



                                As such, I tend to use the following:



                                    WITH constraints AS ( 
                                SELECT
                                QUOTENAME(SCHEMA_NAME(t.[schema_id])) + N'.' + QUOTENAME(t.[name]) [table] ,
                                c.[type_desc] [constraint_type],
                                c.[name] [constraint_name]
                                FROM
                                sys.tables AS t
                                INNER JOIN sys.check_constraints AS c ON t.[object_id] = c.parent_object_id
                                WHERE
                                c.is_disabled = 1

                                UNION

                                SELECT
                                QUOTENAME(SCHEMA_NAME([schema_id])) + N'.' + QUOTENAME(OBJECT_NAME([parent_object_id])) [table],
                                CASE WHEN is_disabled = 1 THEN 'FOREIGN_KEY (DISABLED)' ELSE 'FOREIGN_KEY (UNTRUSTED)' END [constraint_type],
                                [name] [constraint_name]
                                FROM
                                sys.[foreign_keys]
                                WHERE
                                [is_disabled] = 1 OR [is_not_trusted] = 1
                                )

                                SELECT
                                [table],
                                [constraint_type],
                                [constraint_name]
                                FROM
                                constraints
                                ORDER BY
                                [constraint_type], [table];




                                share




























                                  0














                                  Mike and Aaron are both on the right track - but the question asks for ALL constraints that are disabled - not just CHECK constraints (i.e., FKs can be disabled too).



                                  Moreover, FKs can also be UNTRUSTED - which will yield the potential for not just bad/invalid DATA but can/will also lead to perf issues (as SQL Server can't/won't trust these FKs when creating execution plans) as per:
                                  https://sqlserverfast.com/blog/hugo/2007/03/can-you-trust-your-constraints/



                                  As such, I tend to use the following:



                                      WITH constraints AS ( 
                                  SELECT
                                  QUOTENAME(SCHEMA_NAME(t.[schema_id])) + N'.' + QUOTENAME(t.[name]) [table] ,
                                  c.[type_desc] [constraint_type],
                                  c.[name] [constraint_name]
                                  FROM
                                  sys.tables AS t
                                  INNER JOIN sys.check_constraints AS c ON t.[object_id] = c.parent_object_id
                                  WHERE
                                  c.is_disabled = 1

                                  UNION

                                  SELECT
                                  QUOTENAME(SCHEMA_NAME([schema_id])) + N'.' + QUOTENAME(OBJECT_NAME([parent_object_id])) [table],
                                  CASE WHEN is_disabled = 1 THEN 'FOREIGN_KEY (DISABLED)' ELSE 'FOREIGN_KEY (UNTRUSTED)' END [constraint_type],
                                  [name] [constraint_name]
                                  FROM
                                  sys.[foreign_keys]
                                  WHERE
                                  [is_disabled] = 1 OR [is_not_trusted] = 1
                                  )

                                  SELECT
                                  [table],
                                  [constraint_type],
                                  [constraint_name]
                                  FROM
                                  constraints
                                  ORDER BY
                                  [constraint_type], [table];




                                  share


























                                    0












                                    0








                                    0







                                    Mike and Aaron are both on the right track - but the question asks for ALL constraints that are disabled - not just CHECK constraints (i.e., FKs can be disabled too).



                                    Moreover, FKs can also be UNTRUSTED - which will yield the potential for not just bad/invalid DATA but can/will also lead to perf issues (as SQL Server can't/won't trust these FKs when creating execution plans) as per:
                                    https://sqlserverfast.com/blog/hugo/2007/03/can-you-trust-your-constraints/



                                    As such, I tend to use the following:



                                        WITH constraints AS ( 
                                    SELECT
                                    QUOTENAME(SCHEMA_NAME(t.[schema_id])) + N'.' + QUOTENAME(t.[name]) [table] ,
                                    c.[type_desc] [constraint_type],
                                    c.[name] [constraint_name]
                                    FROM
                                    sys.tables AS t
                                    INNER JOIN sys.check_constraints AS c ON t.[object_id] = c.parent_object_id
                                    WHERE
                                    c.is_disabled = 1

                                    UNION

                                    SELECT
                                    QUOTENAME(SCHEMA_NAME([schema_id])) + N'.' + QUOTENAME(OBJECT_NAME([parent_object_id])) [table],
                                    CASE WHEN is_disabled = 1 THEN 'FOREIGN_KEY (DISABLED)' ELSE 'FOREIGN_KEY (UNTRUSTED)' END [constraint_type],
                                    [name] [constraint_name]
                                    FROM
                                    sys.[foreign_keys]
                                    WHERE
                                    [is_disabled] = 1 OR [is_not_trusted] = 1
                                    )

                                    SELECT
                                    [table],
                                    [constraint_type],
                                    [constraint_name]
                                    FROM
                                    constraints
                                    ORDER BY
                                    [constraint_type], [table];




                                    share













                                    Mike and Aaron are both on the right track - but the question asks for ALL constraints that are disabled - not just CHECK constraints (i.e., FKs can be disabled too).



                                    Moreover, FKs can also be UNTRUSTED - which will yield the potential for not just bad/invalid DATA but can/will also lead to perf issues (as SQL Server can't/won't trust these FKs when creating execution plans) as per:
                                    https://sqlserverfast.com/blog/hugo/2007/03/can-you-trust-your-constraints/



                                    As such, I tend to use the following:



                                        WITH constraints AS ( 
                                    SELECT
                                    QUOTENAME(SCHEMA_NAME(t.[schema_id])) + N'.' + QUOTENAME(t.[name]) [table] ,
                                    c.[type_desc] [constraint_type],
                                    c.[name] [constraint_name]
                                    FROM
                                    sys.tables AS t
                                    INNER JOIN sys.check_constraints AS c ON t.[object_id] = c.parent_object_id
                                    WHERE
                                    c.is_disabled = 1

                                    UNION

                                    SELECT
                                    QUOTENAME(SCHEMA_NAME([schema_id])) + N'.' + QUOTENAME(OBJECT_NAME([parent_object_id])) [table],
                                    CASE WHEN is_disabled = 1 THEN 'FOREIGN_KEY (DISABLED)' ELSE 'FOREIGN_KEY (UNTRUSTED)' END [constraint_type],
                                    [name] [constraint_name]
                                    FROM
                                    sys.[foreign_keys]
                                    WHERE
                                    [is_disabled] = 1 OR [is_not_trusted] = 1
                                    )

                                    SELECT
                                    [table],
                                    [constraint_type],
                                    [constraint_name]
                                    FROM
                                    constraints
                                    ORDER BY
                                    [constraint_type], [table];





                                    share











                                    share


                                    share










                                    answered 8 mins ago









                                    Michael K CampbellMichael K Campbell

                                    972




                                    972






























                                        draft saved

                                        draft discarded




















































                                        Thanks for contributing an answer to Database Administrators Stack Exchange!


                                        • Please be sure to answer the question. Provide details and share your research!

                                        But avoid



                                        • Asking for help, clarification, or responding to other answers.

                                        • Making statements based on opinion; back them up with references or personal experience.


                                        To learn more, see our tips on writing great answers.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdba.stackexchange.com%2fquestions%2f103528%2ffind-constraint-disabled-in-sql-server%23new-answer', 'question_page');
                                        }
                                        );

                                        Post as a guest















                                        Required, but never shown





















































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown

































                                        Required, but never shown














                                        Required, but never shown












                                        Required, but never shown







                                        Required, but never shown







                                        Popular posts from this blog

                                        Liste der Baudenkmale in Friedland (Mecklenburg)

                                        Single-Malt-Whisky

                                        Czorneboh