Making 'NOT NULL' as the default
Logan Palanisamy, March 31, 2003 - 7:48 pm UTC
Tom,
You have demonstrated very well the usefulness of the NOT NULL constraint. Despite all its benefits, the default is still NULL when we create a table. Wouldn't it be nice if the default is NOT NULL. One should intentionally set it to NULL if there is a real need.
May be, Oracle should come up with a new init.ora which sets the database default NULL/NOT NULL behavior for all newly created columns. The default value of this new init.ora parameter can be set to NULL to simulate the current behavior. Once you set this parameter to NOT NULL, all new columns created hence forth will be NOT NULL by default which one can override with NULL where it is needed.
Do you think of any drawbacks to this approach.
March 31, 2003 - 8:31 pm UTC
well, it would be nice but a disaster.
far far far too much legacy code relies on the other way. almost NOTHING would work - even with that switch. virtually every application written would "break"
Dave, April 01, 2003 - 1:08 am UTC
Perhaps another disadvantage of automatically constraining new columns to not null would be that it would encourage the idle among us towards not naming their constraints. If there's one thing i hate, it's a system-named constraint.
create table my_table
(
my_column1 number constraint nn1my_table not null,
my_column2 number constraint nn2my_table not null,
my_column3 number constraint nn2my_table not null
)
vs
create table my_table
(
my_column1 number not null,
my_column2 number not null,
my_column3 number not null
)
It's not much to ask, people.
Michael, April 01, 2003 - 3:13 pm UTC
What is the big advantage for naming NN constraint versus not?
April 01, 2003 - 7:30 pm UTC
clarity,
meaning,
documentation,
readability in the data dictionary,
more meaningful information from error messages...
Not only that -- but I've seen massive issues with unnamed constraints with export import.
Here is the scenario:
o you export from db1
o you import into db2
o once a week you truncate and import into db2
over time, you notice that db2 is dog slow -- whats up? well, someone had a nasty un-named constraint in that and now you have it 500 times.
ops$tkyte@ORA920> create table t ( x int check ( x > 0 ) );
Table created.
ops$tkyte@ORA920> select count(*) from user_constraints where table_name = 'T';
COUNT(*)
----------
1
ops$tkyte@ORA920> !exp userid=/ tables=t
Export: Release 9.2.0.3.0 - Production on Tue Apr 1 19:32:23 2003
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to: Oracle9i Enterprise Edition Release 9.2.0.3.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production
Export done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set
About to export specified tables via Conventional Path ...
. . exporting table T 0 rows exported
Export terminated successfully without warnings.
ops$tkyte@ORA920> !imp userid=/ full=y ignore=y
Import: Release 9.2.0.3.0 - Production on Tue Apr 1 19:32:32 2003
Copyright (c) 1982, 2002, Oracle Corporation. All rights reserved.
Connected to: Oracle9i Enterprise Edition Release 9.2.0.3.0 - Production
With the Partitioning, OLAP and Oracle Data Mining options
JServer Release 9.2.0.3.0 - Production
Export file created by EXPORT:V09.02.00 via conventional path
import done in WE8ISO8859P1 character set and AL16UTF16 NCHAR character set
. importing OPS$TKYTE's objects into OPS$TKYTE
. . importing table "T" 0 rows imported
Import terminated successfully without warnings.
ops$tkyte@ORA920> select count(*) from user_constraints where table_name = 'T';
COUNT(*)
----------
2
ops$tkyte@ORA920>
Every time I import -- i get that constraint over and over (we cannot tell they are the same - -they have no name). system gets REALLY slow.....
Name things. You won't be sorry you did -- only sorry if you don't
Excellent point!!!
A reader, April 01, 2003 - 8:01 pm UTC
How to get out of unnamed constraints trap?
Arun Gupta, April 02, 2003 - 10:48 pm UTC
Tom
I recently fell into the trap of unnamed constraints, lack of naming standards and my own laziness to try and develop some. The situation is that some columns have both a NOT NULL constraint and a check constraint which allows only 'Y' and 'N'. Now I wanted to drop only the check constraint only from columns. How do I filter them out ? The column containing constraint text is a LONG and cannot be used in where clause.
Thanks
April 03, 2003 - 7:41 am UTC
search this site for longsubstr and read all of the hits.
Not nulls.
Kashif, April 03, 2003 - 4:05 pm UTC
Thanks Tom, I believe the word I had left out was 'performance' from the question. I was looking for the performance benefits of using NOT NULL constraints. Though I have tested a few things after your response and some of the responses from queries using NOT NULL columns with indexes on them has gone up, after I made changes to make them NOT NULL. Also got to use some fast full scans as a result. Thanks as always.
Kashif
NOT NULL performance
Duke Ganote, June 14, 2004 - 10:40 am UTC
I assume Kashif's performance improvement is due to the specific queries run, and B*tree indexes not storing NULL values (as you discuss in your book "Expert 1-on-1 Oracle" in the section titled "Indexes and Nulls").
Certainly, NOT NULLilfying a column requires some pseudo-NULL value (if there are truly NULLs). But introducing a pseudo-NULL brings issues of its own (such as having a difference pseudo-NULL for each datatype and potentially each column). A function-based index, as touched on in the question titled "Finding NULLs by index" at </code>
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:434019666272, <code>
appears to be one such approach.
naming not null constraints in an alter table command
Susan, June 23, 2004 - 11:11 am UTC
Tom,
I need to alter a table by adding a not null constraint. I'd like to avoid the system generated name, but, I can't seem to find the right syntax in the docs.
alter table add constraint nn_uid u_id not null; (and other attempts) throws errors. Can this be done? Thanks.
June 23, 2004 - 11:41 am UTC
alter table t modify y constraint y_not_null not null;
Quick question.
Kashif, February 17, 2005 - 1:07 pm UTC
Hi Tom,
I've been trying to execute an import from a user-level export dump file into an empty schema, and for some reason the import is not creating not null constraints on tables, specifically the ones NOT associated with a primary key constraint. Of the top of your head, do you know if this is a known bug or something, or maybe a combination of parameters I'm not doing right? I'll post a complete example if I am not able to resolve this, but I thought I'd ask you to see if you knew something of the top of your head. Both the export and the import are done on version 9.2.0.5. Thanks and I'll post if I figure it out.
Kashif
February 17, 2005 - 2:07 pm UTC
not that I am aware of, do an imp userid=u/p full=y indexfile=foo.sql and see if they are there.
Thanks.
Kashif, February 17, 2005 - 6:06 pm UTC
Hi Tom,
Sorry for the false alarm, I was getting confused and didn't stop to think that I had pre-created those tables before doing the import, and that particular DDL script did not have the NOT NULL constraints on those columns... Silly me ;) Thanks and sorry for bothering you.
Kashif
(re)naming NOT NULL constraints -- thanks!
Duke Ganote, October 25, 2007 - 1:13 pm UTC
October 25, 2007 - 6:35 pm UTC
yes, that is one way to do it - a simple example is just that in the documentation. A simple example.
NOT NULL does give better quality plans
catmando, October 26, 2007 - 1:26 am UTC
As Tom, said, more information, better plans. This year I have seen big performance gains in a warehouse setting when dealing with NOT IN constructs with the appropriate NOT NULL constraints. I had a few queries that went from 1+ hours or never finishing to 1 second results. That's not true of all situations/queries, but repeatable on my side.
I believe it also opens up the FAST FULL index scan across NORMAL indexes. Check the manuals on that one.
I've never seen a performance penalty of significance for putting the constraint on.
Naming (or not) NOT NULL Constraints
Duke Ganote, October 26, 2007 - 11:28 am UTC
named not null constraint
Subrata Chattopadhya, March 19, 2010 - 1:41 pm UTC
Thanks for the named not null constraint syntex. I had a hard time finding it anywhere including Oracle manual.