SQL Insert Row and copy inserted Auto-Increment id to another column

久未见 提交于 2019-12-23 03:07:31

问题


I am using PHP and MySQL.

Goal:

Insert a new row into a table with an auto-increment id column.
While inserting this new row, I would like to copy the new inserted row's auto-increment id # into another column in the same row/entry.
Upon creation, the new entry/row should have the auto-incremented id column and another column with the same number.

My biggest concern:
I need all of this to happen correctly and reliably, even while other transactions from other connections could be taking place on the server.

So Far

I know only a little about last_insert_id()... and I am afraid of being able to use this reliably for my needs...
Will I ever run into a situation where the auto-increment id # will have already incremented (due to some other insert query from another connection perhaps) and now I will not get the correct id #? (I did not quite fully understand what it means when the last_insert_id() is given to the client on a per-connection basis).

Will last_insert_id() play nice with transactions since they become undefined when the transaction is made to rollback? (If another transaction is rolled back and then I run this script immediately after, will my script return NULL for last_insert_id()?)

I do not understand mysql_insert_id(); how to use it and whether or not it will help me.

So far, I have thought about:

  1. INSERT row with column set as last_insert_id();
  2. INSERT row; UPDATE column with SELECT last_insert_id();
  3. SELECT last_insert_id(); INSERT row with auto-increment column and column set as last_insert_id()+1

What happens when I insert a chosen value into the auto-increment column? Will the auto-increment generator start counting from the number I insert? What if I use a value that has been used before (but doesn't exist anymore) and there exists records with id # that come after that value?

Will table or row locking allow me to achieve my desired behavior?

What is the proper/correct way to do something like this?



"last_insert_id() is given to the client on a per-connection basis"

last_insert_id is client independent and will return the ID for the last inserted row from that client, therefore you do not need to worry about the case that a user on another connection transacts with the database.

I still do not fully understand what that means...

For a basic SCENARIO:

INSERT row where id = 1;
SELECT last_insert_id(); outputs 1

Person A makes a connection to the db; SELECT last_insert_id(); outputs 1.
Person B makes a connection to the db; SELECT last_insert_id(); outputs 1?

Person A INSERT another row where id = 2;

Person A SELECT last_insert_id(); outputs 2?
Person B SELECT last_insert_id(); outputs... 1 or 2??

What happens here?


And a SCENARIO that really concerns me:

Person A makes a connection with the db;
Person A SELECT last_insert_id(); outputs 1

Person A INSERT row where id = 2;
Person A SELECT last_insert_id(); outputs 2

Person B makes a connection with the db;
Person B SELECT last_insert_id(); outputs 2
Person B INSERT row where id = 3;

Person A SELECT last_insert_id(); outputs 2??
Person B SELECT last_insert_id(); outputs 3??

In this case, Person A's last_insert_id() is one count behind. If this is true, then I will not be able to use my #3 method.


Please correct my outputs for me wherever I may be wrong.


回答1:


You should check out this article regarding MySQL last_insert_id

last_insert_id is client independent and will return the ID for the last inserted row from that client, therefore you do not need to worry about the case that a user on another connection transacts with the database.




回答2:


considering you may have multiple access to your DB,

i will do:

1, just insert new row and put null into the "another column" let's call it [autoID_Copy]

2, then i can run this:

update table set autoID_Copy= autoID where autoID_Copy is null 

even make it faster you may do where timeInstered < 1 min or maybe some other filter.

hope this help.



来源:https://stackoverflow.com/questions/28485117/sql-insert-row-and-copy-inserted-auto-increment-id-to-another-column

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!