The term "cornice" that your friend probably used refers to the use of bad practices, because maintaining bad code is the kind of thing that in Brazil we call "horn work" or "punishment to the cuckold".
Using JSON as you described can be seen as bad practice for the following reasons:
It’s not intuitive to work with JSON in the pure SQL language - maybe one or another database system will help you with this, but I think it would be easier to find in non-relational databases (that is, quite different from the trio SQL Server, Oracle and Mysql);
But mostly even - there is a very strong relationship between photos and events. Relational databases have this name because they have well-structured ways of dealing with these relationships. By doing the relationship through text in columns, you do what you technically call "weak relationship", which is when you take responsibility for ensuring relationships in the bank’s place. That’s a serious way to reinvention of the wheel, and the more your system becomes complex, the more you will suffer from rework and depression.
The most appropriate way to deal with this is to make a relationship with foreign keys. In this case, since an event can have multiple photos, but a photo is only part of an event (right?), the photo table should be daughter event table. The photo table needs a column of the same type as the event table ID column. This specific column will be a foreign key, that will point to the primary key in the event table. You can call this column from id_evento
or some other descriptive name.
The effect of this is that by default:
- you will not be able to delete events that have photos associated with them;
- every photo should already be inserted in the database pointing to an event.
This is a way to ensure data integrity. There will be no photo without event (although there may be event without photo). The rules of this relationship can be changed... For example, deleting an event can automatically delete all photos.
From now on, it’s all setup. You can only know what is the best configuration when you "put your hand in the dough" and fiddle with the system, so it’s no use asking the "best" way to set up relationships here. What I recommend is that you play a lot with this, because learning by experience is much superior to any other form of learning.
And to view all photos of an event, you can use INNER JOIN:
SELECT
i.*
FROM
images i INNER JOIN eventos e
WHERE
i.id_evento = e.id
I think the table images is that it should have a field that referred to the
id
of the record in the events table. This is clear if each image only belongs to one event.– ramaral
then, but then it would become a table with an FK coming from events right? and type, I have a table that uses the id of the images as reference, Voce suggests to create a table eventos_imgs and make the link in this table?
– Backend FG1
If each image only belongs to an event (Onetomany relation) it suffices to FK if an image can belong to more than one event(Manytomany relation) then yes it is necessary to create a third table.
– ramaral
This is what we call XGH: http://helio.loureiro.eng.br/index.php/pessoal/39-blog/240-extreme-go-horse-xgh
– Leonel Sanches da Silva