問題描述
我有一個(gè)表,它本身有外鍵.列 parentid 是外鍵,它不能為 NULL.
I have table that has foreign key for itself. Column parentid is foreign key and it cannot be NULL.
如果我這樣做INSERT INTO mytable(name) VALUES('name')
,那么它說不能將NULL插入parentid.但是,如果還沒有插入行,我可以為它設(shè)置什么值?!
if I doINSERT INTO mytable(name) VALUES('name')
, so it says that can't insert NULL to parentid. BUT, what value I can set to it if no row was inserted yet?!
如何編寫向該表添加行的腳本?
How I can write script that will add row to this table?
謝謝
推薦答案
一個(gè)技巧:有一個(gè)帶有虛擬鍵的虛擬行,比如 99999.將它作為 FK 插入,然后將 FK 更改為其實(shí)際值.并在交易中完成.
A trick: Have a dummy row with a dummy key, say 99999. Insert with this as the FK, and then change the FK to its real value. And do it in a transaction.
這篇關(guān)于如何在表中插入帶有外鍵的行?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!