Search code examples
sql-serverforeign-key-relationship

Saving ID to SQL table that it's not a Foreign Key


I have a html select tag (dropdown) that is has Products. Each Product has its respective ID in a table called Products in the database.

I also include an option in the select called "any product" so the user can select all products to be shown when a search is done. I set the option "any product" the value 0 and I'm saving with 0 in the database.

I'm saving these searches stats in a table called ProductsSearched. The question is: is it OK to save this option with ID 0? Because this won't match any ProductID from the table Products. I won't be able to use FK from this table to the ProductsSearched.

On the other side, If I add an option "Any Product" in the Products table and assign an ID it also won't be OK because it could be misused in other tables referencing a specific product ("any product" is not a product).

Is there a standard or best way to save this data in this scenario?

Many thanks!


Solution

  • If the table ProductSearches is used for logging I would not even bother with fk to another tabel. I think it's better to just write out the value of the search. This way you won't have troubles later on if the values in the Product table should change somehow.