Find constraint disabled in SQL Server The 2019 Stack Overflow Developer Survey Results Are...

Right tool to dig six foot holes?

What did it mean to "align" a radio?

What is the meaning of Triage in Cybersec world?

Reference request: Oldest number theory books with (unsolved) exercises?

One word riddle: Vowel in the middle

Do these rules for Critical Successes and Critical Failures seem Fair?

Which Sci-Fi work first showed weapon of galactic-scale mass destruction?

How to notate time signature switching consistently every measure

Is a "Democratic" Oligarchy-Style System Possible?

Did Section 31 appear in Star Trek: The Next Generation?

How to save as into a customized destination on macOS?

Worn-tile Scrabble

What do hard-Brexiteers want with respect to the Irish border?

Why can Shazam fly?

Where to refill my bottle in India?

For what reasons would an animal species NOT cross a *horizontal* land bridge?

Are spiders unable to hurt humans, especially very small spiders?

Can one be advised by a professor who is very far away?

Is flight data recorder erased after every flight?

Deal with toxic manager when you can't quit

Why is the maximum length of OpenWrt’s root password 8 characters?

FPGA - DIY Programming

Why isn't airport relocation done gradually?

Does a dangling wire really electrocute me if I'm standing in water?



Find constraint disabled in SQL Server



The 2019 Stack Overflow Developer Survey Results Are InWith MS SQL Server are the generated constraint names predictable?Is there anything a table-level constraint declaration can do that a column-level one can not do?Adding foreign keys constraint on databases usedEnabling Constraint Silently FailsSQL Error: OA-12991 - Unable to Drop a Column after removing all referenced Multi-Column constraintsWhy does SSDT deploy inline FK Constraint as WITH NOCHECK?What is the syntax to add a constraint on an already-created table?Does any kind of constraint in a database have a unique constraint name?Is ALTER TABLE CHECK CONSTRAINT redundant?Can I specify a name for the index which SQL Server creates for my unique constraint?





.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|improve this answer
























                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|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|improve this answer


























                                    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|improve this answer













                                    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|improve this answer












                                    share|improve this answer



                                    share|improve this answer










                                    answered 18 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

                                        ORA-01691 (unable to extend lob segment) even though my tablespace has AUTOEXTEND onORA-01692: unable to...

                                        Always On Availability groups resolving state after failover - Remote harden of transaction...

                                        Circunscripción electoral de Guipúzcoa Referencias Menú de navegaciónLas claves del sistema electoral en...