开发者

NULL or set a default value

开发者 https://www.devze.com 2023-02-14 19:46 出处:网络
I have a table for user questions and each question has a \"question_score\" field. for this field, if i allow it to be a NULL then this may save some spaces or even maybe save some CPU times, will it

I have a table for user questions and each question has a "question_score" field. for this field, if i allow it to be a NULL then this may save some spaces or even maybe save some CPU times, will it?

开发者_Go百科question_id (int) | quesion_name (varchar) | question_score (int) ...


You won't save any space or cpu time.

Even if the value is NULL, MySQL will still have to store this fact.

Sometimes, permits NULL value is the right thing to do, but surely not when the reason is some non-existent space optimization. Default value is generally the way to go.


Use nulls if you cannot know the value at the time the data is entered unless there is a reasonable alternative. For instance if you don't know the name of something, you could perhaps have a default value of 'Unknown'. however if you don't know the enddate, don't put in some fake data that you always have to remember to code around. And never put in data that is fake than might conceivably be a real value (0 for price for instance) or you won't be able to tell the items you gave away from the ones you haven't set a price for yet. Nulls are good, use them when appropriate. It is much harder to work around fake data used in place of nulls (and far more likely to have a problem where you aren't returning the correct results, but don't know it) in my 30 years of database experience than it is to properly handle nulls.


In short, no. It's a question of correctness, not performance. Nulls almost invariably lead to inconsistencies and incorrect results. The best strategy is to design the database in Normal Form without nulls, unless and until you find a compelling reason to do otherwise.


In my opinion are null-values always a no-go. Please apply database normalization to at least to the third level. Probably your db-structure is not optimal.

0

精彩评论

暂无评论...
验证码 换一张
取 消