Find constraint disabled in SQL Server
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom: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
sql-server sql-server-2008 constraint alter-table ddl
add a comment |
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
add a comment |
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
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
sql-server sql-server-2008 constraint alter-table ddl
edited Jan 15 '18 at 14:28
Aaron Bertrand♦
154k18298493
154k18298493
asked Jun 8 '15 at 19:38
Diego FloresDiego Flores
1471313
1471313
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Use the system views for this:
select * from sys.check_constraints
where is_disabled = 1;
add a comment |
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;
add a comment |
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];
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Use the system views for this:
select * from sys.check_constraints
where is_disabled = 1;
add a comment |
Use the system views for this:
select * from sys.check_constraints
where is_disabled = 1;
add a comment |
Use the system views for this:
select * from sys.check_constraints
where is_disabled = 1;
Use the system views for this:
select * from sys.check_constraints
where is_disabled = 1;
answered Jun 8 '15 at 19:51
Mike FalMike Fal
10.5k13353
10.5k13353
add a comment |
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
edited Jun 8 '15 at 20:11
answered Jun 8 '15 at 19:51
Aaron Bertrand♦Aaron Bertrand
154k18298493
154k18298493
add a comment |
add a comment |
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];
add a comment |
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];
add a comment |
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];
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];
answered 8 mins ago
Michael K CampbellMichael K Campbell
972
972
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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