Solved

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

Posted on 2013-01-27
6
289 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 

Author Comment

by:RiverWalk
Comment Utility
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
Comment Utility
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
Comment Utility
I see. Ok, thanks for the further clarification.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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 retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Via a live example, show how to shrink a transaction log file down to a reasonable size.

744 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

13 Experts available now in Live!

Get 1:1 Help Now