From Newsgroup: comp.databases.theory
On 2020-01-09, Derek Ignatius Asirvadem <
derek.asirvadem@gmail.com> wrote:
now proceed with the
ALTER TABLE Node ADD CONSTRAINT ...
one of which is:
ALTER TABLE Node
ADD CONSTRAINT Node_NE_Parent_ck CHECK (
NodeNo <> ParentNodeNo
)
Now, wait. Does Sybase really accept that? Regardless of the answer,
that *is* anti-logical:
- Declared constraint in table definition: NodeNo <> ParentNodeNo
- Current instance of the table:
Nodeno ParentNodeNo ...
0 0 ...
[...]
The system must reject the addition of a constraint that the current
instance violates, period.
Instead of telling me what the genuine SQL platforms SHOULD do, from
your perspective, based on experience with an anti-SQL piece of dog's
meat, why don't you find out (a) what SQL requires, and (b) why we
might want the operation that the SQL platforms do.
ISO SQL:2011 (the draft I have at hand), -o4.18.3.4:
"A table check constraint is satisfied if and only if the specified
<search condition> evaluates to True or Unknown *for every row* of the
table to which it applies." [emphasis mine]
FYI, you can do what you describe in PostgreSQL as well, but better:
alter table Node
add constraint Node_NE_Parent_ck
check (NodeNo <> ParentNodeNo) NOT VALID;
The default is sane (if you add a constraint, usually you want every
instance to conform to it), but you may skip validation with NOT VALID.
No performance penalty, as you ask: the constraint will be enforced only
for subsequent inserts or updates.
Second, the constraint is marked as such in the table definition, e.g.:
[table definition here]
Check constraints:
"Node_NE_Parent_ck" CHECK (NodeNo <> ParentNodeNo) NOT VALID
so you know that it is not necessarily satisfied by each row.
Third, if you want you may validate it at any subsequent time with:
alter table Node validate constraint Node_NE_Parent_ck;
In your example, that would get you an error because of the first
insertion:
ERROR: check constraint "Node_NE_Parent_ck" is violated by some row
and the constraint will remain NOT VALID.
Granted, there are use cases when this feature comes in handy, like
adding a constraint to a huge table. But the idea is that you'd skip the constraint because you *know* that it's valid for the existing data.
There are exceptions, of course ("hey, the codes from now on will be
only numeric; but the alphanumeric codes are still in use and should not
be changed"). But if that feature can be avoided, IMO it's better to
avoid it.
In your example, this would be a cleaner solution:
alter table Node
add constraint Node_NE_Parent_ck
check (NodeNo <> ParentNodeNo or NodeNo = 0);
This could be added before inserting any data and it *declares*
explicitly that we don't care about node 0.
Nicola
--- Synchronet 3.21d-Linux NewsLink 1.2