Mssql deletes all tables in the database and mssql deletes the database.

Source: Internet
Author: User

Mssql deletes all tables in the database and mssql deletes the database.

<Pre name = "code" class = "html"> for this operation, you can first learn about the system table sysobjects (which has been written in the previous blog), and drop, truncate, delete.
------------ Delete the foreign key constraints of all tables (when you delete the table structure (drop), you do not need to delete the data (truncate; delete) ------- DECLARE c1 cursor forselect 'alter table ['+ object_name (parent_obj) +'] drop constraint ['+ name +']; 'From sysobjectswhere xtype = 'F' open c1 -- create the cursor declare @ c1 varchar (8000) fetch next from c1 into @ c1while (@ fetch_status = 0) beginexec (@ c1) fetch next from c1 into @ c1endclose c1deallocate c1 -------------------- Delete All Tables -------------------------- use Database Name GOdeclare @ SQL varchar (8000) while (select count (*) from sysobjects where type = 'U')> 0 beginSELECT @ SQL = 'drop table' + nameFROM sysobjectsWHERE (type = 'U ') order by 'drop table' + nameexec (@ SQL) end

 
<Pre name = "code" class = "html"> 1. the "truncate" and "delete" statements that only delete data do not delete the table's structure (Definition) drop statement will delete the constraints, triggers, and indexes on which the table structure is dependent ); stored Procedures/functions dependent on the table will be retained, but will become invalid (invalid. 2. truncate deletes all records and resets the table (the most obvious is that the automatic id is set to zero). Data will not be backed up when the deletion is performed. delete deletes records. When the automatic id continues delect deletion, the backup will be a DDL language, like all other DDL languages, it will be implicitly submitted and cannot use the ROLLBACK command for truncate. 3. be careful when using drop and truncate, and use delete with the where condition. Of course, most of the customers will have backup with no priority, when appropriate, use the appropriate method # delete to delete some data rows. Note that the where clause is included. # If you want to delete a table, use drop # If you want to retain the table and delete all the data, use truncate if it is irrelevant to the transaction. If it is related to a transaction or you want to trigger a trigger, delete is used. # Use truncate to keep up with the reuse stroage, and then re-import/insert data.


 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.