How to drop constraint on a column of a table in a database using JDBC API?


You can drop a constraint on a column of a table using the ALTER TABLE command.

Syntax

ALTER TABLE table_name
DROP CONSTRAINT MyUniqueConstraint;

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

+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| ProductName  | varchar(255) | YES  | UNI | NULL    |       |
| CustomerName | varchar(255) | YES  |     | NULL    |       |
| DispatchDate | date         | YES  |     | NULL    |       |
| DeliveryTime | time         | YES  |     | NULL    |       |
| Price        | int(11)      | YES  |     | NULL    |       |
| Location     | text         | YES  |     | NULL    |       |
| ID           | int(11)      | NO   |  PRI| NULL    |       |
+--------------+--------------+------+-----+---------+-------+

Following JDBC program establishes connection with MySQL database, and drops a constraint named MyUniqueConstraint from the Sales table.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class DroppingConstraint {
   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 INDEX MyUniqueConstraint";
      //Executing the query
      stmt.executeUpdate(query);
      System.out.println("Constraint dropped......");
   }
}

Output

Connection established......
Constraint dropped......

Since we dropped UNIQUE constraint named MyUniqueConstraint, (which is on the column ProductName) from the sales table, if you get the description of the Sales table using the describe command you can observe that the Key value UNI added opposite to ProductName, removed.

mysql> describe sales;
+--------------+--------------+------+-----+---------+-------+
| Field        | Type         | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| ProductName  | varchar(255) | YES  |     | NULL    |       |
| CustomerName | varchar(255) | NO   |     | NULL    |       |
| DispatchDate | date         | YES  |     | NULL    |       |
| DeliveryTime | time         | YES  |     | NULL    |       |
| Price        | int(11)      | YES  |     | NULL    |       |
| Location     | text         | YES  |     | NULL    |       |
| ID           | int(11)      | NO   | PRI | NULL    |       |
+--------------+--------------+------+-----+---------+-------+
7 rows in set (0.00 sec)

Updated on: 30-Jul-2019

227 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements