Solved

Best way to truncate an entire database under Sql Server 2005 Express?

Posted on 2008-10-07
13
944 Views
Last Modified: 2012-08-13
Hello,

I have a project that involves regularly flushing data from 3 tables with the following structures:

Customer - all the usual fields + a primary key of customer id, PKCustomerID referencing FKCustomerID in the Orders table.

Orders - basic billing/shipping info and a primary key of orderid, PK OrderID, and a foreign key of FKCustomerID referencing PKCustomerID in the customer table.

OrderItems - order line item information, primary key of PKOrderLineItemID, foreign key of FKOrderID referencing PKOrderID in the Orders table.

Essentially, this is like an intermediary database that we import new information into on a daily basis, but flush the old data first. There's an application that sits on top the SQL Server database that allows creation/deletion of databases. Right now, I'm doing I'm flushing the existing data by recreating the database with the application that sits on top the DB, since I can't just truncate each table in turn due to foreign key relations, triggers, etc. but that's rather tedious/time consuming.

Is there an easier way to just empty all records from the database in general without having to drop/recreate the foreign keys etc?

Any insight greatly appreciated.

Thanks!
-James
0
Comment
Question by:jmoriarty
13 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22660988
>>Is there an easier way to just empty all records from the database in general without having to drop/recreate the foreign keys etc?

No, not really.  You could truncate the tables, but you'd have to drop the FKeys first.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22661246
Well you can just do a delete. then wait. Can change the database to simple for a little while, then change it back - it will help a bit. But those foreign keys will prevent a truncate.
0
 
LVL 70

Assisted Solution

by:Éric Moreau
Éric Moreau earned 100 total points
ID: 22661410
script your database, drop it and run back your script
0
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 11

Expert Comment

by:aaronakin
ID: 22663973
It sounds like you are using these 3 tables to stage your data.  In my experience, you generally do not add constraints to staging tables.  Instead, you should verify and cleanse the data once it is in your staging tables and before it goes into the prod tables.  This means no FKs.  This also allows you an easy way to truncate the tables before you repopulate them.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22664964
Could also change the foreign keys to cater for cascading deletes. Won't help with truncate, but then only have to delete "parent" table.
0
 

Author Comment

by:jmoriarty
ID: 22665401
Scripting the DB is a great idea, I wish I'd thought of it before. What's the best way to go about that with MSSQL 2005 Express? Have to excuse my ignorance, my experience is primarily in MySQL. Cascading delete is a good option as well, that's something we could build into the import script even.

It's a little more complex than just using those tables to stage data unfortunately. We basically have a proprietary module for processing shipments that has no real "import" tool, so we export orders from our ecommerce system to import into the shipping app, but the only access we have is to the database itself, which contains all kinds of triggers, stored procedures, and the aforementioned constraints. So each day's orders are exported from the ecommerce database for import into the shipping application.

It's definitely not the best solution; Ideally I want to build an app to handle most of the data migration automatically, but there's other development projects of higher priority in the short term.

Thanks for the help folks.
0
 

Author Comment

by:jmoriarty
ID: 22665512
I misspoke slightly in the above. I know where to generate the scripts from within MS SQL Management Studio, but the issue is, one of the tables (Users) has an encrypted password field. What option do I need to set on the script generation wizard,  or what's the best way to script those type of objects?

Here's the error I'm getting:

TITLE: Microsoft.SqlServer.Express.Smo
------------------------------

Script transfer failed.

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.2047.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Script+transfer+Transfer&LinkId=20476

------------------------------
ADDITIONAL INFORMATION:

Script failed for Server 'SQL_SERVER_BOX1'.  (Microsoft.SqlServer.Express.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.2047.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.FailedOperationExceptionText&EvtID=Script+Server&LinkId=20476

------------------------------

Property TextHeader is not available for Trigger '[TG_Users_Change]'. This property may not exist for this object, or may not be retrievable due to insufficient access rights.  The text is encrypted. (Microsoft.SqlServer.Express.Smo)

For help, click: http://go.microsoft.com/fwlink?ProdName=Microsoft+SQL+Server&ProdVer=9.00.2047.00&EvtSrc=Microsoft.SqlServer.Management.Smo.ExceptionTemplates.PropertyCannotBeRetrievedExceptionText&EvtID=TextHeader&LinkId=20476

------------------------------
BUTTONS:

OK
------------------------------


Alternatively, is there a way to remove the encryption from a trigger/database object? it's a local application, no need for encryption other than plain text since everyone has the credentials by default anyways.
0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22665708
If it is just Customer, Order, Orderitems, then why do the entire database ?

Create script to create the Foreign Key constraints, and then use that to write a new script :
1) drop FK
2) truncate items, orders, customers (in that sequence)
3) create FK

0
 
LVL 51

Accepted Solution

by:
Mark Wills earned 400 total points
ID: 22666193
Something like :

alter table orderitems drop constraint FKOrderID;
alter table orders drop constraint FKCustomerID;
GO

truncate orderitems
truncate orders
truncate customer
GO

alter table orderitems add constraint FKOrderID Foreign Key (orderid)
references orders (orderid) on delete cascade;
GO

alter table orders add constraint FKCustomerID Foreign Key (customerid)
references customer (customerid) on delete cascade;
GO
0
 

Author Comment

by:jmoriarty
ID: 22667298
That's a very good point, mark_wills.  I was thinking of doing the whole thing because data does exist in the other tables, and could do a blanket drop/resubmit, but this is a much better option I think. The rest of the data is static anyway.  Going to give that a go in a short bit then. I shall report back.

Thanks again for the collaborative comments.
0
 

Author Comment

by:jmoriarty
ID: 22676930
Hi Folks,

Thanks for the help so far.  There were a lot more foreign key references than I was first aware of. References from other tables to these target tables basically. So I'm working through finding & adding those to the script in the short term. I'll report back on success or failure shortly.

0
 
LVL 51

Expert Comment

by:Mark Wills
ID: 22680125
this might help:

select * from information_schema.table_constraints where constraint_type like 'F%'
0
 

Author Comment

by:jmoriarty
ID: 22693150
That helped immensely actually. There was almost 25 keys referencing those tables that needed to be accounted for as well before they could be truncated. Got a good script working now though, in the fashion you suggested.

Thanks again!
-James
0

Featured Post

Backup Solution for AWS

Read about how CloudBerry Backup fully integrates your backups with Amazon S3 and Amazon Glacier to provide military-grade encryption and dramatically cut storage costs on any platform.

Question has a verified solution.

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

If you don't have the right permissions set for your WordPress location in IIS, you won't be able to perform automatic updates. Here's how to fix the problem.
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…
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…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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