Question

I have a table myTable having a foreign key, let's say it's called credential_id. Most of the time an authenticated user uses it by doing some actions thus credential_id points to some value (when a row is being inserted into myTable, credential_id has a value).

However, I want to allow anonymous (not authenticated or guests) users also do some actions involving this table. Since they are not authenticated, credential_id can't have any value.

I wonder, what would be better: to allow null values for credential_id or create a special account for an anonymous user and use it whenever the user is not authenticated? The question is about so-called null pattern in a way, but regarding a database.

Was it helpful?

Solution

Such records are commonly used in data warehouses, because NULL values are usually ambiguous. Creating dummy records for various unusual circumstances is preferred, to eliminate this ambiguity. In your case, some kind of "guest" credential would be perfectly appropriate.

That said, if you expect this to be the only reason for an action to not have a credential, using NULL values is fine, too. It's reasonably intuitive: this action doesn't have any creds, but did occur, I guess the user didn't need to authenticate.

You'll need to weigh the (modest) cost of implementing a guest credential versus the chance of confusion from using NULL values.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top