MySQL SQLException java.sql.SQLException: 字段 'userid' 没有默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19708992/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 19:17:27  来源:igfitidea点击:

SQLException java.sql.SQLException: Field 'userid' doesn't have a default value

mysql

提问by manish Sahu

My usertable has previously string primary key now i added a new field userid bigint(20)and made this primary key. then added

我的user表以前有字符串主键,现在我添加了一个新字段userid bigint(20)并创建了这个主键。然后添加

ALTER TABLE smsusers AUTO_INCREMENT = 2335;

Total records in user table are 2334 next record should take value 2335 and should be auto incremented as records are added.

用户表中的总记录数为 2334 下一条记录应取值为 2335,并且应在添加记录时自动递增。

Problem is while inserting data from my application it is showing following error

问题是从我的应用程序插入数据时显示以下错误

SQLException  java.sql.SQLException: Field 'userid' doesn't have a default value

How to resolve this problem

如何解决这个问题

EDIT:while trying to insert new data

编辑:在尝试插入新数据时

    INSERT INTO `dbname`.`smsusers` ( `password`, `fname`, `lname`,
 `mailid`, `dob`, `gender`) VALUES ('asdf', 'asdf', 'asdf',
 '[email protected]', '2013-10-10', 'm');

It is showing following error

它显示以下错误

#1062 - Duplicate entry '0' for key 1

EDIT: Initially my primary key was id than i added userid and made this primary key

编辑:最初我的主键是 id 而不是我添加的 userid 并制作了这个主键

MY table structure is

我的表结构是

CREATE TABLE `smsusers` (
 `id` varchar(60) NOT NULL DEFAULT '',
 `password` varchar(50) NOT NULL,
 `fname` varchar(30) NOT NULL,
 `lname` varchar(30) DEFAULT NULL,
 `mailid` varchar(50) NOT NULL,
 `dob` date NOT NULL,
 `gender` varchar(10) NOT NULL,
 `city` varchar(70) DEFAULT NULL,
 `userid` int(20) NOT NULL DEFAULT '0',
 PRIMARY KEY (`userid`),
 KEY `id_pk_unique` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1; 

While inserting data in smsusers it it only taking default value at 0 not the incremented value.

在 smsusers 中插入数据时,它只采用默认值 0 而不是增量值。

回答by Praveen Prasannan

Run below queries. Then no worries. :)

运行以下查询。那就不用担心了。:)

   ALTER TABLE smsusers MODIFY COLUMN userid INT(20) AUTO_INCREMENT;

   ALTER TABLE smsusers AUTO_INCREMENT = 2335;

回答by Vivek Pal

make sure that you are not using setter for your primary key in model class.

确保您没有在模型类中使用 setter 作为主键。

@Entity
public class BlogDetails {
@Id
@GeneratedValue
@Column(name="B_Id",nullable=false)
private int b_Id;
@Column(name="EmailId")
private String emailId;
@Column(name="Title")
private String title;
@Column(name="BriefDetail")
private String briefDetail;
@Column(name="Detail")
private String detail;

public BlogDetails() {
}

public BlogDetails(String EmailId, String Title, String BriefDetail, String      

Detail) {
this.emailId=EmailId;
this.title=Title;
this.briefDetail=BriefDetail;
this.detail=Detail;
}

public int getB_Id() {
return b_Id;
}

public String getEmailId() {
return emailId;
}

public void setEmailId(String emailId) {
this.emailId = emailId;
}

public String getTitle() {
return title;
}

public void setTitle(String title) {
this.title = title;
}

public String getBriefDetail() {
return briefDetail;
}

public void setBriefDetail(String briefDetail) {
this.briefDetail = briefDetail;
}

public String getDetail() {
return detail;
}

public void setDetail(String detail) {
this.detail = detail;
}


}

回答by Jakob

Since your table has userid as PRIMARY KEY you cannot use DEFAULT 0 as a value. PRIMARY KEY must be unique and it wont be if you have a default value on it. Change DEFAULT 0 to AUTO_INCREMENT instead.

由于您的表的 userid 为 PRIMARY KEY,因此您不能使用 DEFAULT 0 作为值。PRIMARY KEY 必须是唯一的,如果您有默认值就不会。改为将 DEFAULT 0 更改为 AUTO_INCREMENT。

It never uses the auto incremented value because you never say which column should be auto_increment.

它从不使用自动递增的值,因为你从不说哪一列应该是 auto_increment。