Solved

SQL Server Item Purge w/ Foreign Key Constraints

Posted on 2011-02-23
3
392 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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL Recursion schedule 13 48
Many to one in one row 2 48
SQL Server maintenance plan not deleting backup files as anticipated 4 32
SQL Query Across Multiple Tables - Help 5 48
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 …
Long way back, we had to take help from third party tools in order to encrypt and decrypt data.  Gradually Microsoft understood the need for this feature and started to implement it by building functionality into SQL Server. Finally, with SQL 2008, …
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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