-1

Can anybody give me an example when to use

  • allow null
  • default 0
  • default '' and empty string.

In which situations should use these different configurations?

2
  • I'm confused, the way you wrote that it seems like you're trying to use them all together. You want to know when you'd use each one separately? Commented Aug 30, 2011 at 15:51
  • yes. it is exactly what i wanna know Commented Aug 30, 2011 at 15:54

2 Answers 2

2

In general, avoid NULLs. NULL tends to require extra coding effort. Treatment for NULL versus empty string varies by RDBMS. Sorting of NULL within a set varies by RDBMS.

That said, you may wish to: Use NULLs on foreign key columns when the related row is optional.

Use NULLs when you want values to be eliminated from aggregate operations. For example, if you have an "age" column, but don't require this information for all records, you would still be able to get meaningful information from: SELECT AVG(age) FROM mytable

Use NULLs when you need ternary logic.

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

3 Comments

"In general, avoid NULLs"? No way! I would say the opposite: If you don't have a value for something, let the value be NULL. This not only is semantically more accurate but also allows you to improve performance in your queries. Is not the same thing testing for NULL than testing for ''.
I can see the points of both Jamey and @Icarus. I would add that in a 6th normal form database, you should be able to design the structure such that there are no nulls or blank fields. It's rather awkward to do though, and in my experience, often isn't worth the effort. more info
Here is another example from the related list at the right. I've always used an empty string because 1) I've never had a need to use NULL and 2) I don't have that many fields in my databases that can even be left empty.
1

1.A NULL value represents the absence of a value for a record in a field (others softwares call it also a missing value).

2.An empty value is a "field-formatted" value with no significant data in it.

3.NULL isn't allocated any memory, the string with NUll value is just a pointer which is pointing to nowhere in memory. however, Empty IS allocated to a memory location, although the value stored in the memory is "".

4.Null has no bounds, it can be used for string, integer, date, etc. fields in a database. Empty string is just regarding a string; it's a string like 'asdfasdf' is, but is just has no length. If you have no value for a field, use null, not an empty string.

5.Null is the database's determination of an absense of a value logically, so to speak. You can query like: where FIELD_NAME is NULL

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.