How does Mysql Insert the same value as the auto-increment id?

Source: Internet
Author: User
Tags mysql insert
Assume that the database test table has three fields {code ...} now we want to insert a piece of data so that the values of order and id are equal. I want to insert a piece of data first and then get the inserted id. Then, update sets order to the id value. Is there any way to insert it? equal order or selectmax (... assume that the database test table has three fields.

Id (auto-increment) nameorder

Insert a data record to make the values of order and id equal.
Insert a data entry first.
Obtain the inserted id.
Then, update sets order to id.
Is there a way to make the id and order equal when inserting

Select max (id) from test first
Then insert?

In this case, must two statements be used to solve the problem?

Reply content:

Assume that the database test table has three fields.

Id (auto-increment) nameorder

Insert a data record to make the values of order and id equal.
Insert a data entry first.
Obtain the inserted id.
Then, update sets order to id.
Is there a way to make the id and order equal when inserting

Select max (id) from test first
Then insert?

In this case, must two statements be used to solve the problem?

Your design is unreasonable. We can see that your order is used as a sorting field and you want to adjust it manually, instead of simply designing it to sort by the primary key, in this case, you can directly useorder by `order`, `id` This problem is solved. In this case, the order field can be sorted by a default value, for example, 10000. This field is prioritized and then sorted by id.

Remember to add indexes.

In other words, do not use the keyword of mysql as the field name, that is, the name such as order. We recommend that you change it to a name such as priority!

Mysql triggers can be used for implementation.

You can also use transactions for processing.

Obtain the id of the last insert,SET @lastid = LAST_INSERT_ID();Is it betterselect max(id) from testGood?

I thought of a very tricky way to set order as auto-increment.

Why two fields are used for a value?

There are many questions on the top of the page.

Two auto-increment fields cannot be set for a table.

You cannot obtain the id generated during the insert Process. You can only use two statements. Are there any problems with the two operations?

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.