如何使用 JDBC 在 MySQL 中設定自增列的初始值?


在建立表時,在某些情況下,我們需要為列(例如 ID)生成/自動遞增值。各種資料庫以不同的方式支援此功能。

在 **MySQL** 資料庫中,您可以使用以下語法宣告一個列為自增列。

CREATE TABLE table_name(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   column_name1 data_type1,
   column_name2 data_type2,
   column_name3 data_type3,
   column_name4 data_type4,
   ............ ...........
);

在向表中插入記錄時,無需在自增列下插入值。這些值將自動生成。

設定初始值

預設情況下,自增列的初始值為 1。您可以使用 ALTER TABLE 查詢更改它,如下所示:

alter table table_name AUTO_INCREMENT = 1001

示例

讓我們在 MySQL 資料庫中建立一個名為 sales 的表,其中一列為自增列,使用 CREATE 語句如下所示:

CREATE TABLE Sales(
   ID INT PRIMARY KEY AUTO_INCREMENT,
   ProductName VARCHAR (20),
   CustomerName VARCHAR (20),
   DispatchDate date,
   DeliveryTime time,
   Price INT,
   Location VARCHAR(20)
);

以下 JDBC 程式將自增列的初始值設定為 1001,並在其中插入 6 條記錄。

示例

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
public class SettingInitialValue_AutoIncrement_Pstmt {
   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:///sample_database";
      Connection con = DriverManager.getConnection(mysqlUrl, "root", "password");
      System.out.println("Connection established......");
      //Setting the initial value of the auto-incremented column
      Statement stmt = con.createStatement();
      stmt.execute("alter table Sales AUTO_INCREMENT = 1001");
      //Query to Insert values to the sales table
      String insertQuery = "INSERT INTO Sales (ProductName, CustomerName, DispatchDate, DeliveryTime, Price, Location) VALUES (?, ?, ?, ?, ?, ?)";
      //Creating a PreparedStatement object
      PreparedStatement pstmt = con.prepareStatement(insertQuery,Statement.RETURN_GENERATED_KEYS);
      pstmt.setString(1, "Key-Board");
      pstmt.setString(2, "Raja");
      pstmt.setDate(3, new Date(1567315800000L));
      pstmt.setTime(4, new Time(1567315800000L));
      pstmt.setInt(5, 7000);
      pstmt.setString(6, "Hyderabad");
      pstmt.addBatch();
      pstmt.setString(1, "Earphones");
      pstmt.setString(2, "Roja");
      pstmt.setDate(3, new Date(1556688600000L));
      pstmt.setTime(4, new Time(1556688600000L));
      pstmt.setInt(5, 2000);
      pstmt.setString(6, "Vishakhapatnam");
      pstmt.addBatch();
      pstmt.setString(1, "Mouse");
      pstmt.setString(2, "Puja");
      pstmt.setDate(3, new Date(1551418199000L));
      pstmt.setTime(4, new Time(1551418199000L));
      pstmt.setInt(5, 3000);
      pstmt.setString(6, "Vijayawada");
      pstmt.addBatch();
      pstmt.setString(1, "Mobile");
      pstmt.setString(2, "Vanaja");
      pstmt.setDate(3, new Date(1551415252000L));
      pstmt.setTime(4, new Time(1551415252000L));
      pstmt.setInt(5, 9000);
      pstmt.setString(6, "Chennai");
      pstmt.addBatch();
      pstmt.setString(1, "Headset");
      pstmt.setString(2, "Jalaja");
      pstmt.setDate(3, new Date(1554529139000L));
      pstmt.setTime(4, new Time(1554529139000L));
      pstmt.setInt(5, 6000);
      pstmt.setString(6, "Goa");
      pstmt.addBatch();
      System.out.println("Records inserted......");
      //Executing the batch
      pstmt.executeBatch();
   }
}

輸出

Connection established......
Records inserted......

驗證

如果您使用 SELECT 語句驗證 Sales 表的內容,您可以看到插入的記錄,其 ID 值從 1001 開始,如下所示:

mysql> select * from sales;
+------+-------------+--------------+--------------+--------------+-------+----------------+
| ID | ProductName | CustomerName | DispatchDate | DeliveryTime | Price | Location |
+------+-------------+--------------+--------------+--------------+-------+----------------+
| 1001 | Key-Board | Raja | 2019-09-01 | 11:00:00 | 7000 | Hyderabad |
| 1002 | Earphones | Roja | 2019-05-01 | 11:00:00 | 2000 | Vishakhapatnam |
| 1003 | Mouse | Puja | 2019-03-01 | 10:59:59 | 3000 | Vijayawada |
| 1004 | Mobile | Vanaja | 2019-03-01 | 10:10:52 | 9000 | Chennai |
| 1005 | Headset | Jalaja | 2019-04-06 | 11:08:59 | 6000 | Goa |
+------+-------------+--------------+--------------+--------------+-------+----------------+
5 rows in set (0.00 sec)

更新於: 2019-07-30

1K+ 瀏覽量

開啟你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.