[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

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

Posted on 2013-01-27
6
Medium Priority
?
301 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
[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
  • 3
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
mimran18 earned 2000 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 71

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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 

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 71

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
One of the most important things in an application is the query performance. This article intends to give you good tips to improve the performance of your queries.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

649 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