如何为多个表中的行实现唯一的自动递增的id? [英] How to achieve unique auto-incremented id for rows across multiple tables?

查看:136
本文介绍了如何为多个表中的行实现唯一的自动递增的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的更多信息: http://mysqlbackupnet .codeplex.com/wikipage?title =使用%20MySQL%20With%20GUID%20or%20UUID

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

这篇关于如何为多个表中的行实现唯一的自动递增的id?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