MySQL自动增量如何工作? [英] How does MySQL Auto Increment work?
问题描述
我只是使用MySQL查询浏览器创建一个新表,并注意到在自动增量列下有一个勾。
当以数据库编程方式添加数据库时,我只需添加一个数字,然后数据库会自动增加该数字?
每当新用户在我的网站上注册,我想要他们的客户ID(仅整数)自动递增,因此我不必尝试并随机生成一个唯一的号码。
$谢谢!
在程序中添加数据库时,我只是添加一个数字,然后数据库自动增加该数字?
是的,这是 auto_increment
工作的方式。
如果某行被删除,则 auto_increment
最后一个插入行的 auto_increment
可以使用mySQL函数 LAST_INSERT_ID()
访问,但必须在插入查询之后调用,在同一数据库连接中
I was just creating a new table using MySQL Query Browser, and noticed there's a tick under Auto Increment Column. How does that work?
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Everytime a NEW user registers on my site, I want their Customer ID (integer only) to auto increment, so I don't have to try and randomly generate a unique number.
Can this be done simply?
Thank you!
When adding to the database programatically, do I just add a number, and then the database automatically increments that number?
Yes, that's the way auto_increment
works.
The value will be incremented for each new row
The value is unique, duplicates are not possible
If a row is deleted, the
auto_increment
column of that row will not be re-assigned.The
auto_increment
value of the last inserted row can be accessed using the mySQL functionLAST_INSERT_ID()
but it must be called right after the insert query, in the same database connection
这篇关于MySQL自动增量如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!