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;
}
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 18 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