Solved

SQL Server Item Purge w/ Foreign Key Constraints

Posted on 2011-02-23
3
391 Views
Last Modified: 2012-05-11
I would like to purge some items out of our sql table that has foreign key constraints associated with it. I have saved these specific items to a historical table. What is the best practice in deleting these items from this type of table with regard to the foreign keys? Do I have to drop and recreated the keys? If so, what is the proper way in doing so, so that I do not disturb the data I still want retained in the table.

Much Regards,

Traci M.

delete from dimitem  where
itemwk not in (select distinct(itemwk) from factcustsales)and datepart(year, effbegindate) <= '2008'
0
Comment
Question by:SasDev
[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
3 Comments
 
LVL 8

Accepted Solution

by:
rushShah earned 500 total points
ID: 34962955
You have to drop that foreign key constraint then delete data and then recreate constraint.

--drop foreign key

alter table tablename drop constraint ConstraintName

-- Delete data
delete from dimitem  where
itemwk not in (select distinct(itemwk) from factcustsales)and datepart(year, effbegindate) <= '2008'

--Recreate Constraint
ALTER TABLE [dbo].[TableName]
  WITH CHECK ADD CONSTRAINT [FK_TableName_TableName2] FOREIGN KEY([FK_Name])
    REFERENCES [dbo].[TableName2] ([ID])
go
0
 
LVL 1

Author Comment

by:SasDev
ID: 34963044
ALTER TABLE [dbo].[DimItem] DROP CONSTRAINT [DF_DimItem_BrandFamilyID]
ALTER TABLE [dbo].[DimItem] DROP CONSTRAINT [DF_DimItem_BrandFamilyDesc]

delete from dimitem  where
itemwk not in (select distinct(itemwk) from factcustsales)and datepart(year, effbegindate) <= '2008'

ALTER TABLE [dbo].[DimItem] ADD  CONSTRAINT [DF_DimItem_BrandFamilyDesc]  DEFAULT (' ') FOR [BrandFamilyDesc]
ALTER TABLE [dbo].[DimItem] ADD  CONSTRAINT [DF_DimItem_BrandFamilyID]  DEFAULT (' ') FOR [BrandFamilyID]
0
 
LVL 1

Author Closing Comment

by:SasDev
ID: 34963054
Thank you!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Help Required 3 117
query optimization 6 21
sql2016-WIn10: standard,for SQL servc-account.. 51 42
query output (script) from a stored procedure 4 36
I have written a PowerShell script to "walk" the security structure of each SQL instance to find:         Each Login (Windows or SQL)             * Its Server Roles             * Every database to which the login is mapped             * The associated "Database User" for this …
In this article I will describe the Detach & Attach 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 Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
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…

735 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