【问题标题】:How to fix `AUTO_INCREMENT` error in my code如何修复我的代码中的“AUTO_INCREMENT”错误
【发布时间】:2019-12-04 10:07:39
【问题描述】:

我正在尝试解决我的错误,但我经常看到此错误。帮我解决。

 public static void fetchDetails() throws SQLException
    {
        int i;
        Scanner scann = new Scanner(System.in);
        Connection c = DriverManager.getConnection("jdbc:derby://localhost:1527/AttendClass","ms","6079");         
        Statement st = c.createStatement();

        System.out.println("\n1.Civil\n2.CSE\n3.ECE\n4.EEE\n4.ICE\n5.IT\n6.MECH\n7.MBA\nChoose Your Department: ");
        ch = scann.nextInt();

        switch(ch)
        {
            case 1:
                dept = "CIVIL";
                String tbl1 = "CREATE TABLE "+dept+"(bno int NOT NULL AUTO_INCREMENT,one varchar(30),two varchar(30),three varchar(30),four varchar(30),five varchar(30),six varchar(30),seven varchar(30),eight varchar(30)),PRIMARY KEY (bno)";
                st.executeUpdate(tbl1); 
                ResultSet rs = st.executeQuery("select * from "+dept+"");
                 System.out.println("Enter the no. of students: ");
                 i = scann.nextInt();

我希望我的表自动递增的输出。

【问题讨论】:

标签: sql derby auto-increment identity-column


【解决方案1】:

这是增加主键的方法
下面的代码是创建 Derby 数据库表的字符串语句

String SQL_CreateCBBalance = "create table CBBalance ("
+ "MID    int not null generated always as identity "
+ "       (start with 100,increment by 1),"
+ "masterbalance  varchar(20),"
+ "primary key (MID))";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-05
    • 1970-01-01
    • 1970-01-01
    • 2021-12-06
    • 2019-11-23
    • 2020-04-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多