[英]MySQL Inserting into table with 2 foreign keys
I'm working on a C# application that uses a MySQL Database. 我正在使用MySQL数据库的C#应用程序上工作。 I'm trying to insert into one table with 2 foreign keys, but am getting an error: 我正在尝试使用2个外键将其插入一个表,但出现错误:
Cannot add or update a child row: a foreign key constraint fails ( thedatabase
. tblC
, CONSTRAINT tblC_ibfk_1
FOREIGN KEY ( ID
) REFERENCES tblA
( ID
) ON DELETE NO ACTION ON UPDATE CASCADE) 无法添加或更新子行:外键约束失败( thedatabase
tblC
,CONSTRAINT tblC_ibfk_1
键( ID
)参考tblA
( ID
)删除时不执行更新tblC_ibfk_1
操作
The table structure is 表结构是
tblA - ID, col2, col3, col4... tblA-ID,col2,col3,col4 ...
tblB - ID, col2, col3, col4... tblB-ID,col2,col3,col4 ...
tblC - tblA.ID, tblB.ID, col3, col4, col5... tblC-tblA.ID,tblB.ID,col3,col4,col5 ...
I've been looking over the forums, and for the life of me I can't find the syntax to make it work. 我一直在浏览论坛,在我的生命中,我找不到使它正常工作的语法。
Here is what I'm using to connect. 这是我用来连接的东西。 I've confirmed that the ID's to exist in the parent tables. 我已经确认ID在父表中存在。
//Connect to the database
MySqlConnection connection = new MySqlConnection(connectionString);
MySqlCommand cmd;
connection.Open();
//Insert new record into database
cmd = connection.CreateCommand();
cmd.CommandText = "INSERT INTO tblC(tblA_ID, tblB_ID, col3, col4, col5) VALUES(@tblA_ID, @tblB_ID, @col3, @col4, @col5);";
cmd.Parameters.AddWithValue("@tblA_ID", tblA_ID);
cmd.Parameters.AddWithValue("@tblB_ID", tblB_ID);
cmd.Parameters.AddWithValue("@col3", col3);
cmd.Parameters.AddWithValue("@col4", col4);
cmd.Parameters.AddWithValue("@col5", col5);
cmd.ExecuteNonQuery();
connection.Close();
Your syntax is fine but a foreign key error indicates a data issue. 您的语法不错,但是外键错误表示数据有问题。
The error is happening because the ID being referenced does not exist in tblA. 发生错误是因为被引用的ID在tblA中不存在。
So if you are doing something like: 因此,如果您正在执行以下操作:
INSERT INTO tblC SET tblA_id = 100, tblB_id = 101
tblA MUST have a record with an ID = 100 and tblB MUST have a record with ID = 101 or a foreign key error will occur. tblA必须具有ID = 100的记录,而tblB必须具有ID = 101的记录,否则会发生外键错误。
The foreign records must be added first ; 外国记录必须先加入; use a transaction over all the inserts to ensure consistency. 在所有插入内容上使用事务以确保一致性。
This is because MySQL does not support deferred FK constraints , but then neither does SQL Server.. 这是因为MySQL 不支持延迟的FK约束 ,但是SQL Server 都不支持 。
Like MySQL in general, in an SQL statement that inserts, deletes, or updates many rows, InnoDB checks UNIQUE and FOREIGN KEY constraints row-by-row. 通常,与MySQL一样,在插入,删除或更新许多行的SQL语句中,InnoDB逐行检查UNIQUE和FOREIGN KEY约束。 When performing foreign key checks, InnoDB sets shared row-level locks on child or parent records it has to look at. 在执行外键检查时,InnoDB在必须查看的子记录或父记录上设置共享的行级锁。 InnoDB checks foreign key constraints immediately ; InnoDB立即检查外键约束 ; the check is not deferred to transaction commit. 该检查不会推迟到事务提交。 .. ..
(Besides, for surrogate/auto-increment PKs, how can one access IDs that do not exist yet?) (此外,对于代理/自动增量PK,如何才能获得一个尚不存在的访问ID?)
Inserting such a relation can be illustrated with LAST_INSERT_ID and the following SQL: 可以使用LAST_INSERT_ID和以下SQL来说明插入这样的关系:
BEGIN TRANSACTION;
DECLARE a_id INT DEFAULT 0,
b_id INT DEFAULT 0;
INSERT INTO tblA (..) VALUES (..)
SET a_id = LAST_INSERT_ID();
INSERT INTO tblA (..) VALUES (..)
SET b_id = LAST_INSERT_ID();
INSERT INTO tblC (..) VALUES (@a_id, @b_id, ..)
COMMIT;
The exact same approach must be followed when doing the inserts from the client . 从客户端进行插入时,必须遵循完全相同的方法 。 Create the foreign records; 创建国外记录; get the IDs, use the IDs in the dependent record. 获取ID,使用相关记录中的ID。
For a "generic" ADO.NET solution one can use the result of ExecuteScalar for the insert along with a trailing select (vary for database). 对于“通用” ADO.NET解决方案,可以将ExecuteScalar的结果用于插入以及尾随选择 (对于数据库而言是不同的)。 If using MySqlCommand
, from MySqlConnector, this is natively supported via LastInsertedId
. 如果使用MySqlConnector的MySqlCommand
, 则可以通过LastInsertedId
本地支持 。
When using another Framework and/or ORM, follow the rules used therein for building object graphs and/or determining inserted IDs. 使用其他框架和/或ORM时,请遵循其中使用的规则来构建对象图和/或确定插入的ID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.