添加项目
问题描述:
我正准备创建使用phpMyAdmin和我撞到一个问题一个网站:我不understant为什么我的代码做的事情,我不想。添加项目
这是创建表和一些随机输入代码:
create table stud (
matrnr int primary key,
pname Varchar(30) not null
);
create table prof (
persnr int primary key,
pname Varchar(50) not null
);
create table vorl (
vorlnr int primary key,
titel varchar(50),
prof int references prof(persnr) on delete set null
);
create table prüfen (
stud int references stud(matrnr) on delete cascade,
vorl int references vorl(vorlnr),
prof int references prof(persnr) on delete set null,
note float,
primary key(Stud, vorl)
);
insert into stud values
(1, 'G'),
(2, 'F'),
(3, 'C');
insert into Prof values
(1, 'M'),
(2, 'L'),
(3, 'M');
insert into vorl values
(1, 'Info1', 'M'),
(1, 'Info2', 'L'),
(1, 'Info3', 'M');
insert into prüfen values
(1, 1, 1, 2.0),
(1, 2, 1, 1.7),
(2, 3, 2, 2.3);
在这个位置上我试图
Insert into prüfen values (3, 1, 4, 2.0);
,但仍只有3表prüfen
线。
任何帮助是惠康。 有一个美好的一周。
答
您的最终插入失败,因为它引用了一个prof
ID为4的教授,prof
表中不存在prof
表。
如果您在您的定义为prüfen
表仔细一看,这将是更加清晰:
create table prüfen (
stud int references stud(matrnr) on delete cascade,
vorl int references vorl(vorlnr),
prof int references prof(persnr) on delete set null,
note float,
primary key(Stud, vorl)
);
领域prof
是一个外键进入prof
表。 MySQL将要求该字段引用实际存在的教授。它不适用于最终插入的情况。为了解决这个错误,你可以创建一个这样的教授,例如
insert into Prof values
(4, 'M');
您没有在您的问题中报告错误,这对我来说似乎是错误的。也许你没有提到它,或者你的错误报告被关闭出于某种原因。
不会显示错误吗? OP没有提到任何有关错误的信息。 –
@RickS是的,我期望在任何地方出现错误。也许OP没有提及它,或者他的错误报告被关闭。我的解释是目前唯一想到的。 –
好的,非常感谢你。现在我明白了实际发生的事情。我重新调整了它,现在它工作正常。非常感谢你 ;) –