Solved

SQL Server Item Purge w/ Foreign Key Constraints

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dimension table indexes 8 25
SQL Server - SQL field is defined as text 3 36
Webservices in T-SQL 3 30
SSRS 2013 - Overlapping reports 2 19
Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now