May 16, 2021 MariaDB
In this chapter, we'll learn to delete tables.
Table deletion is easy, but remember that all deleted tables are not recoverable. T he general syntax for table deletion is as follows -
DROP TABLE table_name ;
There are two options for performing table deletion: use command prompts or PHP scripts.
At the command prompt, simply use the DROP TABLE SQL command -
root@host# mysql -u root -p Enter password:******* mysql> use PRODUCTS; Database changed mysql> DROP TABLE products_tbl mysql> SELECT * from products_tbl ERROR 1146 (42S02): Table 'products_tbl' doesn't exist
PHP provides mysql_query() for deleting tables. S imply pass its second argument with the appropriate SQL command -
<html> <head> <title>Create a MariaDB Table</title> </head> <body> <?php $dbhost = 'localhost:3036'; $dbuser = 'root'; $dbpass = 'rootpassword'; $conn = mysql_connect($dbhost, $dbuser, $dbpass); if(! $conn ) { die('Could not connect: ' . mysql_error()); } echo 'Connected successfully<br />'; $sql = "DROP TABLE products_tbl"; mysql_select_db( 'PRODUCTS' ); $retval = mysql_query( $sql, $conn ); if(! $retval ) { die('Could not delete table: ' . mysql_error()); } echo "Table deleted successfully "; mysql_close($conn); ?> </body> </html>
After successfully deleting the table, you will see the following output -
mysql> Table deleted successfully