简体   繁体   English

DBMS 似乎在交易中提交(新手问题)

[英]DBMS appears to COMMIT mid transaction (Newbie question)

I am using the batabase that is shown in the following relational diagram:我正在使用以下关系图中显示的 batabase: 在此处输入图片说明

And using the following SQL script, I am trying to add a new book along with its new author as well as new genre (no ratings at this points) (I am using MySQL like most students):并使用以下 SQL 脚本,我尝试添加一本新书及其新作者和新类型(此时没有评分)(我像大多数学生一样使用 MySQL):

use booksdb;
start transaction;
insert into genre values ('Educational');
insert into books values ('123456789', 'Databases Illuminated',null, '1990,01,01' );
insert into book_genre values('123456789','Educational');
insert into authors(firstName, familyName) values ('Catherine','Ricardo');
insert into writes values('123456789',LAST_INSERT_ID());
COMMIT;

And I am having two problems: The first is that the DBMS is telling me that I am trying to add a tuple that contains a reference that does not exist in the referenced table (Error Code: 1452. Cannot add or update a child row: a foreign key constraint fails).我有两个问题:第一个是 DBMS 告诉我我正在尝试添加一个元组,其中包含引用表中不存在的引用(错误代码:1452。无法添加或更新子行:外键约束失败)。 And my first question is, is not the whole poind of using transaction is so that no constrains will be enforced/checked untill the transaction is complete?我的第一个问题是,使用交易的全部意义不是在交易完成之前不会强制/检查约束吗? (In which case everything should be in its place with no "empty references"). (在这种情况下,一切都应该在没有“空引用”的地方)。 The only way I am able to add everything is by doing it one line each time I press Excute button.我能够添加所有内容的唯一方法是每次按 Excute 按钮时添加一行。 The second qusetion is: Should I add primary keys first or forig ones?第二个问题是:我应该先添加主键还是先添加主键? The "obvious" answer I thought is that I needed to add PK's first, but even if I arrange all the lines in the scripts so that PK's are always added first, I am still getting Error Code: 1452, the only way I could add everything is to feed the script lines to the DBMS one line at a time.我认为的“明显”答案是我需要先添加 PK,但即使我安排脚本中的所有行以便始终首先添加 PK,我仍然收到错误代码:1452,这是我可以添加的唯一方法一切都是将脚本行一次一行地提供给 DBMS。 There is obviously something fundamentally wrong in my understanding of how this should work, I tried to look it up many times here and on Google but could not find the answer.我对这应该如何工作的理解显然存在根本性的错误,我试图在这里和谷歌上多次查找,但找不到答案。

Alrghit, I found what I was doing wrone.好吧,我发现我做错了。 In the relational diagram I made the relation between books and book_genre one to many where the many side is books, and it should have been the other way around.在关系图中,我在书籍和 book_genre 之间建立了一对多的关系,其中许多方面是书籍,它应该是相反的。 I tried to flip it and so far I do not have any problems.我试图翻转它,到目前为止我没有任何问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM