Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 302
  • Last Modified:

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

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
RiverWalk
Asked:
RiverWalk
  • 3
  • 2
1 Solution
 
mimran18Commented:
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
 
RiverWalkAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
Get quick recovery of individual SharePoint items

Free tool – Veeam Explorer for Microsoft SharePoint, enables fast, easy restores of SharePoint sites, documents, libraries and lists — all with no agents to manage and no additional licenses to buy.

 
RiverWalkAuthor Commented:
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
 
QlemoC++ DeveloperCommented:
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
 
RiverWalkAuthor Commented:
I see. Ok, thanks for the further clarification.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now