Greetings As I understand it, an increment by one with the auto_increment option occurs on any access to the table? and how to make the lines increment only when writing? Thanks in advance)

  • Not ... how could they be removed? I only use SELECT and INSERT with two commands. - alexsher

1 answer 1

In the normal case, auto_increment works only when adding a new record, if the value of the primary key is not explicitly specified.

If the primary key is specified explicitly and the value to be set is greater than the current value of auto_increment , then auto_increment recalculated (maximum value is + 1). The same thing happens when updating the value of the auto_increment -th key.

  • I do not explicitly ask. I only set the value to 1 in the base settings. The initial AUTO_INCREMENT value for table. - alexsher
  • Setting explicitly is if, for example, there is a table of two fields ( id and name ). id - auto-increment key. And a new entry is added through: INSERT INTO (id, name) VALUE (1, 'Bla-bla-bla'); And implicitly: INSERT INTO (name) VALUE ('Bla-bla-bla'); Or: INSERT INTO (id, name) VALUE (NULL, 'Bla-bla-bla'); In the second and third insertion variants, the id value is set by auto increment. - KiTE September
  • suppose I do so INSERT INTO (name) VALUE ('Bla-bla-bla'); incrementing and so SELECT (id) FROM table is also incremented. But can it be done so that in this case the id value does not change? - alexsher
  • @alexsher, immediately a series of questions arise. Zachev you then set the autoincrement on this field? Why it is impossible to use some other field as a key, where autoincrement is not needed? And in general, it is absolutely not necessary that in each table there is a field that is auto-incremented. It is possible that for your task just a unique field will fit and in the query to use the construction ON DUPLICATE KEY UPDATE - Deonis
  • @alexsher it is possible in more detail, about the increment at select)) I hear this for the first time. What exactly is incremented?) Increment is an increase in the value of the column of a new record by 1 relative to the last largest value of this record. How and what do you increase with selekt? - Ozim pm