Solved

Explain this TSQL

Posted on 2010-08-31
4
384 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 143

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 143

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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.
How to leverage one TLS certificate to encrypt Microsoft SQL traffic and Remote Desktop Services, versus creating multiple tickets for the same server.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

730 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