How does Mysql insert the same value as the self-increment ID?

Source: Internet
Author: User
Tags mysql insert
Suppose the database test table has 3 fields

id(自增)nameorder

Now insert a piece of data to make the order and ID values equal
I'm inserting a piece of data first.
Get to the inserted ID again
The update then sets the order to the ID value
There is no way to insert the ID and order equal

First select MAX (ID) from test
Perform the insert again?

Does this have to take two statements to get it done?

Reply content:

Suppose the database test table has 3 fields

id(自增)nameorder

Now insert a piece of data to make the order and ID values equal
I'm inserting a piece of data first.
Get to the inserted ID again
The update then sets the order to the ID value
There is no way to insert the ID and order equal

First select MAX (ID) from test
Perform the insert again?

Does this have to take two statements to get it done?

Your design is unreasonable, you can see that the order is used as a sort of field, and you want to have the room for manual adjustment, rather than simple design to sort by the primary key, then you can use it directly in the Select to order by `order`, `id` solve the problem, In this case, the Order field should have a default value, for example 10000, prioritize this field and sort by ID

And of course, remember to add the index

Off-topic, do not use the MySQL keyword as the field name, that is, order this type of name, proposed to change to a priority such as the name!

Can be implemented with MySQL's trigger

You can also use transactions to handle

Is it better to get the last inserted ID SET @lastid = LAST_INSERT_ID(); select max(id) from test ?

Think of a very wretched way is to set order also self-increment.

Why do you use two fields for a value?

Many of the answers upstairs are poorly thought out.

A table cannot set two self-increment fields.

The current generated ID cannot be obtained in the insert process, and can only be done with two statements. Two operations what's the problem?

  • 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.