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.