1

I have several tables that contain several strings for fields. Some of these fields may or may not always contain data. If I create a record with a string field that's null at the time of the record's creation, does it become a problem when I do a read query of that record because the field is null or does the query return an empty string? What's considered based? Non-nullabe strings or nullable strings, and when to know the difference?

Thanks for your suggestions.

3 Answers 3

1

The query will return an empty string if that is what is stored in the database and a null value (System.DBNull) if it is a nullable string with a null in it.

The decision, however, should be based on the data you are modeling, not the functionality of queries.

Null in a DB generally means that a value is either unknown or was left blank. This can be distinct from an empty string which could be used to mean there IS no value.

Take for example a MiddleName field. You might want to distinguish between someone with no middle name (empty string) and someone who didn't enter it yet (Null). In this case you'd want a nullable string field.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a similar question on the stack exchange site for Database Administrators. They pretty much echo what is said here.

One thing that has not been mentioned is that if you attempt to multiply by a null value by a real value, you get a null result. Same for lots of other operations.

Comments

1

That has been debated over and over again million times. In my opinion, though, NULL values are there for a reason, and you should stay away from "magic" values (even empty string means something, doesn't it).

NULL values do NOT get converted to anything at read-time. You'll get null. You're supposed to interpret it as "this value has not been set", and treat it that way. If you replace it with any magic value (e.g. empty string), you're just looking for trouble, as sooner or later that magic value will start having a meaning of something else, and you won't know how to distinguish it from those that have never been set.

In other words, if you consider a property to be optional, then it's definitely a candidate to be nullable. Whenever you start debating that with yourself, remember the word "optional", and I'm sure you'll make a good decision.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.