Solved

Does droping and recreating an SQL Server table, delete the tables data?

Posted on 2013-01-27
6
297 Views
Last Modified: 2013-01-27
Hello,

I need to add a primary key field to a populated table on MS SQL Server. The table is in production. I have read that certain activities/changes to a table cause it to be dropped and recreated. Knowing so little about this, it makes me wonder if that means the data will be deleted in the process. Although that would seem crazy to me, because I can easily add a new field to a MS Access table and not have the data wiped out.

So, here are the direct questions.

1. Does adding a new field to a table cause the table to drop and recreate?

2. Does dropping and recreating a table cause its data to be wiped out?

Thank you,
Riverwalk
0
Comment
Question by:RiverWalk
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
mimran18 earned 500 total points
ID: 38824552
1- No you donot need to drop the table simply add a new field.

2- If you drop a table definitly you will lose its data. If you want to drop and recreate a table better to do like this.

First take a backup into a temporary table.

Select * into temp_table from table

Then drop and recreate the table.

and then insert the data back to the table

Insert into table select * from temp_table.
0
 

Author Closing Comment

by:RiverWalk
ID: 38824564
Thank you very much. Wow! I am surprised. I am glad I asked!

It amazes me that SQL Server would need to drop/recreate & wipe data for changes that MS Access can handle without doing such a thing. As a parting response, can you comment on why SQL Server requires such a harsh reaction to something that MS Access easily handles?

Thank you,
Riverwalk
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38824582
The answer to your first question was "No you donot need to drop the table simply add a new field". Doesn't that say all?
MSSQL requires to recreate the table when there is referencial integrity - foreign keys and such. Or if you want to change datatypes (e.g. from varchar to integer). This is done to make sure everything keeps in place, and all references are maintained properly.
The MSSQL Management Studio (SSMS) does that by renaming the table, creating the new one with the changes as requested, and then reinstate primary and foreign keys, constraints and such.
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

Author Comment

by:RiverWalk
ID: 38824596
Ok. Thanks. I understood your answer. I was just under the impression that SQL Server had to drop and recreate for some changes that MS Access did not need to. But maybe that's not the case. Thanks again.

Riverwalk
0
 
LVL 69

Expert Comment

by:Qlemo
ID: 38824609
There are a lot of features Access does not allow for, and so things are more easy there. Also, the front-end is in control over the MDB file, and does not care much about what happens with other users currently connected ...
0
 

Author Comment

by:RiverWalk
ID: 38824645
I see. Ok, thanks for the further clarification.
0

Featured Post

Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

Question has a verified solution.

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

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.

856 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