![](/img/trans.png)
[英]Insert foreign keys in multiple MySQL tables using Python function
[英]Insert into multiple tables connected by foreign keys
假設我有3張桌子,如下所示:
artist
- artist_id // primary key, autoincrement
- artist_name // varchar
album
- album_id // primary key, autoincrement
- artist_id // foreign key to artist.artist_id
- album_name // varchar
song
- song_id // primary key, autoincrement
- album_id // foreign key to album.album_id
- song_name // varchar
將單首歌曲以及相關的藝術家和專輯插入其對應表的最佳方法是什么?
最簡單的方法是使用3個INSERT
語句和LAST_INSERT_ID()
。
INSERT INTO artist(artist_name) VALUES("Name");
INSERT INTO album(artist_id, album_name) VALUES(LAST_INSERT_ID(),"Album");
INSERT INTO song(album_id,song_name) VALUES(LAST_INSERT_ID(),"Song");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.