2013-08-19 44 views
2

所以我有,表的課程:SQL錯誤:1364字段 'XXXX' 沒有默認值

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 

當我作出這樣的插入

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

我得到那個錯誤:

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

我犯了什麼錯誤?我怎麼能解決它。

+0

在'courses'列名稱似乎是'teachers_id_teacher'而不是'id_teacher' - 錯字在這個問題上或在您的代碼? –

+0

我太信任我的MySQL WorkBench自動生成代碼。它在我創建時調用這樣的列(teachers_id_teacher)。無論如何THX – Aku

回答

2

如果您要發送INSERT到表courses,則沒有字段id_teacher(您需要teachers_id_teacher)。由於您使用的是外鍵,因此該鍵必須存在於表teachers之前INSERT記錄到表courses。另外,您應該只在所有語句中使用反引號(`)和單引號(')。

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

+0

這就是這樣:錯誤代碼:1364.字段'id_teacher'沒有默認值 – Aku

相關問題