Duplicate entry '2' for key 'PRIMARY' error (comprehensive summary)

Method 1:

Data is provided in Table primary key (Primary Key), and a value corresponding to the primary key is not allowed to repeat.
Set the primary key increment, please ignore that has been set;

Act 2

Insert statement optimization, plus you can ignore keyword, meaning that if the primary key already exists, the article sql is not performed. If not added to the data, the interface return value is zero (0 @ new data), then you need to call into the interface of the secondary insert the data again. ! ! ! Here to remind the interface return value void students, the return value is best to use int (@ return value is meaningful).

INSERT IGNORE INTO tase1......

Act 3:

The current id 10000 is not data, but the Insert id = 10000 when this report was wrong! We need to abandon the id, directly to the table from the current transfer large increase in the number, you can:

alter table Table_Name AUTO_INCREMENT=11000;
id=10000的有问题,我直接把自增数调到11000(不要调的太大,因为每条都会有空间的资源浪费),这下不会冲突了

Law 4 :( emphasis)

1. The primary key is set to grow automatically. You can set the ID correlation table in the database manually to grow from
2 to manually set a table to growth since there are drawbacks, it is recommended POJO class annotation using annotations provided inside the self-growth. mysql database is provided as shown in FIG.
@GeneratedValue (strategy = GenerationType.IDENTITY)

@Entity
@Table(name = "f_user")
@Data
public class FUser implements Serializable {
    private static final long serialVersionUID = 8060959602677501490L;
    //    @Id:声明主键字段
    //    @Data:构造set get方法
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Integer uid;

    private String uname;

    private String utime;

    private String upwd;

    @Column(insertable = false,updatable = false)
    private Integer hid;

    private String filePath;

    @OneToOne
    @JoinColumn(name = "hid",referencedColumnName = "hid")
    private FHospital fHospitalDemo;
    //@joincolumn 中name的属性对用数据库中的字段addressId,referencedColumn Name属性对应关联表的主键

}
Published 14 original articles · won praise 4 · Views 247

Guess you like

Origin blog.csdn.net/Red_rose9/article/details/105257507