I think you are saying: you need to avoid duplicating rows in this table.
There are many ways to handle this. One of the easiest:
INSERT INTO theTable (image_id, tag_id) VALUES (39, 8) WHERE NOT EXISTS (SELECT * FROM theTable WHERE image_id = 39 AND tag_id = 8)
As @Henrik Opel noted, you can use a check constraint for combo columns, but then you should have a try / catch block somewhere else, which adds irrelevant complexity.
Edit to explain this comment ...
I guess this is a table comparing many-to-many relationships between movies and tags. I understand that you are probably using php, but I hope the C # pseudo-code below is clear enough anyway.
If I have a Movie class, the most natural way to add a tag is the AddTag() method:
class Movie { public void AddTag(string tagname) { Tag mytag = new Tag(tagname);
There is no practical way to check for duplicates earlier in this process, because another user can tag the movie at any time, so there is no way around this.
Note. . If you try to insert an error record, then there is an error, so throwing an error is appropriate, but if not, you do not need additional complexity in the error handler.
source share