3

I am trying to insert data into arrivaltimes tables but I am getting the following error:

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

stt.execute("CREATE TABLE IF NOT EXISTS stops"
            + "(stop_id INT(11) NOT NULL AUTO_INCREMENT PRIMARY KEY, "
            + " name varchar(30) NOT NULL, "
            + " route INT(11) NOT NULL, "
            + " lat double(10,6) NOT NULL, "
            + " longi double(10,6)NOT NULL) " );

    stt.execute("INSERT INTO stops(name, route, lat, longi) values"
            + "('blabla', '1', '93.838039', '15.700440' ),"
            + "('backery', '9', '98.868863', '19.665438' )" );

    stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL PRIMARY KEY,"
            +  " weekday VARCHAR(20) NOT NULL,"
            + "arrivaltime time NOT NULL,"
            + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );
    //The error appears in this execution statement.
    stt.execute("INSERT INTO arrivaltimes(weekday, arrivaltime) values"
            + "('mon-fri', '05:30' ),"
            + "('mon-fri', '06:07' )" );
2
  • Your column id in the arrivaltimes table is your primary key, make sure you are inserting some value against this column Commented May 8, 2015 at 22:09
  • You probably want to make id auto_increment in the arrivaltimes table. Commented May 8, 2015 at 22:11

3 Answers 3

5

You are missing AUTO INCREMENT for Primary Key in arrivaltimes table. Just need to add AUTO_INCREMENT while creating table

stt.execute("CREATE TABLE IF NOT EXISTS arrivaltimes(id INT(11) NOT NULL  AUTO_INCREMENT PRIMARY KEY,"
            +  " weekday VARCHAR(20) NOT NULL,"
            + "arrivaltime time NOT NULL,"
            + " stop_id INT, FOREIGN KEY fk_stop_id(stop_id) REFERENCES stops(stop_id) )" );
Sign up to request clarification or add additional context in comments.

Comments

0

Because the column id is defined as NOT NULL which means it has to have a value and you're not providing one.

Comments

0

I was having the same problem with my table in MySQL WorkBench. Clearly, it is the auto increment command that is giving the Field 'id' a default value. Here is the code that worked for my table:

create table product( <br>
id int auto_increment PRIMARY KEY, <br>
name varchar(20), <br>
description varchar(100), <br>
price decimal(8,3) <br>
);

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.