TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   MySQL & Databases (http://www.talkphp.com/mysql-databases/)
-   -   Empty a DB? (http://www.talkphp.com/mysql-databases/2393-empty-db.html)

obolus 03-02-2008 01:28 AM

Empty a DB?
 
Is there a way to empty a DB without having direct access (ie phpMyAdmin)? I searched the MySQL reference documentation and couldn't find (or maybe I missed it?) what I was looking for.

SOCK 03-02-2008 01:35 AM

The best method, IMHO, is to use TRUNCATE (similar to DELETE on all records, but resets the table as well).

I don't know what you mean by 'direct access'. If you don't have 'direct access' one way or another (PHP script, phpMyAdmin, mysql command line client) then how else would you perform a query?

Wildhoney 03-02-2008 01:50 AM

I think he means via a query and not the MySQL application via command line. If I'm right then SOCK is right with TRUNCATE :-) !

SOCK 03-02-2008 02:33 AM

Ok, well as long as the connecting user has DELETE permissions, TRUNCATE will work from a PHP script.

MySQL Manual : TRUNCATE

obolus 03-02-2008 09:06 PM

I don't want to empty all of the existing tables, I want to get rid of every existing table in the database.

Thanks for replies.

Alan @ CIT 03-02-2008 09:25 PM

How many tables do you have? You could just use DROP TABLE (MySQL :: MySQL 5.1 Reference Manual :: 12.1.16 DROP TABLE Syntax) on each one.

PHP Code:

mysql_query("DROP TABLE foo");
mysql_query("DROP TABLE bar");
// ... 

Might take a while if you have thousands of tables though :-)

Alan

SOCK 03-02-2008 10:28 PM

Quote:

Originally Posted by obolus (Post 11805)
Is there a way to empty a DB...

Quote:

Originally Posted by obolus (Post 11805)
I don't want to empty all of the existing tables, I want to get rid of every existing table in the database.

Most of the time when forum posts request info on "how to do this with a database", they're referring to the table, not the actual database. I naturally assumed you wanted to empty the table, not DROP all tables from the database.

You can use the command line tool 'mysqladmin' to drop all tables in a comma delimited list, I believe. If you could create a system exec() statement that called this function with those parameters it might be easier to do that several calls to mysql_query() to drop the table(s) (depending on how many you have).

The other way to look at it, if you have the privilege to do so, just make one call to
Code:

DROP DATABASE databasename;

obolus 03-02-2008 11:26 PM

aye, sorry for the confusion, sock. I'm pretty sure I dont have the perms to be able to drop/delete an entire database.

Alan- that answers my question to a degree, but- the problem, and my fault for not bringing this up in my original post, is that I don't know all of the table names/prefixes in the db.

So, is it possible to drop all tables in the DB with something along the lines of this:

Quote:

DROP * FROM dbname
or

Quote:

DROP TABLE * FROM dbname
..?

Thanks for help so far!

Salathe 03-02-2008 11:32 PM

Just DROP the database. If you don't have permissions, get them.


All times are GMT. The time now is 12:41 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0