Adding a CHECK
ed column
Using a CHECK
constraint is another way to disallow NULL
entries in a column:
alter table Adventures add id int check (id is not null)
If you do it this way, a subsequent SELECT
will return:
Name | Bought | Price | ID |
---|---|---|---|
Maniac Mansion |
12-Jun-1995 |
$ 49,-- |
<null> |
Zak McKracken |
9-Oct-1995 |
$ 54,95 |
<null> |
Well, at least now you can see that the fields are NULL
!Firebird does not enforce CHECK
constraints on existing rows when you add new fields.The same is true if you add checks to existing fields with ADD CONSTRAINT
or ADD CHECK
.
This time, Firebird not only tolerates the presence and the backing up of the NULL
entries, but it will also restore them.Firebird’s gbak
tool does restore CHECK
constraints, but doesn’t apply them to the existing data in the backup.
Note
|
Even with the |
This restorability of your NULL
data despite the presence of the CHECK
constraint is consistent with the fact that Firebird allows them to be present in the first place, and to be backed up as well.But from a pragmatical point of view, there’s a downside: you can now go through cycle after cycle of backup and restore, and your “illegal” data will survive without you even receiving a warning.So again: make sure that your existing rows obey the new rule immediately after adding the constrained column.The “default” trick won’t work here;you’ll just have to remember to set the right value(s) yourself.If you forget it now, chances are that your outlawed NULL
s will survive for a long time, as there won’t be any wake-up calls later on.
Note
|
The Likewise, the engine describes these columns as nullable when a query is executed.This accounts for the fact that |