Solution: Strictly Follow Referencing Rules
Let’s find out the ways to clear out the Pseudo key Neat-Freak antipattern.
We'll cover the following
The values in any primary key must be unique and non-null so we can use them to reference individual rows, but that’s the only rule — they don’t have to be consecutive numbers to identify rows.
Numbering rows
Most pseudo key generators return numbers that almost look like row numbers because they’re monotonically increasing (that is, each successive value is one greater than the preceding value), but this is only a coincidence of their implementation. Generating values in this way is a convenient way to ensure uniqueness.
We mustn’t confuse row numbers with primary keys. A primary key identifies one row in one table, whereas row numbers identify rows in a result set. Row numbers in a query result set don’t correspond to primary key values in the table, especially when we use query operations like JOIN
, GROUP BY
, or ORDER BY
.
There are good reasons to use row numbers, for example, to return a subset of rows from a query result. This is often called pagination, like in the pages resulting from an Internet search. To select a subset in this way, we need to use true row numbers that are increasing and consecutive, regardless of the form of the query.
SQL:2003 specifies “window functions”, which include ROW_NUMBER()
. This returns consecutive numbers that are specific to a query result set. A common use of row numbering is to limit the query result to a range of rows:
Get hands-on with 1400+ tech skills courses.