如何为多个表中的行实现唯一的自动递增的id?

编程入门 行业动态 更新时间:2024-10-14 00:27:04
本文介绍了如何为多个表中的行实现唯一的自动递增的id?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我在数据库中有几个表,假设它们被称为table1,table 2等. 所有表都有一个具有自动增量的主键列"id".

I've got several tables in a database, let's say they're called table1, table 2, etc. All tables have a primary key column 'id' with auto-increment.

在我当前的配置中,碰巧插入表1时,生成的ID为1. 然后,当插入table2时,生成的ID也恰好是1.

In my current configuration it happens that when inserting into table1, the generated id is 1. Afterwards when inserting into table2, the generated id happens to be 1 as well.

如何在数据库的所有表中强制使用绝对唯一的ID?我想在插入table1时生成的ID为1,然后再插入到table2中时生成的ID为2?

How to force absolutely unique ids across all tables in a database? I want when inserting into table1, generated id to be 1, and if afterwards inserting into table2, generated id be 2?

我在某些计算机上使用了mysql服务器,但没有出现此问题,但是当我在本地计算机上安装mysql时,它开始出现.所以我想这一定是某种适用于mysql配置的设置吗?

I used mysql server on some machine and did not have this problem, but when I installed mysql on my local machine, it started to occur. So I guess it must be some kind of a setting that is applied to the mysql configuration?

谢谢

推荐答案

,您可以使用UUID.

you can use UUID.

INSERT INTO mytable(id, name) VALUES(SELECT UUID(), 'some data');

了解有关UUID的更多信息: mysqlbackupnet .codeplex/wikipage?title =使用%20MySQL%20With%20GUID%20or%20UUID

Read more about UUID: mysqlbackupnet.codeplex/wikipage?title=Using%20MySQL%20With%20GUID%20or%20UUID

更多推荐

如何为多个表中的行实现唯一的自动递增的id?

本文发布于:2023-11-28 15:10:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1642908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   何为   id

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!