What is the difference between drop table and delete table in SQL Server? - sql-server

In SQL Server, what is the difference between the following two-
Right click on a database object (table/view) and opt for Drop table (i.e. Script table as -> DROP To -> New Query Editor Window)
Right click on a database object (table/view) and opt for Delete.
I tried them both and both perform the same action. Any reason for having two options for the same thing? Is the Delete option just a crude way of dropping the DB object?
Just for the record - I'm using SS2008.

DROP will delete all data and the table structure as well.
DELETE will delete the data but the table structure will remain the same and we can still rollback the data. Also with DELETE you can use the where condition i.e. to delete only certain records.

it is drop table and delete object, at least in SQL Server 2005. Both perform the same action.
Delete table and Drop table are not the same though. The former will delete all data from the table whilst the latter will remove the table from the database.

Delete v/s Drop.
Delete statement performs conditional based deletion, whereas Drop command deletes entire records in the table.
Delete statement removes only the rows in the table and it preserves the table structure as same, and Drop command removes all the data in the table and the table structure.
Delete operation can be rolled back and it is not auto committed, while Drop operation cannot be rolled back in any way as it is an auto committed statement.

One of these performs a delete, the other provides you with the TSQL script to do a delete so you can modify or use it elsewhere.

Those two are the same operations. DROP TABLE is SQL statement for this, Delete is standard, user-friendly, menu-driven command name. That's all.

In the delete object GUI, on top there is a 'script' option which gives the t-sql statement which is plain drop table statement. Where as when you go for the drop table option, the t-sql generated would perform drop only if the table exists in the sys.objects table.

Drop table..it will delete complete table from the Database.it can not retrieved bak
Delete is used to deleting data from the table.. data can be retrieved using ROLL BACK.

Another major difference is that in DELETE it will traverse/scan to all(conditional based on predicate) the records and does delete. But in drop it will traverse records instead it will remove the actual table entry from database. If we look at execution plan for Delete:
In case of Drop as it is DDL command it will not even traverse records and will not create any execution plan.

If you choose Delete, then choose to script it, you will see that it gives you a drop table statement as well.

Using Delete command rows can be deleted from table conditionally, but changes can be done/undone by using COMMIT and ROLLBACK. Table structure is never lost. Truncate is an alternative of Delete in the sense that it allows Deletion of rows without losing the table structure but UNLIKE Delete, operation can't be undone. Drop statement however, removes the table completely along with its structure from the database and this operation can't be undone. So, Delete is a DML statement and Turncate and Drop are DDL statements.

Drop table will delete the content of the table and the table self.
Delete table will just drop the content.
Truncate will just 'reset' the content to zero

We can delete the particular record from a table with using delete command . but using drop we can drop the tables. if you use Drop the structure will also go, but if you use delete command the structure won't go.

Related

Cloning a table without importing row data

I need to make a provisionary table (TAB_PROV). This table will have its origin in a main table (TAB_MAIN). I need everything from the main table, except the data (rows).
I searched for some examples and none of them worked for me
CREATE TABLE TAB_PROV LIKE TAB_MAIN
CREATE TABLE TAB_PROV AS TAB_MAIN
You can just simply do:
SELECT *
FROM TAB_MAIN
INTO TAB_PROV
WHERE 1 = 2
Since the WHERE condition will never be true, no data is ever copied - but the table structure is replicated - TAB_PROV is created and has the same column as TAB_MAIN. This does NOT however copy any constraints (check or default constraints) or triggers over - it only recreates the columns (and their datatypes).
If you want a real and complete "copy" of your table, then you should use the "Script Table" function in SSMS to get the SQL needed for TAB_MAIN and then adapt it to create TAB_PROV from it.
In SQL Server Management Studio, you can right-click the table and select
Script Table as -> Create To -> New Query Editor Window
This will create just the table creation script for you.
You can also try the code below but it will copy everything.
SELECT *
INTO NewTable
FROM OldTable
TRUNCATE TABLE NewTable

Deleting a large number of rows by copying rows to keep into temporary table, dropping original table, and copying rows back

