How to Drop MySQL table step by step guide for beginner


Mysql drop table  if exists | In the previous article we learned how to create MySQL table . In this article we learn how to drop the existing table from the mysql database.

We have to drop any existing mysql table very carefully because after dropping the mysql table we can not recover the mysql  table therefore there is a chance to data loss.

Drop existing MySQL table using mysql promt.


Before drop table in MySQL we need to list how many tables are there in the MySQL database. To list all tables in the MySQL database we need to use the below command.
List all tables in MySQL,Drop table MySQL
List all tables in MySQL


 MySQL drop table  if exists we need to login mysql server, then choose the database from which the existing table will be deleted. For these activities we need to execute the below command to delete table sql . This is a example of one table deletion.

[root@localhost ~]# mysql -uroot -p
Enter password:
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 3
Server version: 5.1.73 Source distribution

Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

mysql> use techrideradmin
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
mysql> drop table student;

Query OK, 0 rows affected (0.00 sec)
How to Drop MySQL table step by step guide for beginner,delete table sql,Mysql drop table  if exists
How to Drop MySQL table step by step guide for beginner

The student table has been dropped.



If we want to delete multiple table at a time we need to execute the below command. We need to use “,“ separator to delete multiple tables from a database.
mysql> drop tables employee,teacher;
Query OK, 0 rows affected (0.00 sec)

mysql> show tables;
+--------------------------+
| Tables_in_techrideradmin |
+--------------------------+
| student                  |
+--------------------------+
1 row in set (0.00 sec)


Now we found that one table is present among the three tables in the database.


Drop  table MySQL using MySQL workbench


We can also drop MySQL table using the MySQL workbench. First login the workbench using the credentials and then find the schemas or database and also the table which need to be dropped. Then we need to go the query section and execute the drop table command and after executing the query we got the output in the output section.


If we use “drop table IF exists” command and table name is not listed in my database , we got one warning message in the output section.If we use “drop table” command only we don’t get any warning message we got error message in the output section.


We have three tables into our database(teacher,student and employee).If we execute drop table command with IF exists and we define a table name teacher1 along with this command but this table name is not present into our database so we will get one warning message in the output.
drop mysql table if exists
drop mysql table if exists



 But when we execute same command without IF exists we will get the error message.
drop mysql table if exists
drop mysql table


Now we are going to drop MySQL table which is present in database using MySQL workbench. Before dropping MySQL first we show the table list using MySQL workbench.
show table using MySQL workbench,drop table mysql
show table using MySQL workbench
 
Then we execute the command to drop all the tables and we got the output message in the output section.After executing the command all tables will be dropped.
drop MySQL table using MySQL workbench,drop table MySQL
drop MySQL table using MySQL workbench


Drop existing mysql table using php script

To drop existing mysql table using php script we need to use mysql_query function .


To connect to mysql server we use mysql_connect function and to select the database we will use mysql_select_db function.All functions are important to drop mysql table using php script. 


To drop mysql table we need to pass two arguments inside the mysql_query function. 

Lets take an example to drop existing mysql table using php script.
<html>
   <head>
      <title>Drop existing MySQL table on linux system</title>
   </head>
   <body>
      <?php
         $mysql_host = 'localhost';
         $mysql_user = 'root';
         $mysql_pass = 'passwordofroot';
         $conn = mysql_connect($mysql_host, $mysql_user, $mysql_pass);
        
         if(! $conn ) {
            die('Could not connect the server: ' . mysql_error());
         }
         echo 'Connection successfully established';
         $sql = “DROP TABLE student”;
          mysql_select_db( 'techrideradmin' );
         $result = mysql_query( $sql, $conn );
        
         if(! $result ) {
            die('table could not drop: ' . mysql_error());
         }
         echo "Table Dropped successfully\n";
         mysql_close($conn);
      ?>
   </body>
</html>

That’s all.If this article is helpful to know about Mysql drop table  if exists please share it!!!!


SHARE

Admin

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment