MySQL self-increment ID gets

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.