简体   繁体   English

MySQL auto_increment不递增

[英]MySQL auto_increment not incrementing

I'm using MySQL 5.x and I don't know why I the auto_increment isn't working. 我正在使用MySQL 5.x,但我不知道为什么auto_increment无法正常工作。 I set the addressID as Primary Key in the AddressInformation and the addressID in PersonalInformation as the Foreign Key for AddressInformation's addressID. 我在addressInformation中将addressID设置为主键,在PersonalInformation中将addressID设置为AddressInformation的addressID外键。 Is this the cause? 这是原因吗? When I update it via Workbench, it's working but when I update it via JDBC, it isn't working. 当我通过Workbench更新它时,它正在工作,但是当我通过JDBC更新它时,它不起作用。 It throws and parameter index out of range error. 它将引发参数索引超出范围错误。 Here's the code for the two tables 这是两个表的代码

CREATE TABLE  PersonalInformation  (
   Username  VARCHAR(50) NOT NULL,
   Lastname  VARCHAR(50),
   Firstname  VARCHAR(50),
   Middlename  VARCHAR(50),
   Gender  CHAR(1),
   Birthdate  VARCHAR(50),
   AddressID  INT NOT NULL,
   Email  VARCHAR(50),
   PhoneNumber  VARCHAR(50),
  PRIMARY KEY  ( Username )
);

CREATE TABLE  AddressInformation  (
   AddressID  INT NOT NULL AUTO_INCREMENT,
   Country  VARCHAR(50),
   ZipCode  VARCHAR(50),
   State  VARCHAR(50),
   City  VARCHAR(50),
   Street  VARCHAR(50),
   HouseNumber  VARCHAR(50),
  PRIMARY KEY  ( AddressID )
);

ALTER TABLE  PersonalInformation  ADD CONSTRAINT  PersonalInformation_fk1  FOREIGN KEY ( AddressID ) REFERENCES AddressInformation( AddressID );

And the code in my servlet: 还有我的servlet中的代码:

private void toDatabase(PersonalBean p)
    {
        try
        {
            Class.forName("com.mysql.jdbc.Driver");
            Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/FoundationSystem","root","!Qaz2wsx");
            PreparedStatement stmt1;
            stmt1 = con.prepareStatement("INSERT INTO AddressInformation (Country, ZipCode, State, City, Street, HomeNumber) VALUES (?,?,?,?,?,?)");
            stmt1.setString(2, "TestCountry");
            stmt1.setString(3, p.getFirstName());
            stmt1.setString(4, "TestState");
            stmt1.setString(5, "TestCity");
            stmt1.setString(6, "TestStreet");
            stmt1.setString(7, "TestHouse");
            stmt1.executeUpdate();
        }
        catch (Exception e)
        { 
            System.out.println(e);
        }
    }

If my opinion all its all right. 如果我认为一切正常。 Look this 看看这个

SQL Fiddle SQL小提琴

The default size of an INT is 11, which is what would be here in PersonalInformation: INT的默认大小为11,这就是PersonalInformation中的大小:

AddressID  INT NOT NULL

but you've got 15 specified in your AddressInformation table: 但您在AddressInformation表中指定了15个:

AddressID  INT(15) NOT NULL AUTO_INCREMENT

Yes I would agree that this relationship is causing the dysfunction. 是的,我同意这种关系会导致功能障碍。

Resize the PersonalInformation to 15 and try again. 将PersonalInformation调整为15,然后重试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM