dbTalk Databases Forums  

Turning Off Foreign Keys during DB coping

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Turning Off Foreign Keys during DB coping in the microsoft.public.sqlserver.dts forum.



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

Default Turning Off Foreign Keys during DB coping - 11-15-2005 , 08:17 AM






I have step in my package that copies database from the server to my local
machine.
I am doing it so I can back up database - I am using hosting database.

The problem is that coping of data fails if I specify in options that I want
to copy Primary and Foreign keys. I would like to copy that also in order to
have ready to restore back up.
Is there a way to turn off keys after schema was copied but before data is
loaded?
Thank you,
Shimon.



Reply With Quote
  #2  
Old   
Michael Cheng [MSFT]
 
Posts: n/a

Default RE: Turning Off Foreign Keys during DB coping - 11-15-2005 , 08:43 PM






Hi Shimon,

Welcome to use MSDN Managed Newsgroup!

You could use NOCHECK instead of UNCHECKED when altering a existing
constraint to be invalid. I made a sample on my side with DTS and it works.
Here is a sample

CREATE TABLE Test2
(
TID INT Primary KEY,
CID INT IDENTITY(1,1) UNIQUE
)

INSERT Test2 VALUES (11)
INSERT Test2 VALUES (12)
INSERT Test2 VALUES (13)
INSERT Test2 VALUES (14)

--SELECT * FROM Test2

CREATE TABLE Test2Child
(
TCID INT FOREIGN KEY REFERENCES Test2(CID),
TC CHAR(1)
)

INSERT Test2Child VALUES (55,'B')
--The statement above will fail
ALTER TABLE Test2Child NOCHECK CONSTRAINT FK__Test2Child__TCID__48CFD27E

INSERT Test2Child VALUES (55,'B')
--The statement above will do since we have altered NOCHECK
--Use a DTS package to do the test, it also works

NOTE that if you have specified NOCHECK for the constraint, it will not
check the constraints any more.



Sincerely yours,

Michael Cheng
Microsoft Online Partner Support

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no rights.


Reply With Quote
  #3  
Old   
Shimon Sim
 
Posts: n/a

Default Re: Turning Off Foreign Keys during DB coping - 11-16-2005 , 02:17 PM



Michael
Do you suggest me to write the script for each table to disable keys?
It is not really a solution since I have over 100 tables. Plus the schema
changes and my script can become invalid.
Is there something that can disable all the keys whatever they are?
An if such thing exists how can I out it in the DTS package together with
backup and data transfer tasks?

Thank you,
Shimon

"Michael Cheng [MSFT]" <v-mingqc (AT) online (DOT) microsoft.com> wrote

Quote:
Hi Shimon,

Welcome to use MSDN Managed Newsgroup!

You could use NOCHECK instead of UNCHECKED when altering a existing
constraint to be invalid. I made a sample on my side with DTS and it
works.
Here is a sample

CREATE TABLE Test2
(
TID INT Primary KEY,
CID INT IDENTITY(1,1) UNIQUE
)

INSERT Test2 VALUES (11)
INSERT Test2 VALUES (12)
INSERT Test2 VALUES (13)
INSERT Test2 VALUES (14)

--SELECT * FROM Test2

CREATE TABLE Test2Child
(
TCID INT FOREIGN KEY REFERENCES Test2(CID),
TC CHAR(1)
)

INSERT Test2Child VALUES (55,'B')
--The statement above will fail
ALTER TABLE Test2Child NOCHECK CONSTRAINT FK__Test2Child__TCID__48CFD27E

INSERT Test2Child VALUES (55,'B')
--The statement above will do since we have altered NOCHECK
--Use a DTS package to do the test, it also works

NOTE that if you have specified NOCHECK for the constraint, it will not
check the constraints any more.



Sincerely yours,

Michael Cheng
Microsoft Online Partner Support

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no
rights.




Reply With Quote
  #4  
Old   
Wm. Scott Miller
 
Posts: n/a

Default Re: Turning Off Foreign Keys during DB coping - 11-16-2005 , 02:46 PM



You can create a task that will create a script (or even a Stored Proc) that
will disable and then reenable the constraints. You can generate both
scripts by the below resultset:

SELECT 'ALTER TABLE ' + TABLE_NAME + ' NOCHECK CONSTRAINT ALL' FROM
INFORMATION_SCHEMA.TABLES

SELECT 'ALTER TABLE ' + TABLE_NAME + ' CHECK CONSTRAINT ALL' FROM
INFORMATION_SCHEMA.TABLES

Scott

"Shimon Sim" <shimonsim048 (AT) community (DOT) nospam> wrote

