I'm designing a new physical data model using a third party tool. When I generate the database creation script automatically it puts 'not null' constraints on the primary keys. I don't have much experience with this particular tool and can't find the correct setting to stop this.
The extra 'not null' constraints are redundant but what would be the problem if I just leave them? For example, would the 'not null' condition be checked twice for every insert or update?
You cannot alter table column to not null or null if that column is a PRIMARY key. It is by default a not null if it has a primary key constraint on it.
Amar "There is a difference between knowing the path and walking the path."
SQL> create table my_table (my_column number primary key);
Table created.
SQL> desc my_table
Name Null? Type
----------------------------------------- -------- ----------------------
MY_COLUMN NOT NULL NUMBER
SQL> alter table my_table modify (my_column not null);
Table altered.
SQL> alter table my_table modify (my_column null);
Table altered.
SQL> alter table my_table modify (my_column null);
alter table my_table modify (my_column null)
*
ERROR at line 1:
ORA-01451: column to be modified to NULL cannot be modified to NULL
SQL> desc my_table
Name Null? Type
----------------------------------------- -------- -------------------
MY_COLUMN NOT NULL NUMBER
Note that it is the second modify to not null that fails, but not because o the PK definition ...
SQL> drop table my_table;
Table dropped.
SQL> create table my_table (my_column number null);
Table created.
SQL> alter table my_table modify (my_column null);
alter table my_table modify (my_column null)
*
ERROR at line 1:
ORA-01451: column to be modified to NULL cannot be modified to NULL
I just thought of a reason why a designer might want to keep the not null on there actually.
Suppose you were defining a table for which the PK had to be deferrable -- you might want the "unique" part of the PK to be deferrable, but keep the "not null" part enforced. In such a case you would have to keep a seperate not null constraint on the table as well as the deferrable PK.
Probably not a very common case, I would think. The only situation i can think of for having a deferrable pk would be on a fast-refresh materialized view -- i believe that deferrable pk's are a benefit there because Oracle does not guarantee uniqueness on a PK or Unique column list while the refresh is taking place.
Bookmarks