Skip to content

Commit f12133a

Browse files
committed
Document ability to drop not null constraints
1 parent 32c0df3 commit f12133a

File tree

1 file changed

+32
-9
lines changed

1 file changed

+32
-9
lines changed

src/docs/asciidoc/en/firebirddocs/nullguide/firebird-null-guide.adoc

Lines changed: 32 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2085,6 +2085,18 @@ etc. -- see <<nullguide-add-check-not-null-field>>.
20852085
==== Removing a `NOT NULL` constraint
20862086
(((NOT NULL,remove)))
20872087

2088+
In Firebird 3.0 and higher, you can drop a `NOT NULL` constraint from a table using:
2089+
2090+
[source]
2091+
----
2092+
alter table Adventures alter ID drop not null;
2093+
----
2094+
2095+
If the `ID` column is based on a domain which also has a `NOT NULL` constraint, the column will still be constrained.
2096+
You will need to drop the `NOT NULL` constraint from the domain as well to make the column really nullable, see the next section.
2097+
2098+
For Firebird 2.5 and earlier, dropping the `NOT NULL` constraint is more involved.
2099+
20882100
If you gave the `NOT NULL` constraint a name when you created it, you can simply drop it:
20892101

20902102
[source]
@@ -2109,15 +2121,13 @@ where rc.rdb$constraint_type = 'NOT NULL'
21092121
and cc.rdb$trigger_name = '<FieldName>'
21102122
----
21112123

2112-
Don't break your head over some of the table and field names in this statement;
2113-
they are illogical but correct.
2114-
Make sure to uppercase the names of your table and field if they were defined case-insensitively.
2124+
Make sure to uppercase the names of your table and field if they were defined case-insensitively (that is, as a regular identifier).
21152125
Otherwise, match the case exactly but don't enclose the names in double-quotes like you would do in a regular query.
21162126
Also don't include the angle brackets (`<>`). Once you have the constraint name, you can drop it just like in the previous example.
21172127

21182128
[TIP]
21192129
====
2120-
If the above statement returns an empty set and you are sure that you've correctly filled in the table and field names (including case!), and the constraint did not come from a domain either (this is discussed in the next sections), it may be that a third-party tool has made the column `NOT NULL` by setting a flag in a system table.
2130+
If the above statement returns an empty set, and you are sure that you've correctly filled in the table and field names (including case!), and the constraint did not come from a domain either (this is discussed in the next sections), it may be that a third-party tool has made the column `NOT NULL` by setting a flag in a system table.
21212131
In that case it's probably best to remove it again with the same tool.
21222132
If that is not an option, check the field's `NULL` flag with:
21232133
@@ -2155,7 +2165,7 @@ However, before you can _insert_ ``NULL``s into the column, you must commit your
21552165

21562166
If the `NOT NULL` constraint came with a domain, it is not registered directly with the column.
21572167
This means you can't `DROP` it from the column either.
2158-
Instead, change the column's type to a nullable domain or built-in data type:
2168+
You can either drop it from the domain as well -- Firebird 3.0 and higher only -- or change the column's type to a nullable domain or built-in data type:
21592169

21602170
[source]
21612171
----
@@ -2244,7 +2254,19 @@ You must close all connections and reconnect before you can insert values that w
22442254
[[nullguide-change-domain-notnull]]
22452255
=== Altering a domain's `NOT NULL` setting[[d0e25188]]
22462256

2247-
Once a domain is created, Firebird doesn't allow you to add or remove a `NOT NULL` constraint (`DROP CONSTRAINT` will only drop a `CHECK`).
2257+
In Firebird 3.0 and higher, you can drop the `NOT NULL` constraint from a domain using:
2258+
2259+
[source]
2260+
----
2261+
alter domain MyDomain drop not null;
2262+
----
2263+
2264+
[CAUTION]
2265+
====
2266+
Dropping the `NOT NULL` constraint from the domain affects all columns based on that domain, if they don't have an explicit `NOT NULL` constraint of their own.
2267+
====
2268+
2269+
Firebird 2.5 and earlier don't allow you to add or remove a `NOT NULL` constraint from an existing domain (`DROP CONSTRAINT` will only drop a `CHECK`).
22482270
If you want to change a domain-wide `NOT NULL` setting, the official procedure is:
22492271

22502272
. Create a new domain with the desired characteristics.
@@ -2253,7 +2275,7 @@ If you want to change a domain-wide `NOT NULL` setting, the official procedure i
22532275

22542276
This is fine when it only concerns a few columns, but what if there are dozens or even hundreds?
22552277
It is possible to change the setting by going directly to the system table.
2256-
Be aware however that Firebird does _not_ recommend or support this type of operation, nor is it guaranteed to keep working in future versions.
2278+
Be aware however that Firebird does _not_ recommend or support this type of operation, and it is disallowed in Firebird 3.0 and higher.
22572279
That being said, it's a relatively simple operation and if properly executed it shouldn't cause you any problems.
22582280

22592281
So here's the SQL, but remember: at your own risk!
@@ -2270,7 +2292,7 @@ To remove a `NOT NULL` constraint, use `0` or `NULL`.
22702292
Write the domain name in all-caps if it was created case-insensitively;
22712293
otherwise, match the case exactly.
22722294
Don't use double-quotes and don't include the '```<>```'.
2273-
Also note that, even when DDL autocommit is on (which is the default in `isql` and many other clients), this statement won't be autocommitted because technically it's not DDL.
2295+
Also note that, even when DDL autocommit is on (which is the default in `isql` and many other clients), this statement won't be auto-committed because technically it's not DDL.
22742296
So don't forget to commit!
22752297

22762298
If you have set the flag to `1`, a subsequent `SHOW DOMAIN` will immediately report the domain as being `NOT NULL`.
@@ -2284,7 +2306,7 @@ If you have changed the flag from `1` to `0` or `NULL` -- making the domain null
22842306
But you still can't insert ``NULL``s in the columns, and any present ``NULL``s are still shown as zeroes (in most clients).
22852307
Close all connections and reconnect to straighten everything out.
22862308

2287-
Lastly, please be warned again that this type of fiddling with the system tables is not recommended or supported by Firebird, and not guaranteed to keep working in future versions.
2309+
Lastly, please be warned again that this type of fiddling with the system tables is not recommended or supported by Firebird, and no longer works in Firebird 3.0 and higher.
22882310
If the number of columns based on the domain is limited, it's better to switch them over to another domain or built-in type and then drop the old domain.
22892311

22902312
[[nullguide-testing]]
@@ -2757,6 +2779,7 @@ The exact file history is recorded in the firebird-documentation git repository;
27572779
|TBD
27582780
|MR
27592781
a|* Reordered revision history so most recent revision is at the top
2782+
* Document Firebird 3.0 ability to drop not null constraints in _Making non-nullable columns nullable again_
27602783

27612784
|1.2
27622785
|30 Jun 2020

0 commit comments

Comments
 (0)