Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How liberal should I be with NOT NULL columns?

I'm designing a database schema, and I'm wondering what criteria I should use for deciding whether each column should be nullable or not.

Should I mark as NOT NULL only those columns that absolutely must be filled out for a row to make any sense at all to my application?

Or should I mark all columns that I intend to never be null?

What are the performance implications of small vs large numbers of NOT NULL columns?

I assume lots of NOT NULL columns would slow down inserts a bit, but it might actually speed up selects, since the query execution plan generator has more information about the columns..

Can someone with more knowledge than me give me the low-down?

like image 813
Blorgbeard Avatar asked Mar 17 '09 23:03

Blorgbeard


People also ask

When should a column be not null?

The NOT NULL constraint enforces a column to NOT accept NULL values. This enforces a field to always contain a value, which means that you cannot insert a new record, or update a record without adding a value to this field.

Should all columns be not null?

You must therefore use NOT NULL for all columns that cannot legitimately contain nulls. If you specify that a column is NOT NULL , you are defining a constraint that ensures that that the column can never hold or accept NULL , so you can't accidentally leave the value out.

Should you use not null?

A NOT NULL constraint in SQL is used to prevent inserting NULL values into the specified column, considering it as a not accepted value for that column. This means that you should provide a valid SQL NOT NULL value to that column in the INSERT or UPDATE statements, as the column will always contain data.

Which key column should not contain NULLs?

Primary Key is used to identify rows uniquely in a table which cannot be null while Unique key may contain null value as per SQL rules.


2 Answers

Honestly, I've always thought NOT NULL should be the default. NULL is the odd special case, and you should make a case for it whenever you use it. Plus it's much easier to change a column from NOT NULL to nullable than it is to go the other way.

like image 134
kquinn Avatar answered Oct 08 '22 19:10

kquinn


There are no significant performance consequences. Don't even think about considering this as an issue. To do so is a huge early optimization antipattern.

"Should I only mark as NOT NULL only those columns that absolutely must be filled out for a row to make any sense at all to my application?"

Yes. It's as simple as that. You're a lot better off with a NULLable column without any NULL values in it, than with the need for NULLs and having to fake it. And anyway, any ambiguous cases are better filtered out in your Business Rules.


EDIT:

There's another argument for nullable fields that I think is ultimately the most compelling, which is the Use Case argument. We've all been subject to data entry forms that require values for some fields; and we've all abandoned forms where we had no sensible values for required fields. Ultimately, the application, the form, and the database design are only defensible if they reflect the user requirements; and it's clear that there are many, many database columns for which users can present no value - sometimes at given points in the business process, sometimes ever.

like image 38
dkretz Avatar answered Oct 08 '22 19:10

dkretz