W
W
WeReng2016-09-03 00:02:23
MySQL
WeReng, 2016-09-03 00:02:23

How to properly design a database: one post - many images?

Hello!
Please help me, I'm studying php, writing a simple blog, but I just can't decide on the structure of the database. How to properly organize the database so that many images correspond to one post? For example, a post is created in the admin panel, the title and text of the post are entered, and a lot of images are loaded.
I'm guessing we need to make two tables posts and images. Create a post_id column in the images table. I also think about the option to create 3 tables posts, images and post_images_relation, where to specify the image id and post id.
Which option is correct? Or maybe there are some other ways to organize the structure of the database?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Belyaev, 2016-09-03
@WeReng

The first option is a one-to-many relationship
The second is many-to-many
We reason: one post has many pictures, ok
We ask ourselves the question: can one picture be in only one post or can it still turn out to be in several?
By answering this question you will be able to choose the type of connection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question