Do I have to have an ID column in every table?

I know it might seem like the question is being repeated, but I'm asking for my case. In my database, I have two tables for a user and a second one for images of that user. My question is, should I have an ID column in the Images table?

enter image description here

I know the big question is (Should I have an ID column in every table?), There are many answers and most of the answers say (it depends on your case), so I want to know if the case can, what should I do?

+3


source to share


2 answers


You don't need an ID column. If you don't have a table that connects to the image table (for example, image data or something), or if you need to sort the user's images by the time they are saved to the database, the ID column in the image table will be useless. Having your primary key as well as your clustered index on userId + imageSrc will give you a better lookup. Please note that you will need a unique ImageSrc anyway, otherwise you can overwrite images in the file system.



+2


source


Yes, otherwise you will not be able to select the corresponding image from the image table. (This is if you decide to go with two tables.)



However, if the user only has one image associated with it, you can simply have an ImageSrc column in the user table containing the src of that single image

-1


source







All Articles