Since databases don’t offer "Are you sure?" dialogs, it’s best to be very certain of what you’re changing before you do it. UPDATE
and
DELETE
statements that don’t precisely limit their effects to single rows risk changing more than was intended. That’s why they should be
reviewed carefully.
This rule raises an issue when an UPDATE
or DELETE
statement’s WHERE
clause does not use precisely either a
unique index or all parts of the table’s primary key. That includes both cases where they are omitted in whole or in part, and when they are used but
could still describe multiple rows. E.G. WHERE AGE = 34
, and WHERE TABLE_ID > 0 AND TABLE_ID < 40
.
Note That this rule raises issues only when a database catalog is provided during the SonarQube analysis.
Noncompliant code example
CREATE table my_table (
compound_a integer not null,
compound_b integer not null,
column_c varchar(50),
primary key (compound_a, compound_b)
);
DELETE FROM my_table
WHERE compound_b=4; -- Noncompliant
Exceptions
Statements using a cursor and WHERE CURRENT OF
are ignored.