MySQL self-increment ID gets
The
- uses the max function: select Max (ID) from tablename
- Advantages: Easy and quick to use.
- Disadvantage: Get not the real self-increment ID, is the largest ID in the table, if there is delete data, then this value and the self-increment ID is relatively large. If you have a table data, it can cause data confusion.
- uses the last_insert_id function: Select last_insert_id ()
- Pros: Gets the true self-increment ID.
- Disadvantage: The function is table-independent and always retains the ID of the newly inserted self-increment column. If the multi-client operation is not guaranteed to be atomic, this value may cause errors. So it needs to be called after the insert.
- uses the @ @IDENTITY variable: SELECT @ @IDENTITY
- This method is similar to the last_insert_id () function, with the same advantages and disadvantages. Needs to be called after insertion.
- uses the MySQL query function: SHOW TABLE status;
- Pros: Ability to accurately find the self-increment ID. You can also filter by adding a where statement or a like statement after the statement.
- Disadvantage: The statement returns a recordset and cannot return self-increment individually. So extra action is needed to get it.
- uses custom query methods: MySQL table-related information is placed in the INFORMATION_SCHEMA tables. So we refer to the SHOW TABLE status to build the query statement. such as:
- Select Auto_increment into Autoid from information_schema.tables where table_schema = ' dbname ' and table_name = ' tablename ';
- Pros: Highly customizable to accurately get any information you want.
MySQL self-increment ID gets