4

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

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.

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

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.

3 Answers 3

5

Run below queries. Then no worries. :)

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

   ALTER TABLE smsusers AUTO_INCREMENT = 2335;
Sign up to request clarification or add additional context in comments.

2 Comments

This problem was from last 2 months. New users were not able to register on my site. After analyzing I find above problem. Thanks for resolving my problem.
I have no idea what this did, but it solved my same issue
1

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

@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;
}


}

Comments

0

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.

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.