本文介紹了如何在表中插入帶有外鍵的行?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我有一個表,它本身有外鍵.列 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.但是,如果還沒有插入行,我可以為它設置什么值?!
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?
謝謝
推薦答案
一個技巧:有一個帶有虛擬鍵的虛擬行,比如 99999.將它作為 FK 插入,然后將 FK 更改為其實際值.并在交易中完成.
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.
這篇關于如何在表中插入帶有外鍵的行?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!