MySQL自动增量

发布于 2024-08-27 11:10:23 字数 228 浏览 6 评论 0原文

我有一个带有自动增量字段的表,但我需要将该表传输到另一个数据库上的另一个表。字段 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

酒浓于脸红 2024-09-03 11:10:23

听起来您正在尝试以相同的顺序将数据分别插入到两个单独的数据库中,并使用自动增量字段来链接两行。看来您基本上是在问,如果数据以相同的顺序插入,则可以依赖两个数据库中相同的自动增量吗?

如果是这样,答案是否定的——你不能依赖这种行为。自动增量跳过某个值是合法的,例如 参见此处< /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.

燃情 2024-09-03 11:10:23

如果您完全指定 auto_increment 值,传输数据不会有问题。 MySQL 允许您将任何内容插入到 auto_increment 字段中,但只有当您插入的值为 0 或 NULL 时,才会执行实际的 auto_increment。至少在我的 MySQL 5.0 副本上,它会自动调整 auto_increment 值以考虑您插入的内容:

mysql> create table test (x int auto_increment primary key);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test (x) values (10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test (x) values (null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test (x) values (0);   
Query OK, 1 row affected (0.00 sec)
mysql> insert into test (x) values (5);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+
| x  |
+----+
| 5  | <--inserted '5' (#4)
| 10 | <--inserted '10' (#1)
| 11 | <--inserted 'null' (#2)
| 12 | <--inserted '0' (#3)
+----+
3 rows in set (0.00 sec)

您还可以调整表的下一个 auto_increment 值,如下所示:

mysql> alter table test auto_increment=500;
Query OK, 4 rows affected (0.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into test (x) values (null);
Query OK, 1 row affected (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|              500 | 
+------------------+
1 row in set (0.01 sec)

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:

mysql> create table test (x int auto_increment primary key);
Query OK, 0 rows affected (0.01 sec)
mysql> insert into test (x) values (10);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test (x) values (null);
Query OK, 1 row affected (0.00 sec)
mysql> insert into test (x) values (0);   
Query OK, 1 row affected (0.00 sec)
mysql> insert into test (x) values (5);
Query OK, 1 row affected (0.00 sec)
mysql> select * from test;
+----+
| x  |
+----+
| 5  | <--inserted '5' (#4)
| 10 | <--inserted '10' (#1)
| 11 | <--inserted 'null' (#2)
| 12 | <--inserted '0' (#3)
+----+
3 rows in set (0.00 sec)

You can also adjust the table's next auto_increment value as follows:

mysql> alter table test auto_increment=500;
Query OK, 4 rows affected (0.04 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> insert into test (x) values (null);
Query OK, 1 row affected (0.00 sec)

mysql> select last_insert_id();
+------------------+
| last_insert_id() |
+------------------+
|              500 | 
+------------------+
1 row in set (0.01 sec)
谜兔 2024-09-03 11:10:23

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

三生路 2024-09-03 11:10:23

使用 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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文