gpt4 book ai didi

MySQL 1452 错误。我不明白为什么它不起作用。外键

转载 作者:行者123 更新时间:2023-11-29 06:04:14 26 4
gpt4 key购买 nike

我似乎无法弄清楚代码的外键部分有什么问题?我看过其他线程,但似乎无法正常工作。任何帮助都是极好的。谢谢。

drop database db5;  
create database if not exists db5;
use db5;
create table if not exists bookTable(
bookNumber integer not null primary key,
bookName varchar(50),
bookPrice decimal(5,2),
coverType varchar(15) default"hardcover",
publicationDate datetime);

insert into bookTable
(bookNumber,bookName,bookPrice,coverType,publicationDate)
values
(100,"Intro to Programming",145.99,"paperback",01/20/2009),
(110,"Networking",135.95,"paperback",02/18/2015);

create table if not exists instructorTable(
instructorNumber integer not null primary key,
instructorName varchar(20),
instructorPhone varchar (10),
instructorOfficeNumber varchar(5));

create table if not exists courseTable(
courseNo integer not null primary key,
courseName varchar(20),
semester varchar(10),
bookNumber integer not null,
instructorNumber integer not null,
foreign key(bookNumber) references bookTable(bookNumber),
foreign key(instructorNumber) references instructorTable(instructorNumber));

insert into courseTable
(courseNo,courseName,semester,bookNumber,instructorNumber)
values
(101,"Programming1","Spring",100,500),
(102,"Intro to Networking","Fall",110,600);

insert into instructorTable
(instructorNumber,instructorName,instructorPhone,instructorOfficeNumber)
values
(500,"Tom Hart",5703235555,237),
(600,"Jackie Knight",5703235566,245);

use db5;
select *
from bookTable, courseTable, instructorTable;

最佳答案

您必须在 courseTable 之前运行 instructorTable 插入

关于MySQL 1452 错误。我不明白为什么它不起作用。外键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42754699/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com