mysql alert on delete cascade_如何在mysql中删除级联使用?(How do I use on delete cascade in mysql?)...
如何在mysql中刪除級聯使用?(How do I use on delete cascade in mysql?)
我有一個組件數據庫。 每個組件都是特定的類型。 這意味著組件和類型之間存在多對一的關系。 當我刪除一個類型時,我想刪除所有具有該類型外鍵的組件。 但是如果我沒有弄錯,級聯刪除將刪除該組件時刪除的類型。 有什么辦法可以做我描述的嗎?
I have a database of components. Each component is of a specific type. That means there is a many-to-one relationship between a component and a type. When I delete a type, I would like to delete all the components which has a foreign key of that type. But if I'm not mistaken, cascade delete will delete the type when the component is deleted. Is there any way to do what I described?
原文:https://stackoverflow.com/questions/511361
更新時間:2019-06-19 03:33
最滿意答案
以下是你在組件表中包含的內容。
CREATE TABLE `components` (
`id` int(10) unsigned NOT NULL auto_increment,
`typeId` int(10) unsigned NOT NULL,
`moreInfo` VARCHAR(32),
-- etc
PRIMARY KEY (`id`),
KEY `type` (`typeId`)
CONSTRAINT `myForeignKey` FOREIGN KEY (`typeId`)
REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
請記住,您需要使用InnoDB存儲引擎:默認的MyISAM存儲引擎不支持外鍵。
Here's what you'd include in your components table.
CREATE TABLE `components` (
`id` int(10) unsigned NOT NULL auto_increment,
`typeId` int(10) unsigned NOT NULL,
`moreInfo` VARCHAR(32),
-- etc
PRIMARY KEY (`id`),
KEY `type` (`typeId`)
CONSTRAINT `myForeignKey` FOREIGN KEY (`typeId`)
REFERENCES `types` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
Just remember that you need to use the InnoDB storage engine: the default MyISAM storage engine doesn't support foreign keys.
2009-02-04
相關問答
以下是你在組件表中包含的內容。 CREATE TABLE `components` (
`id` int(10) unsigned NOT NULL auto_increment,
`typeId` int(10) unsigned NOT NULL,
`moreInfo` VARCHAR(32),
-- etc
PRIMARY KEY (`id`),
KEY `type` (`typeId`)
CONSTRAINT `myForeignK
...
ON DELETE CASCADE將刪除表中包含外鍵(子)的表中的行,當它所引用的表中的行(父)被刪除時。 如果沒有ON DELETE CASCADE ,帶有外鍵(child)的行將被指向一個不再存在的行(父),并且會得到一個INTEGRITY CONSTRAINT VIOLATION 。 相反,沒有這樣的問題,在沒有刪除父項的情況下刪除子項不會留下孤立的行,并且就MySQL而言不存在任何完整INTEGRITY CONSTRAINT VIOLATION ,并且不需要級聯。 如果您想一起刪除孩子,父
...
如果您已設置表B以刪除表A的外鍵上的級聯,那么如果標記被刪除,表B中的相應記錄將被刪除。 表B刪除不會影響表A,因為表A沒有任何引用或外鍵到表B. If you have set up table B to delete cascade on the foreign key to table A then if a tag get's deleted the corresponding records in Table B will be deleted. Table B deletions wi
...
Order表中有拼寫錯誤。 你有PRIMAY它應該是PRIMARY 。 在糾正之后,我嘗試創建表格并且所有語句都正常工作,包括最后一個。 You have a typo in the Order table. You have PRIMAY where it should be PRIMARY. After correcting this, I tried creating the tables and all statements worked fine, including the last
...
您的表和引用的表都在相關列上有索引嗎? 來自http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html : InnoDB需要外鍵和引用鍵的索引,以便外鍵檢查可以快速而不需要表掃描。 在引用表中,必須有一個索引,其中外鍵列以相同的順序列為第一列。 如果索引不存在,則會自動在引用表上創建此索引。 (這與某些舊版本形成對比,其中必須明確創建索引或創建外鍵約束失敗。)如果給定index_name,則如前所述使用。 首
...
如果表上沒有ON DELETE CASCADE,您可以嘗試使用AFTER DELETE觸發器實現它。 否則,可以輕松定義ON DELETE CASCADE。 但是,請小心,特別是如果子表有多個約束。 If there is no ON DELETE CASCADE on a table, you could try implementing it with a AFTER DELETE trigger. Otherwise, ON DELETE CASCADE can be defined eas
...
不,簡單的答案是,不,沒有捷徑。 您可以記下DELETE語句以刪除相關表中的所有相關行,或者使用ON DELETE CASCADE定義外鍵約束。 請注意 - 只要外鍵關系中沒有循環路徑 - 就可以使用從多個表中刪除的單個DELETE語句: DELETE a, b, c, d
FROM a
LEFT JOIN b ON b.a_id = a.a_id
LEFT JOIN c ON c.a_id = a.a_id
LEFT JOIN d ON d.b_id = b.b_id
...
按照順序: 首先,我將一些數據插入到userdetails中 INSERT INTO `test`.`userdetails` (`userid`, `forename`, `surname`, `emailaddress`, `password`, `passwordhint`, `subscriptionexpiration`, `salt`) VALUES (NULL, 'John', 'Doo', 'johndoo@john.com', '1234', 'seq.', '2012-02-0
...
您不能在ENGINE = MEMORY使用ON DELETE CASCADE 。 不支持外鍵。 這里有一個參考:( MySQL Doc。)[ https://dev.mysql.com/doc/refman/5.6/en/memory-storage-engine.html] Table 15.4 MEMORY Storage Engine Features
---------------------------------------------------
Foreign key sup
...
總結
以上是生活随笔為你收集整理的mysql alert on delete cascade_如何在mysql中删除级联使用?(How do I use on delete cascade in mysql?)...的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql数据迁移 脚本_PHP将数据从
- 下一篇: mysql容器操作_如何使用运行MySQ