Search code examples
phpmysqlsqlsql-delete

delete entire table


Please bear with me, I am quite new at PHP, so I am not so good yet. I am trying to make a button, which has the function that can delete all rows in MySQL database.

I have two columns in my numtable: "n_id(primary key)" and "num". They are both INT.

As it is now I have filled out 3 values in MySQL database:

n_id   num
1       4
2       9
3       47

index.html:

<form method = "post" action = "delete.php">
    <input name="delete" type="submit" id="delete" value="Delete">
</form>

delete.php:

$query = ("DELETE FROM numtable WHERE id="delete");

I have looked a lot around stack overflow, but I can only find questions, where it is a single row there has to be deleted, and not the entire table. My code is definitely wrong, but what is it that I need more?

I can see that it is also a possibility to use TRUNCATE instead of DELETE in the SQL query. As I understand it, it is best to use TRUNCATE if the database has very big data, and delete if the database is not so big. Is that correct?


Solution

  • If you need to delete all records from the table - then just omit where condition:

    DELETE FROM numtable