Solved

Explain this TSQL

Posted on 2010-08-31
4
380 Views
Last Modified: 2012-05-10
Below is some TSQL from a script that has been generated by Management Studio.

The first command "CHECK ADD CONSTRAINT" creates a foreign key constration, and I guess forces a cascade delete.

What does the second command "CHECK CONSTRAINT "  do?

I am porting the database to a compact database i want to be sure i have included everything.
/****** Object:  ForeignKey [FK_FormData_Units]    Script Date: 08/31/2010 10:16:18 ******/

ALTER TABLE [dbo].[FormData]  WITH CHECK ADD  CONSTRAINT [FK_FormData_Units] FOREIGN KEY([uni_id])

REFERENCES [dbo].[Units] ([uni_id])

ON DELETE CASCADE

GO

ALTER TABLE [dbo].[FormData] CHECK CONSTRAINT [FK_FormData_Units]

GO

Open in new window

0
Comment
Question by:soozh
  • 2
4 Comments
 
LVL 13

Accepted Solution

by:
sameer2010 earned 200 total points
ID: 33565256
This will add the check constraint called FK_FormData_Units which will validate if the uni data exists in units table and on deletion of row from units, would cascade the deletion to FormData table as well.
It would then execute the constraint FK_FormData_Units
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 33565275
the second statement forces the contraint to be checked (again) explicitly...
because you could create a constraint with telling "don't check the data right now" ...
0
 

Author Comment

by:soozh
ID: 33565330
does this mean that in my case the second statement is not required?

My sql for the compact database is :

ALTER TABLE FormData
ADD CONSTRAINT FK_FormData_Units
FOREIGN KEY(uni_id) REFERENCES Units(uni_id)
ON DELETE CASCADE ;

Do i need to add anything?
0
 
LVL 142

Assisted Solution

by:Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3] earned 300 total points
ID: 33565361
exactly.
you can create / alter a constraint to be NOCHECK:
http://msdn.microsoft.com/en-us/library/ms190273.aspx

means: it exists, but is not checked by the sql engine.
with the ALTER, you can change that to be CHECKED, which will do 2 things:
* check the data, if it is valid in regards to the constraint
* if the data IS valid, enables the constraint, so any further data changes/inserts will be checked against the constraint automatically
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

In this article I will describe the Copy Database Wizard method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now