Quote:
Michael
Do you suggest me to write the script for each table to disable keys?
It is not really a solution since I have over 100 tables. Plus the schema
changes and my script can become invalid.
Is there something that can disable all the keys whatever they are?
An if such thing exists how can I out it in the DTS package together with
backup and data transfer tasks?

Thank you,
Shimon

"Michael Cheng [MSFT]" <v-mingqc (AT) online (DOT) microsoft.com> wrote in message
news:%2338TAel6FHA.3036 (AT) TK2MSFTNGXA02 (DOT) phx.gbl...
Hi Shimon,

Welcome to use MSDN Managed Newsgroup!

You could use NOCHECK instead of UNCHECKED when altering a existing
constraint to be invalid. I made a sample on my side with DTS and it
works.
Here is a sample

CREATE TABLE Test2
(
TID INT Primary KEY,
CID INT IDENTITY(1,1) UNIQUE
)

INSERT Test2 VALUES (11)
INSERT Test2 VALUES (12)
INSERT Test2 VALUES (13)
INSERT Test2 VALUES (14)

--SELECT * FROM Test2

CREATE TABLE Test2Child
(
TCID INT FOREIGN KEY REFERENCES Test2(CID),
TC CHAR(1)
)

INSERT Test2Child VALUES (55,'B')
--The statement above will fail
ALTER TABLE Test2Child NOCHECK CONSTRAINT FK__Test2Child__TCID__48CFD27E

INSERT Test2Child VALUES (55,'B')
--The statement above will do since we have altered NOCHECK
--Use a DTS package to do the test, it also works

NOTE that if you have specified NOCHECK for the constraint, it will not
check the constraints any more.



Sincerely yours,

Michael Cheng
Microsoft Online Partner Support

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no
rights.






Reply With Quote
  #5  
Old   
Shimon Sim
 
Posts: n/a

Default Re: Turning Off Foreign Keys during DB coping - 11-16-2005 , 05:48 PM



This look cool.
I have to try it.
Thank you.

"Wm. Scott Miller" <Scott.Miller (AT) spamkiller (DOT) wvinsurance.gov> wrote in
message news:O7sUD7u6FHA.1416 (AT) TK2MSFTNGP09 (DOT) phx.gbl...
Quote:
You can create a task that will create a script (or even a Stored Proc)
that will disable and then reenable the constraints. You can generate
both scripts by the below resultset:

SELECT 'ALTER TABLE ' + TABLE_NAME + ' NOCHECK CONSTRAINT ALL' FROM
INFORMATION_SCHEMA.TABLES

SELECT 'ALTER TABLE ' + TABLE_NAME + ' CHECK CONSTRAINT ALL' FROM
INFORMATION_SCHEMA.TABLES

Scott

"Shimon Sim" <shimonsim048 (AT) community (DOT) nospam> wrote in message
news:e$ma4qu6FHA.3752 (AT) tk2msftngp13 (DOT) phx.gbl...
Michael
Do you suggest me to write the script for each table to disable keys?
It is not really a solution since I have over 100 tables. Plus the schema
changes and my script can become invalid.
Is there something that can disable all the keys whatever they are?
An if such thing exists how can I out it in the DTS package together with
backup and data transfer tasks?

Thank you,
Shimon

"Michael Cheng [MSFT]" <v-mingqc (AT) online (DOT) microsoft.com> wrote in message
news:%2338TAel6FHA.3036 (AT) TK2MSFTNGXA02 (DOT) phx.gbl...
Hi Shimon,

Welcome to use MSDN Managed Newsgroup!

You could use NOCHECK instead of UNCHECKED when altering a existing
constraint to be invalid. I made a sample on my side with DTS and it
works.
Here is a sample

CREATE TABLE Test2
(
TID INT Primary KEY,
CID INT IDENTITY(1,1) UNIQUE
)

INSERT Test2 VALUES (11)
INSERT Test2 VALUES (12)
INSERT Test2 VALUES (13)
INSERT Test2 VALUES (14)

--SELECT * FROM Test2

CREATE TABLE Test2Child
(
TCID INT FOREIGN KEY REFERENCES Test2(CID),
TC CHAR(1)
)

INSERT Test2Child VALUES (55,'B')
--The statement above will fail
ALTER TABLE Test2Child NOCHECK CONSTRAINT FK__Test2Child__TCID__48CFD27E

INSERT Test2Child VALUES (55,'B')
--The statement above will do since we have altered NOCHECK
--Use a DTS package to do the test, it also works

NOTE that if you have specified NOCHECK for the constraint, it will not
check the constraints any more.



Sincerely yours,

Michael Cheng
Microsoft Online Partner Support

When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
================================================== ===
This posting is provided "AS IS" with no warranties, and confers no
rights.








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