Easy Tutorial
❮ Mysql Using Sequences Mysql Connection ❯

MySQL Delete Table

Deleting a table in MySQL is quite straightforward, but you must be very careful when performing this operation as all data will be lost once the delete command is executed.

Syntax

The following is the general syntax for deleting a MySQL table:

DROP TABLE table_name ;

Deleting a Table in Command Prompt

To delete a table in the mysql> command prompt, use the DROP TABLE statement:

Example

The following example deletes the table tutorialpro_tbl:

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

Deleting a Table Using PHP Script

PHP uses the mysqli_query function to delete a MySQL table.

This function takes two parameters and returns TRUE on success or FALSE on failure.

Syntax

mysqli_query(connection,query,resultmode);
Parameter Description
connection Required. Specifies the MySQL connection to use.
query Required. Specifies the query string.
resultmode Optional. A constant. Can be one of the following: MYSQLI_USE_RESULT (use this if you need to retrieve large amounts of data)<br> MYSQLI_STORE_RESULT (default)

Example

The following example deletes the table tutorialpro_tbl using a PHP script:

<?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));
}
echo 'Connection successful<br />';
$sql = "DROP TABLE tutorialpro_tbl";
mysqli_select_db( $conn, 'tutorialpro' );
$retval = mysqli_query( $conn, $sql );
if(! $retval )
{
  die('Failed to delete table: ' . mysqli_error($conn));
}
echo "Table deleted successfully\n";
mysqli_close($conn);
?>

After successful execution, we can verify the deletion using the following command:

mysql> show tables;
Empty set (0.01 sec)
❮ Mysql Using Sequences Mysql Connection ❯