您可以使用ALTER TABLE命令将新列添加到表中。
ALTER TABLE table_name ADD column_name datatype;
假设我们在数据库中有一个名为Sales的表,其中有5列,分别是ProductName,CustomerName,DispatchDate,DeliveryTime,Price和Location,如下所示:
+-------------+--------------+--------------+--------------+-------+----------------+ | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location | +-------------+--------------+--------------+--------------+-------+----------------+ | Key-Board | Raja | 2019-09-01 | 08:51:36 | 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 | Vijayawada | | Headset | Jalaja | 2019-04-06 | 05:19:16 | 6000 | Vijayawada | +-------------+--------------+--------------+--------------+-------+----------------+
接下来的JDBC程序建立与MySQL数据库的连接,并在Sales表中添加一个名为ID的新列并进行填充。
import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import java.sql.Statement; public class AddingColumn { 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 ADD ID INT NOT NULL"; //Executing the query stmt.executeUpdate(query); System.out.println("Column added......"); //inserting values in to the added column PreparedStatement pstmt = con.prepareStatement("UPDATE sales SET id = ? where ProductName = "); pstmt.setInt(1, 1); pstmt.setString(2,"Key-Board"); pstmt.executeUpdate(); pstmt.setInt(1, 2); pstmt.setString(2,"Earphones"); pstmt.executeUpdate(); pstmt.setInt(1, 3); pstmt.setString(2,"Mouse"); pstmt.executeUpdate(); pstmt.setInt(1, 4); pstmt.setString(2,"Mobile"); pstmt.executeUpdate(); pstmt.setInt(1, 5); pstmt.setString(2,"Headset"); pstmt.executeUpdate(); System.out.println("Values inserted......"); } }
输出结果
Connection established...... Column added...... Values inserted in the added column
由于我们已添加一列,因此,如果您使用SELECT命令检索Sales表的内容,则可以观察到7列,另外还有名为id的列:
select * from Sales; +-------------+--------------+--------------+--------------+-------+----------------+----+ | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location | ID | +-------------+--------------+--------------+--------------+-------+----------------+----+ | Key-Board | Raja | 2019-09-01 | 08:51:36 | 7000 | Hyderabad | 1 | | Earphones | Roja | 2019-05-01 | 05:54:28 | 2000 | Vishakhapatnam | 2 | | Mouse | Puja | 2019-03-01 | 04:26:38 | 3000 | Vijayawada | 3 | | Mobile | Vanaja | 2019-03-01 | 04:26:35 | 9000 | Chennai | 4 | | Headset | Jalaja | 2019-03-01 | 05:19:16 | 6000 | Delhi | 5 | +-------------+--------------+--------------+--------------+-------+----------------+----+ 5 rows in set (0.00 sec)