Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

asked on 

Drop all tables in a database, or just delete the database altogether

Hi,

Is there a sql command to drop all the tables in a database, or just delete the whole database altogether? Right now I'm using:

    DROP TABLE my_table_name

which works ok, but it would be easier to drop all of them in one step instead of iterating over every single one. If I just have to drop the whole database, that's fine too, I can just re-create it.

Thanks
DatabasesSQL

Avatar of undefined
Last Comment
Mark Wills
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

you could use this:
exec sp_MsForeachTable ' DROP TABLE ? '

Open in new window

note: above is assuming ms sql server ....
Avatar of DJ_AM_Juicebox
DJ_AM_Juicebox

ASKER

Oh yeah I was thinking non-database specific stuff - like most databases support a SELECT statement, I was wondering if there were a generic DROP ALL TABLES statement of some sort.

Thanks
sorry, no generic stuff :(
ASKER CERTIFIED SOLUTION
Avatar of Anthony Perkins
Anthony Perkins
Flag of United States of America image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Mark Wills
Mark Wills
Flag of Australia image

the DROP DATABASE will remove the instance of all tables, views, procedures and the database. The DROP Tables will leave some elements there.

What is your actual goal ?
Avatar of DJ_AM_Juicebox

ASKER

My goal was (for security purposes) see if it was possible to drop a table or the whole database without knowing the database name. If the statements didn't require the DB name or table name, then it would be really easy to mess up someone else's db. It is good that the names are required, at least.

Thanks
Avatar of Mark Wills
Mark Wills
Flag of Australia image

If you do not have a good / all encompassing security matrix, then it is relatively easy to mess up a database - with out without drop - in fact the more malicious the least obvious it might be... and if there is sufficient privilege to issue a drop, then it is just as easy to get names.
Databases
Databases

Databases are organized collections of data, most commonly accessed through management systems including schemas, tables, queries and processes that allow users to enter and manipulate the information or utilize it in other fashions, such as with web applications or for reporting purposes.

62K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo