SQL - Inserting a row and returning primary key

前端 未结 8 1166
南笙
南笙 2020-12-01 18:07

I have a little witty problem. Say, I inserted a row with some data in a table where a primary key is present. How would one \"SELECT\" the primary key of the row one just i

8条回答
  •  悲&欢浪女
    2020-12-01 18:21

    For MySQL, use LAST_INSERT_ID()

    http://dev.mysql.com/doc/refman/5.0/en/getting-unique-id.html

    You should also be able to start a transaction, insert the row, and select the row using some field that has a unique value that you just inserted, like a timestamp or guid. This should work in pretty much any RDBMS that supports transactions, as long as you have a good unique field to select the row with.

提交回复
热议问题