2

所以我有,餐桌课程:

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) 值 ("Math",'7',"Math",'2013-08-01','2014-06-29');

我得到那个错误:

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

我在哪里犯错了?我怎么能解决它。

4

2 回答 2

2

如果您要发送INSERTto table courses,则没有字段id_teacher(您想要teachers_id_teacher)。由于您使用的是外键,因此该键必须先存在于 table 中teachers ,然后才能INSERT记录到 table 中courses。此外,您应该只在所有语句中使用反引号 (`) 和单引号 (')。

于 2013-08-19T14:37:15.097 回答
1

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

萨拉约格

于 2013-08-19T14:41:30.520 回答