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

时间:2013-08-19 14:34:20

标签: mysql sql foreign-keys

所以我有,表课程:

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

和第二个表

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

当我插入

  

插入appdziennik.courses               (name,id_teacher,about,start_date,end_date)           价值观(“数学”,“7”,“数学”,“2013-08-01”,“2014-06-29”);

我收到了这个错误:

  

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

我犯了错误?我怎么能解决它。

2 个答案:

答案 0 :(得分:2)

如果您要向表INSERT发送courses,则表示没有字段id_teacher(您希望teachers_id_teacher)。由于您使用的是外键,因此在您teachersINSERT中的记录courses之前,表{{1}} 中存在 。此外,您应该只在所有语句中使用反引号(`)和单引号(')。

答案 1 :(得分:1)

您正在尝试在列id_teacher列的课程表中插入一个值,但是您没有为列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