开发者

Get last created ID with auto-increment in MySQL

开发者 https://www.devze.com 2023-04-08 06:57 出处:网络
I have a primary index on column URL but at the same time I need to have records with URL left blank which won\'t be permitted by the index.

I have a primary index on column URL but at the same time I need to have records with URL left blank which won't be permitted by the index.

I thought of a solution which is to add a new column called ID and make it auto-increment.

So I need to get the ID created with auto-increment on INSERT so that I stick it to column URL too. How can I do this?

If you can think of a bette开发者_JAVA技巧r solution please tell

thanks


It is almost certainly better to use the mysql wrapper in your scripting language of choice to acquire the last insert id, but you can SELECT LAST_INSERT_ID(); to get it with mysql itself.


The solution may depend on what other columns you have on your table. It will be hard for you to add an auto_increment column to a table that already has data. The process will be to create a new table with the new column added and add the data from the old table and then drop the old table and re-name the new one to the matching name.

0

精彩评论

暂无评论...
验证码 换一张
取 消