Possible to make foreign key auto update?

Hi guys,
The scenario:
I can submit new article and upload picture for that article.
So lets say i submitted a new article with an image. Base on the table below, my new article_id will be 2. Then problem is how the foreign key in image table auto update the article_id = 2. So that the image name auto matches the article_id i newly submitted. How do i do that?

articles table:
article_id content
1              xxx

image table(stored all the article images):
id    article_id(foreign key)     name
1            1                         xyz.jpg

You normally don’t change id keys values.

PK/FK are not meant to be changed. That’s the whole idea about id keys: values that say nothing about the data, so they should not change.

But if you have to, you could rely on ON triggers. And triggers have a history that doesn’t recommend them for this job at all, keeping together the data integrity.

you use LAST_INSERT_ID to get the value inserted in the article table and use that to insert into the images table.

^ Right you are. I misread what the OP was asking. Sorry.

I don’t use or recommend LAST_INSERT_ID myself, but that’s another story altogether.

BIG THANKS to you. I solved my problem tyty! :slight_smile: