Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL drop table and re-create and keep data

On our original design we screwed up a foreign key constraint in our table. Now that the table is full of data we cannot change it without dropping all of the records in the table. The only solution I could think of is to create a backup table and put all of the records in there, then delete all the records, alter the table and start adding them back. Any other (BETTER) ideas? Thanks!

Using MS SQL Server

like image 833
user516883 Avatar asked May 17 '11 15:05

user516883


People also ask

Does dropping a table delete the data?

The DROP TABLE is another DDL (Data Definition Language) operation. But it is not used for simply removing data from a table; it deletes the table structure from the database, along with any data stored in the table.

Can we rollback after dropping a table?

You cannot roll back a DROP TABLE statement. Note: For an external table, this statement removes only the table metadata in the database.

What happens when you DROP TABLE in SQL?

Dropping a table removes the table definition from the data dictionary. All rows of the table are no longer accessible. All indexes and triggers associated with a table are dropped. All views and PL/SQL program units dependent on a dropped table remain, yet become invalid (not usable).


1 Answers

I'm a bit late, just for reference.
If You are using SQL Server Management Studio, You could generate a DROP and RECREATE script with "Keep schema and data" option.

  1. Right click on the desired DB in object explorer
  2. Tasks > Generate scripts
  3. Select the table you want to script
  4. Then clicking on Advanced button
    • "Script DROP and CREATE" ->"Script DROP and CREATE"
    • "Types of data to script" -> "Schema and data"

Hope this helps

like image 126
voodoo_patch Avatar answered Oct 13 '22 22:10

voodoo_patch