dbTalk Databases Forums  

relationship question

microsoft.public.sqlserver.tools microsoft.public.sqlserver.tools


Discuss relationship question in the microsoft.public.sqlserver.tools forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Kenny M.
 
Posts: n/a

Default relationship question - 12-21-2005 , 07:38 PM






Hi It may be a stupid question but I can not see why I define a relationship
and disable the option "Disabling a Foreign Key Constraint with INSERT and
UPDATE Statements"

in this case I just dont define the relationship.

Somebody can say a comment.


Reply With Quote
  #2  
Old   
Jens
 
Posts: n/a

Default Re: relationship question - 12-22-2005 , 03:11 AM







You can define FK on tables on simply disable them for loading
maintainance or other purposes, and then enable them again. Otherwise
you would have to drop them and recreate them.



CREATE TABLE SomeParentTable
(
ParentPKCol INT
CONSTRAINT PK_SomeParentTable PRIMARY KEY (ParentPKCol)
)

Create table SomeChildTable
(
PKChildCol INT,
ParentPKCol INT NOT NULL
CONSTRAINT fk1_SomeParentTable
FOREIGN KEY
REFERENCES SomeParentTable (ParentPKCol)
)


--Doesn´t work, CHECK is enabled
insert into SomeChildTable Values (1,1)

--Disabling the CHECK
ALTER TABLE SomeChildTable NOCHECK CONSTRAINT fk1_SomeParentTable
--This works now
insert into SomeChildTable Values (1,1)

--Delete it once again
DELETE FROM SomeChildTable

--THis is the normal behaviour
ALTER TABLE SomeChildTable NOCHECK CONSTRAINT fk1_SomeParentTable

--Inserting first the parent then the child records
insert into SomeParentTable Values (1)
insert into SomeChildTable Values (1,1)


Drop table SomeChildTable

Drop table SomeParentTable



HTH, Jens Suessmeyer.


Reply With Quote
  #3  
Old   
Kenny M.
 
Posts: n/a

Default Re: relationship question - 12-22-2005 , 04:34 PM



Ohh absolutly, now I see the reason
--
Kenny M.


"Jens" wrote:

Quote:
You can define FK on tables on simply disable them for loading
maintainance or other purposes, and then enable them again. Otherwise
you would have to drop them and recreate them.



CREATE TABLE SomeParentTable
(
ParentPKCol INT
CONSTRAINT PK_SomeParentTable PRIMARY KEY (ParentPKCol)
)

Create table SomeChildTable
(
PKChildCol INT,
ParentPKCol INT NOT NULL
CONSTRAINT fk1_SomeParentTable
FOREIGN KEY
REFERENCES SomeParentTable (ParentPKCol)
)


--Doesn´t work, CHECK is enabled
insert into SomeChildTable Values (1,1)

--Disabling the CHECK
ALTER TABLE SomeChildTable NOCHECK CONSTRAINT fk1_SomeParentTable
--This works now
insert into SomeChildTable Values (1,1)

--Delete it once again
DELETE FROM SomeChildTable

--THis is the normal behaviour
ALTER TABLE SomeChildTable NOCHECK CONSTRAINT fk1_SomeParentTable

--Inserting first the parent then the child records
insert into SomeParentTable Values (1)
insert into SomeChildTable Values (1,1)


Drop table SomeChildTable

Drop table SomeParentTable



HTH, Jens Suessmeyer.



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 - 2010, Jelsoft Enterprises Ltd.