Application Development. Should I check the primary key on the table or assume that it should be there?

When creating an application and using a table with a primary key, you should check whether the table has a primary key or if there are no duplicate identifiers?

I came across some code that I support, checking that there are no duplicate identifiers in the result set. But the verified identifier is the primary key. Therefore, I do not need this check, since you cannot have primary keys with the same value.

But ... should this be checked if the DBA disconnected the primary key in the table for any reason or suggested that the primary key should always be there?

+3
source share
10 answers

Make sure that the query actually returns data only from the table using the primary key. If this table is joined to another table in the query and is not a one-to-one relationship, this may result in the return of several rows that have the same identifier in the primary table. In this case, checking the code for duplicates can actually do something valuable.

While this is not the case, remove the code that checks for duplicates. This is a waste of CPU and memory cycles to ensure that the database is doing its job.

+3
source

, . , , , (, ).

+3

, , , . . , , .

- . , , .

+3

, , . , , , , ... .

- , ? , , ( ) .

+2

, sql, . . , , .

+1

, RDBMS. , , - .

0

, . , , .

DBA .

0

, . , - .

, - , , db.

0

, , , . , . , , PK , .

0

It usually makes sense to check the version of the circuit during installation, rather than at runtime. One way to do this is to save the version number of the schema in the database and check the installation time, this is what the application expects. In any case, updating the schema may be part of the installation.

For a production application, a change management process is also usually implemented so that any changes (whether application or database) are regressed before release.

0
source

Source: https://habr.com/ru/post/1761755/


All Articles