I’m designing a database containing messages and messengers.
create table Message(
MessageID int,
MessengerID int,
Content nvarchar(max)
)
create table Messenger(
MessengerID int,
MessengerName nvarchar(100)
)
Sometimes the messenger in unknown. Would you use a NULL value in that case, or a reserved record for unknown messengers in the Messenger table? I’d love to see a short explanation why one solution is better than the other.
Neither. I would use another table for the messenger info:
(I note in passing that ALL your columns are nullable and your tables don’t have keys. I would fix that first!)
NULL in SQL does not accurately represent the semantics of something being “unknown”. Using it that way frequently leads to contradictions and incorrect results and it isn’t necessary if you design tables that accurately model the situation that the database is supposed to represent.
Another reason not to use nullable foreign keys is that different DBMSs disagree on how they work and users probably won’t understand them or use them correctly.