简体   繁体   English

java.sql.SQLException:字段“id”没有默认值

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

I am trying to insert data into arrivaltimes tables but I am getting the following error:我正在尝试将数据插入到arrivaltimes时间表中,但出现以下错误:

java.sql.SQLException: Field 'id' doesn't have a default value java.sql.SQLException:字段“id”没有默认值

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' )" );

You are missing AUTO INCREMENT for Primary Key in arrivaltimes table.您在到达时间表中缺少主键的AUTO INCREMENT Just need to add AUTO_INCREMENT while creating table只需要在创建表时添加AUTO_INCREMENT

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) )" );

因为列id被定义为NOT NULL ,这意味着它必须有一个值,而您没有提供一个值。

I was having the same problem with my table in MySQL WorkBench.我在 MySQL WorkBench 中的表遇到了同样的问题。 Clearly, it is the auto increment command that is giving the Field 'id' a default value.很明显,自动增量命令给字段“id”一个默认值。 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>
);

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

相关问题 java.sql.SQLException:字段没有默认值 - java.sql.SQLException: Field doesn't have a default value Java 实体 java.sql.SQLException:字段“id”没有默认值 - Java Entity java.sql.SQLException: Field 'id' doesn't have a default value java.sql.SQLException: 字段 &#39;supplier_id&#39; 没有默认值 - java.sql.SQLException: Field 'supplier_id' doesn't have a default value Spring Boot java.sql.SQLException:字段“id”没有默认值 - Spring Boot java.sql.SQLException: Field 'id' doesn't have a default value java.sql.SQLException:字段“participant_one_fk_id”没有默认值 - java.sql.SQLException: Field 'participant_one_fk_id' doesn't have a default value java.sql.SQLException:字段“register_id”没有默认值 - java.sql.SQLException: Field 'register_id' doesn't have a default value java.sql.SQLException:字段“ client_id”没有默认值 - java.sql.SQLException: Field 'client_id' doesn't have a default value java.sql.SQLException:field id 没有默认值 - java.sql.SQLException:field id does not have a default value 具有一对多关系映射的“ java.sql.SQLException:字段&#39;id_parent&#39;没有默认值” - “java.sql.SQLException: Field 'id_parent' doesn't have a default value” with one-to-many relationship mapping java.lang.RuntimeException:java.sql.SQLException:字段“ UserName”没有默认值 - java.lang.RuntimeException: java.sql.SQLException: Field 'UserName' doesn't have a default value
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM