SQL Server: setting a foreign key for a specific value

I am not sure what is the best way to structure my question.

I have a table with a foreign key column. By default, the foreign key is set to NOT NULL , and I want to save it this way, because maybe this will be the end result. But at the moment there may be records that do not need (and have) the meaning of foreign keys, and I want to distinguish them somehow, so that it is as clear as possible that these records are somewhat different from others.

I tried, but it seems that I cannot use negative numbers for bigint , which is the foreign key value in my SQL Server table. I think this is pretty standard stuff, so what is the best thing to do in this situation, besides making the foreign key NULL ?

+4
source share
2 answers

Not sure why HABO didn't make this answer, because it's almost your only option.

  • Make null
  • Create a row in the specified table and set it to TBD or any other nickname you prefer and use the identifier from it instead of NULL

If you have records that FK does not need and will never have, then you must set the column to NULL, otherwise use the temp value.

You cannot use a negative value because you SHOULD refer to something in the foreign table if you have a foreign key constraint.

+3
source

Foreign key constraints force you to reference an existing PK of another table.

One way to not mention dropping the restriction is:

 ALTER TABLE YourTable DROP CONSTRAINT fk_something 
+1
source

All Articles