虽然我已经在父表中添加了必要的信息,但我遇到了外键问题。这是模式,select语句,用于显示外键存在于父表中,以及在子表中插入新数据的错误:
MariaDB [scansmusic]> show create table chhymnal;
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| chhymnal | CREATE TABLE `chhymnal` (<br>
`CHhymnalID` int(5) unsigned NOT NULL,<br>
`Title` varchar(30) DEFAULT NULL,<br>
`CHNumber` int(5) unsigned DEFAULT NULL,<br>
`Type` varchar(1) DEFAULT NULL,<br>
`WorshipItemType` varchar(30) DEFAULT NULL,<br>
`DateLastSung` date DEFAULT NULL,<br>
`HighNote` varchar(5) DEFAULT NULL,<br>
`UseThisWeek` tinyint(1) DEFAULT NULL,<br>
`ServiceOrder` int(2) unsigned DEFAULT NULL,<br>
`StartKey` varchar(2) DEFAULT NULL,<br>
`EndKey` varchar(2) DEFAULT NULL,<br>
`Tempo` int(1) unsigned DEFAULT NULL,<br>
`TimeSig` varchar(7) DEFAULT NULL,<br>
`CCLINumb` varchar(10) DEFAULT NULL,<br>
PRIMARY KEY (`CHhymnalID`)<br>
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |<br>
+----------+-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1行(0.00秒)
MariaDB [scansmusic]&gt; MariaDB [scanningmusic]&gt; show create table chimages;
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table | Create Table |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| <br>chimages | CREATE TABLE `chimages` (<br>
`CHImageID` int(5) unsigned NOT NULL,<br>
`ImagePath` varchar(50) DEFAULT NULL,<br>
`Instrument` varchar(30) DEFAULT NULL,<br>
`CHhymnalID` int(5) DEFAULT NULL,<br>
PRIMARY KEY (`CHImageID`),<br>
CONSTRAINT `fk_images` FOREIGN KEY (`CHImageID`) REFERENCES `chhymnal` (`CHhymnalID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 |
+----------+----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+<br>
1 row in set (0.00 sec)
MariaDB [scannedmusic]> SELECT * FROM `chhymnal` WHERE `CHhymnalID` = 343;
+------------+-------------------------+----------+------+-----------------+--------------+----------+-------------+--------------+----------+--------+-------+---------+----------+
| CHhymnalID | Title | CHNumber | Type | WorshipItemType | DateLastSung | HighNote | UseThisWeek | ServiceOrder | StartKey | EndKey | Tempo | TimeSig | CCLINumb |<br>
+------------+-------------------------+----------+------+-----------------+--------------+----------+-------------+--------------+----------+--------+-------+---------+----------+
| 343 | What a Day That Will Be | 762 | h | Hymn | 0000-00-00 | Db | 0 | 12 | Ab | Ab | 3 | 3/4 | |
+------------+-------------------------+----------+------+-----------------+--------------+----------+-------------+--------------+----------+--------+-------+---------+----------+
1行(0.00秒)
MariaDB [scannedmusic]> INSERT INTO <br>Chimages(`CHImageID`,`ImagePath`,`CHhymnalID`) VALUES ( 393, "\\BMP\\762_o1.bmp",343);
错误1452(23000):无法添加或更新子行:外键 约束失败(
scannedmusic
。chimages
,CONSTRAINTfk_images
外键(CHImageID
)参考chhymnal
(CHhymnalID
))
答案 0 :(得分:0)
您正在查看错误的列。在表格chhymnal
中,没有chhymnalid = 393
行。
您的SELECT
声明表明您正在检查chhymnalid
chhymnal
表中的chimageid
,但您的外键约束适用于393
值INSERT
的{{1}}你的chhymnalid = 393
陈述。
有两种选择 - 其中一种必须是真的:
chhymnal
中没有chhymnalid
行chimageid
而不是{{1}}