dbTalk Databases Forums  

Check if DB Constraints exist on a table

microsoft.public.sqlserver.programming microsoft.public.sqlserver.programming


Discuss Check if DB Constraints exist on a table in the microsoft.public.sqlserver.programming forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
SQL_Learner
 
Posts: n/a

Default Check if DB Constraints exist on a table - 03-06-2006 , 01:20 PM






If we alter the table by disabling the constraints (use NOCHECK CONSTRAINT
ALL). Is there any command to verify that the constraints are disabled or
don't exist.

Reply With Quote
  #2  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Check if DB Constraints exist on a table - 03-06-2006 , 01:29 PM






Check out sp_helpconstraint in the BOL.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"SQL_Learner" <SQL_Learner (AT) discussions (DOT) microsoft.com> wrote

If we alter the table by disabling the constraints (use NOCHECK CONSTRAINT
ALL). Is there any command to verify that the constraints are disabled or
don't exist.


Reply With Quote
  #3  
Old   
SQL_Learner
 
Posts: n/a

Default Re: Check if DB Constraints exist on a table - 03-06-2006 , 02:20 PM



Thanks Tom, this command is what I am looking for.

In SQL server, without checking if we enable all constraints (irrespective
of they are enabled or disabled at that point) are the any issues? What are
your thoughts on this.

Thanks in Advance

"Tom Moreau" wrote:

Quote:
Check out sp_helpconstraint in the BOL.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"SQL_Learner" <SQL_Learner (AT) discussions (DOT) microsoft.com> wrote in message
news:E615AA31-2B87-4B85-BC6B-6CC01BF0D8C8 (AT) microsoft (DOT) com...
If we alter the table by disabling the constraints (use NOCHECK CONSTRAINT
ALL). Is there any command to verify that the constraints are disabled or
don't exist.



Reply With Quote
  #4  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Check if DB Constraints exist on a table - 03-06-2006 , 02:35 PM



That depends. If you attempt to enable the constraints WITH CHECK, and
there are existing violations of those constraints, then re-enabling will
fail. However, if you re-enable WITH NOCHECK, then it will succeed.

That said, if you have a partitioned view, then you'd want to use WITH
CHECK, so as to take advantage of the performance benefits that having such
constraints will give you.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"SQL_Learner" <SQLLearner (AT) discussions (DOT) microsoft.com> wrote

Thanks Tom, this command is what I am looking for.

In SQL server, without checking if we enable all constraints (irrespective
of they are enabled or disabled at that point) are the any issues? What are
your thoughts on this.

Thanks in Advance

"Tom Moreau" wrote:

Quote:
Check out sp_helpconstraint in the BOL.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"SQL_Learner" <SQL_Learner (AT) discussions (DOT) microsoft.com> wrote in message
news:E615AA31-2B87-4B85-BC6B-6CC01BF0D8C8 (AT) microsoft (DOT) com...
If we alter the table by disabling the constraints (use NOCHECK CONSTRAINT
ALL). Is there any command to verify that the constraints are disabled or
don't exist.




Reply With Quote
  #5  
Old   
SQL_Learner
 
Posts: n/a

Default Re: Check if DB Constraints exist on a table - 03-06-2006 , 03:05 PM



I am enabling constraint in the following way:

STMT1: ALTER TABLE Customers CHECK CONSTRAINT ALL

Disabling as:

STMT2: ALTER TABLE Customers NOCHECK CONSTRAINT ALL

In disabled or enabled state, if I am running STMT1 again and again in the
Query Analyser, I am not getting any error. Does that mean in disabled state
the SQL server will enable the constraints and in enabled state it will
ignore to reenable the constraints?

What is the performance issue in this case where I don't have a partitioned
view.

Thanks!

Reply With Quote
  #6  
Old   
Tom Moreau
 
Posts: n/a

Default Re: Check if DB Constraints exist on a table - 03-06-2006 , 03:20 PM



Yes, it will ignore things if they're already enabled.

In some cases, you could get a performance hit if you're doing a query like:

select
*
from
MyTable m
where exists
(
select
*
from
OtherTable o
where
o.FK = m.PK
)

.... and you've disabled the foreign key from OtherTable to MyTable or
re-enabled it with NOCHECK. The optimizer can take advantage of the fact
that it knows something about the data in OtherTable, due to the constraint.

--
Tom

----------------------------------------------------
Thomas A. Moreau, BSc, PhD, MCSE, MCDBA
SQL Server MVP
Columnist, SQL Server Professional
Toronto, ON Canada
www.pinpub.com
..
"SQL_Learner" <SQLLearner (AT) discussions (DOT) microsoft.com> wrote

I am enabling constraint in the following way:

STMT1: ALTER TABLE Customers CHECK CONSTRAINT ALL

Disabling as:

STMT2: ALTER TABLE Customers NOCHECK CONSTRAINT ALL

In disabled or enabled state, if I am running STMT1 again and again in the
Query Analyser, I am not getting any error. Does that mean in disabled state
the SQL server will enable the constraints and in enabled state it will
ignore to reenable the constraints?

What is the performance issue in this case where I don't have a partitioned
view.

Thanks!


Reply With Quote
  #7  
Old   
SQL_Learner
 
Posts: n/a

Default Re: Check if DB Constraints exist on a table - 03-06-2006 , 03:36 PM



Thanks Tom for your post!

Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.