SQL错误:1364字段'XXXX'没有默认值

时间:2021-10-08 08:20:56

So I have, table courses:

我有,餐桌课程:

CREATE  TABLE IF NOT EXISTS `AppDziennik`.`courses` (
  `id_course` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(80) NOT NULL ,
  `teachers_id_teacher` INT NOT NULL ,
  `end_date` DATE NULL ,
  `about` VARCHAR(255) NULL ,
  `start_date` DATE NULL ,
  PRIMARY KEY (`id_course`) ,
  UNIQUE INDEX `id_course_UNIQUE` (`id_course` ASC) ,
  CONSTRAINT `fk_courses_teachers1`
    FOREIGN KEY (`teachers_id_teacher` )
    REFERENCES `AppDziennik`.`teachers` (`id_teacher` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

and second table

和第二个表

CREATE  TABLE IF NOT EXISTS `AppDziennik`.`teachers` (
  `id_teacher` INT NOT NULL AUTO_INCREMENT ,
  `name` VARCHAR(45) NOT NULL ,
  `surname` VARCHAR(45) NOT NULL ,
  `about` VARCHAR(255) NULL ,
  `id_class` INT NULL ,
  `rank` VARCHAR(45) NULL ,
  `logins_id_login` INT NOT NULL ,
  PRIMARY KEY (`id_teacher`) ,
  INDEX `fk_teachers_classes1_idx` (`id_class` ASC) ,
  INDEX `fk_teachers_logins1_idx` (`logins_id_login` ASC) ,
  CONSTRAINT `fk_teachers_classes1`
    FOREIGN KEY (`id_class` )
    REFERENCES `AppDziennik`.`classes` (`id_class` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION,
  CONSTRAINT `fk_teachers_logins1`
    FOREIGN KEY (`logins_id_login` )
    REFERENCES `AppDziennik`.`logins` (`id_login` )
    ON DELETE NO ACTION
    ON UPDATE NO ACTION)
ENGINE = InnoDB

When I make that insert

当我插入的时候

insert into appdziennik.courses (name, id_teacher, about, start_date, end_date) values ("Math",'7',"Math",'2013-08-01','2014-06-29');

插入appdziennik。课程(名称、id_teacher、about、start_date、end_date)值(“Math”、“7”、“Math”、“2013-08-01”、“2014-06-29”);

I get that error:

我得到这个错误:

Error Code: 1364. Field 'teachers_id_teacher' doesn't have a default value

错误代码:1364。字段'teachers_id_teacher'没有默认值

Where I made mistake? How i could fix it.

我犯了错误在哪里?我该如何修复它。

2 个解决方案

#1


2  

If you're sending an INSERT to table courses, there is no field id_teacher (you want teachers_id_teacher). Since you're using a foreign key, that key must exist in table teachers before you INSERT a record into table courses. Also, you should only be using backticks (`) and single quotes (') in all your statements.

如果要向表课程发送插入,则不存在字段id_teacher(需要teachers_id_teacher)。由于使用的是外键,所以在将记录插入到表课程之前,必须在表教师中存在该键。此外,您应该在所有语句中只使用回勾(')和单引号(')。

#2


1  

you are trying to insert into the courses table for the column id_teacher a value but your not inserting a value for the column teachers_id_teacher. so I would propose to make the statement as followed:

您正在尝试将一个值插入到id_teacher列的courses表中,但是没有为teachers_id_teacher列插入一个值。因此,我提议如下发言:

insert into appdziennik.courses (name, teachers_id_teacher, about, start_date, end_date) values ("Math",'7',"Math",'2013-08-01','2014-06-29');

Sarajog

Sarajog

#1


2  

If you're sending an INSERT to table courses, there is no field id_teacher (you want teachers_id_teacher). Since you're using a foreign key, that key must exist in table teachers before you INSERT a record into table courses. Also, you should only be using backticks (`) and single quotes (') in all your statements.

如果要向表课程发送插入,则不存在字段id_teacher(需要teachers_id_teacher)。由于使用的是外键,所以在将记录插入到表课程之前,必须在表教师中存在该键。此外,您应该在所有语句中只使用回勾(')和单引号(')。

#2


1  

you are trying to insert into the courses table for the column id_teacher a value but your not inserting a value for the column teachers_id_teacher. so I would propose to make the statement as followed:

您正在尝试将一个值插入到id_teacher列的courses表中,但是没有为teachers_id_teacher列插入一个值。因此,我提议如下发言:

insert into appdziennik.courses (name, teachers_id_teacher, about, start_date, end_date) values ("Math",'7',"Math",'2013-08-01','2014-06-29');

Sarajog

Sarajog