I have a table in an SQL Server 2012 database that has over 30 million rows, most of which I need to delete. However, deleting those rows takes far too long (we're talking multiple days).
The most likely solution I've found online is one where I use a SELECT INTO query to grab all the rows I want to keep and put them in a temporary table, the DROP the original table before using another SELECT INTO statement to repopulate the original table from the temporary table.
However, I've run into the problem where I can't drop the original table because it is referenced by foreign key constraints. I've used the following query:
SELECT * FROM [sys].[foreign_keys] WHERE referenced_object_id = object_id('OriginalTable')
To get the foreign key constraints, but when I try ALTER TABLE OriginalTable NOCHECK CONSTRAINT [Constraint name], I get a message saying that the constraint does not belong to the table.
My goal is to remove these 30 million rows without altering the state of my database, so I want to keep any foreign keys intact. Am I going about this the right way? If so, how can I drop the original table?
I've come up with what appears to be a working solution:
Step 1: I use a SELECT INTO statement to back up the desired rows into a temporary table.
Step 2: I use SQL Server Management Studio's "Script Key As" functions to make DROP and CREATE sql scripts for the foreign key constraints.
Step 3: I drop the foreign key constraints using the created scripts.
Step 4: I TRUNCATE the original table.
Step 5: I use an INSERT INTO statement to copy all rows from my temporary table back into the truncated original table.
Step 6: I recreate the foreign key constraints using the created scripts.
Step 7: I drop the temporary table.
I believe this effectively deletes the rows I don't want, and leaves the database in the same state it was in at the start of the process (minus the rows I don't want, of course).

SQL Server SysTable with tstamp of last inserted row of each table

Is there any system table or dmv in SQL Server 2008 R2 that contains information regarding the last DML statement (except select) that was issued against any user table?
I see that in sys.tables there is a modify_date column but that's just for any table alteration (DDL statements).
I wouldn't want to create triggers on every table in the db nor a trigger on the database level for this scope.
The reason for this is that I would like to see when was the last time an insert, update or delete statement was made into each table in order to see if I can drop some of the tables that are no longer used - this is for a DWH db, where each table in the db is supposed to have any of these 3 operations at least once a week/month/quarter/year.
Option 1:
Enabling Change Data Capture for your DB.
Refer the below link for CDC:
http://technet.microsoft.com/en-us/library/cc627369%28v=sql.105%29.aspx
Option 2:
Create trigger for each table and do logging in common table whenever INSERT/UPDATE/DELETE happens in any table(Old traditional method).

Can executing DDL command e.g alter query affects the rows of the table?

I am trying to execute scenarios when DDL commands affects the number of rows of the table,but executing alter command always return 0 number of rows.
a) Table a has 3 columns b,c,d with 3 records inserted.
b) ALTER TABLE a DROP COLUMN b
Dropping column b shouldn't be affecting the rows already in table.
To speak in simple terms, Except for TRUNCATE , And DROP TABLE no other DDL would affect the data (rows) the table holds..
To speak technically, a DDL is supposed to touch the schema objects and not the data. TRUNCATE will reset the High WaterMark (HWM). And marked as blocks have no data. DROP TABLE will entirely drop the metadata and the data associated with it.
CREATE as SELECT is something oracle gives specially apart from standard SQL, where a DDL (CREATE) is executed first using the result set metadata(FROM SELECT) and then data is loaded too! If the loadimg fails for any reason, the process would be halted and no object creation happens!

SQL code to copy data from 1 database table to another

I have messed up the data. I have been ammended ids and values to the point where i cannot remember what the originals were. (testing a few things).
I have a table called query_string_interpretation and its part of the DB called, test1_db
I have a backup database which i have restored called, test2_db
How can I restore its contents from one database table to another?
Without more information on your part...generally you can use INSERT INTO/SELECT FROM to move data between tables.
The syntax is roughly:
INSERT INTO
test1_db..query_string_interpretation ( <your columns here>)
select <your columns here>
FROM test2_db..query_string_interpretation
At first, you need to be sure that you have all your data in source table, or data is placed in both tables - source and destination. In first case you need to truncate the destination table:
TRUNCATE TABLE test1_db..query_string_interpretation
Second, you need to be sure that you will insert the right values into IDENTITY fields, if these fields exists. Use SET INDENITY_INSERT ON statement.
Third, you need to insert the values:
insert into test1_db..query_string_interpretation
select * from test2_db..query_string_interpretation
And don't forget to switch the INDENITY_INSERT to OFF, you you switched it to ON in the second step.
You can use a SQL wizard to do this for you. In Management Studio, right click on either database, select Tasks then Import Data... or Export Data...
try this:
delete from test1_db..query_string_interpretation
insert into test1_db..query_string_interpretation
select * from test2_db..query_string_interpretation
if you have an identity field you may have to write the name of the columns (except for the identity one). if you need to keep the IDs, take a look at the SET IDENTITY_INSERT ON statement here

Resources