How to delete a column from an existing table in a database using JDBC API?


You can delete a column in a table using the ALTER TABLE command.

Syntax

ALTER TABLE table_name DROP COLUMN column_name;

Assume we have a table named Sales in the database with 7 columns namely id, CustomerName, DispatchDate, DeliveryTime, Price and, Location as shown below:

+----+-------------+--------------+--------------+--------------+-------+----------------+
| id | productname | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+----+-------------+--------------+--------------+--------------+-------+----------------+
| 1  | Key-Board   | Raja         | 2019-09-01   | 08:51:36     | 7000  | Hyderabad      |
| 2  | Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000  | Vishakhapatnam |
| 3  | Mouse       | Puja         | 2019-03-01   | 04:26:38     | 3000  | Vijayawada     |
| 4  | Mobile      | Vanaja       | 2019-03-01   | 04:26:35     | 9000  | Chennai        |
| 5  | Headset     | Jalaja       | 2019-04-06   | 05:19:16     | 6000  | Delhi          |
+----+-------------+--------------+--------------+--------------+-------+----------------+

Following JDBC program establishes connection with MySQL database, and deletes the column named ID to from the Sales table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DeletingColumn {
   public static void main(String args[]) throws SQLException {
      //Registering the Driver
      DriverManager.registerDriver(new com.mysql.jdbc.Driver());
      //Getting the connection
      String mysqlUrl = "jdbc:mysql://localhost/mydatabase";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Creating the Statement
      Statement stmt = con.createStatement();
      //Query to alter the table
      String query = "ALTER TABLE Sales Drop ID";
      //Executing the query
      stmt.executeUpdate(query);
      System.out.println("Column Deleted......");
   }
}

Output

Connection established......
Column Deleted......

Since we have deleted one column, if you retrieve the contents of the Sales table using the SELECT command you can observe only 6 (no column named id) columns as:

mysql> select * from Sales;
+-------------+--------------+--------------+--------------+-------+----------------+
| ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location       |
+-------------+--------------+--------------+--------------+-------+----------------+
| Key-Board   | Raja         | 2019-09-01   | 05:30:00     | 7000 | Hyderabad       |
| Earphones   | Roja         | 2019-05-01   | 05:54:28     | 2000 | Vishakhapatnam  |
| Mouse       | Puja         | 2019-03-01   | 04:26:38     | 3000 | Vijayawada      |
| Mobile      | Vanaja       | 2019-03-01   | 04:26:35     | 9000 | Chennai         |
| Headset     | Jalaja       | 2019-04-06   | 05:19:16     | 6000 | Delhi           |
+-------------+--------------+--------------+--------------+-------+----------------+
5 rows in set (0.00 sec)

Updated on: 30-Jul-2019

560 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements