0
I have a table called Hoteis
which is related to another helper table Hoteis_Fotos
, as its name suggests this table will save the photos of a particular hotel. As you may have understood, each hotel can have more than one photo, but a maximum of 10 photos per hotel.
Assuming the structure of my table Hoteis_Fotos
be the following:
create table Hoteis_Fotos(
id_hotel_foto int not null primary key identity,
fk_hotel int not null, -- faz referência ao id do hotel na tabela 'Hoteis'
foto varbinary(max)
)
How do I add a constraint (or something like that) that makes it possible to insert only 10 photos per hotel into the table Hoteis_Fotos
?
I would do by Trigger -- https://stackoverflow.com/questions/7930286/set-limit-for-a-table-rows-in-sql
– Motta
This validation really needs to be via bank?
– Ronaldo Araújo Alves
@Motta was not aware of how to use the rollback and other types of transactions, I found interesting.
– Lone Tonberry
@Ronaldoaraújoalves not necessarily, but would avoid having to make another consultation at execution time (in the application) to check whether a given hotel already contains the 10 images or not, at least if it was not done in the bank the only way I thought was this. You would have another suggestion ?
– Lone Tonberry
I would suggest making the query. Avoid a
select count(*)
, filtered by PK (which has index), thinking about performance is like buying milk R $ 0,02 cheaper thinking about saving to buy a house.– Ronaldo Araújo Alves
a column in "hotels" qtd_photos , the Insert or delete Trigger in "hoteis_photos" would add or decrease 1 to the field , if there is 10 bar , can be a simpler solution
– Motta