MySQL自动增量
我有一个带有自动增量字段的表,但我需要将该表传输到另一个数据库上的另一个表。字段 1 的值为 1,字段 2 的值为 2,等等?
另外,如果数据库损坏并且我需要恢复数据,自动增量会以某种方式起作用吗?值会改变吗? (例如,如果第一行,id(auto-inc)= 1,name = john,country = UK .... id字段将保持为1吗?)我问是因为如果其他表引用该值,则所有数据都会如果该字段发生变化,就会失去同步。
I have table with an auto-increment field, but I need to transfer the table to another table on another database. Will the value of field 1 be 1, that of field 2 be 2, etc?
Also in case the database get corrupted and I need to restore the data, will the auto-increment effect in some way? will the value change? (eg if the first row, id (auto-inc) = 1, name = john, country = UK .... will the id field remain 1?) I am asking because if other table refer to this value, all data will get out of sync if this field change.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
听起来您正在尝试以相同的顺序将数据分别插入到两个单独的数据库中,并使用自动增量字段来链接两行。看来您基本上是在问,如果数据以相同的顺序插入,则可以依赖两个数据库中相同的自动增量吗?
如果是这样,答案是否定的——你不能依赖这种行为。自动增量跳过某个值是合法的,例如 参见此处< /a>.
但也许你会问,一个自增值在写入并提交后会突然变成另一个值吗?不 - 它们将来不会改变(当然除非你明确地改变它们)。
这能回答你的问题吗?如果没有,也许你可以再次解释你的问题。
It sounds like you are trying to separately insert data into two separate databases in the same order, and using the auto-increment field to link the two rows. It seems you are basically asking, is it OK to rely on the auto-increment being the same in both databases if the data is inserted in the same order.
If so, the answer is no - you cannot rely on this behaviour. It is legitimate for the auto-increment to skip a value, for example see here.
But maybe you are asking, can an auto-increment value suddenly change to another value after it is written and committed? No - they will not change in the future (unless of course you change them explicitly).
Does that answer your question? If not, perhaps you can explain your question again.
如果您完全指定 auto_increment 值,传输数据不会有问题。 MySQL 允许您将任何内容插入到 auto_increment 字段中,但只有当您插入的值为 0 或 NULL 时,才会执行实际的 auto_increment。至少在我的 MySQL 5.0 副本上,它会自动调整 auto_increment 值以考虑您插入的内容:
您还可以调整表的下一个 auto_increment 值,如下所示:
Transferring the data wouldn't be a problem, if you completely specify the auto_increment values. MySQL allows you to insert anything you want into an auto_increment field, but only does the actual auto_increment if the value you're inserting is 0 or NULL. At least on my 5.0 copy of MySQL, it'll automatically adjust the auto_increment value to take into account what you've inserted:
You can also adjust the table's next auto_increment value as follows:
SELECT INTO 应在目标表上保留相同的 ID
http://dev.mysql.com/doc/refman/5.1/en/ansi-diff-select-into-table.html
SELECT INTO should keep the same ids on target table
http://dev.mysql.com/doc/refman/5.1/en/ansi-diff-select-into-table.html
使用 MySQL 备份可以做到这一点,如果您创建自己的插入语句,请确保包含您的 id 字段并且将插入该值(它不像 MSSQL,您必须设置identity_insert),需要注意的是,如果您生成 DDL 它有时会“错误地”为您的身份列生成(即,它声明起点是您的最后一个身份值?您可能不希望出现这种行为)。
Using MySQL backup will do this, if you create your own insert statements make sure that you include your id field and that will insert the value (its not like MSSQL where you have to set identity_insert), a thing to watch for is that if you generate a DDL it sometimes generates "incrorectly" for your identity column (i.e. it states that starting point is at your last identity value? you may not want this behaviour).