Hello. I have a database in which columns change from time to time (added, deleted). A php script is committed to doing UPDATE commands. So, I basically need this: if there is a column and a record, they are updated, but if they are not there, then nothing happens.

Actually the question is: is it possible to access the database without checking, so that if there is no column, then an error would occur and not to react to it? Or do you need to do checks? Then there will be no mistakes, but there will be performance costs. How is more correct?

  • In typical cases, RDBMS columns are assumed to be known in advance, and the application operates with the existence of records. Is your data badly normalized? - D-side
  • 3
    In the event of an error, at a minimum, you need to make sure that this is exactly the error you were expecting (lack of a column, not a lack of a tiblice, not a transaction error, not a restrict on the associated field). - Arnial
  • How often do you change the columns in the tables and for what? - Arnial
  • @Arnial frequency of changes may be different. How much the user wishes, so much and change ... - Nik
  • @ D-side the situation is this: my php daemon receives data from sites once every 5 minutes, checks them and puts them into the database if they fit the condition. The columns in the database are sites. The user can at any time add or delete a site. Although the daemon bypasses only those sites that are in the database, I consider the case when a user deleted a site at a time when the demon is in the middle of execution — then we can turn to a nonexistent column. - Nik

2 answers 2

The question is rather strange. As the D-side said in a normal situation, the columns rarely change.

Is it possible to access the database without checking (they mean pre-checks)

Yes, it is possible and even desirable. Verifying that the column exists does not guarantee that it will exist at the time of the update request (or that the connection to the database will not fall off immediately after the check).

But, it is always necessary to check whether an error occurred in the request (server response). Errors in queries can occur for many different reasons, from an error in the query itself (sql syntax, non-existent field ...) to disk failure or lack of space ( see error codes ). If you know that sometimes there can be an error in this request and this is normal, then you should check that the request failed because of the planned error.

    In general, I think it is necessary to make a start not from the script but from the list of columns in the table. Well, for example. We analyze the table and then we form the necessary script for us depending on the conditions.