Solved

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

Posted on 2013-01-27
6
292 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 68

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
Backup Your Microsoft Windows Server®

Backup all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 

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 68

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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
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…
Via a live example, show how to extract insert data into a SQL Server database table using the Import/Export option and Bulk Insert.

919 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

21 Experts available now in Live!

Get 1:1 Help Now