English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

MySQL Delete Table

Deleting a data table in MySQL is very easy to operate, but you must be very careful when performing the delete table operation, because all data will be deleted after executing the delete command.

Syntax

The following is the general syntax for deleting MySQL data tables:

DROP TABLE table_name ;

Delete a data table in the command prompt window

The SQL statement to delete a data table in the mysql> command prompt window is DROP TABLE :

Online example

The following example deletes the data table w3codebox_tbl:

root@host# mysql -u root -p
Enter password:*******
mysql> use w3codebox;
Database changed
mysql> DROP TABLE w3codebox_tbl
Query OK, 0 rows affected (0.8 sec)
mysql>

Delete data table using PHP script

PHP uses the mysqli_query function to delete MySQL data tables.

This function has two parameters, and returns TRUE on success, otherwise returns FALSE.

Syntax

mysqli_query(connection, query, resultmode);
ParameterDescription
connectionRequired. Specify the MySQL connection to use.
queryRequired, specify the query string.
resultmode

Optional. A constant. It can be any of the following values:

  • MYSQLI_USE_RESULT (use this if you need to retrieve a large amount of data)

  • MYSQLI_STORE_RESULT (default)

Online example

The following example uses a PHP script to delete the data table w3codebox_tbl:

<?php
$dbhost = 'localhost';  // MySQL server host address
$dbuser = 'root';            // MySQL username
$dbpass = '123456';          // MySQL username password
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Connection failed: ' . mysqli_error($conn));
}
 />
$sql = "DROP TABLE w3codebox_tbl";
mysqli_select_db($conn, 'w3codebox');
$retval = mysqli_query($conn, $sql);
if(! $retval )
{
  die('Table deletion failed: ' . mysqli_error($conn));
}
echo "Table deletion successful\n";
mysqli_close($conn);
?>

After the execution is successful, we use the following command to not see w3codebox_tbl table:

mysql> show tables;
Empty set (0.01 sec)