如何在mysql中设计一个具有两个自动增量字段的表

发布于 2024-10-01 06:10:36 字数 529 浏览 1 评论 0原文

对于一个项目,我们目前正在进行数据库设计。我们认为应该在一张表中使用两个 auto_increment 字段。

table master:

`pid` int(10) NOT NULL auto_increment,
`iid` int(10) NOT NULL auto_increment,
...

要从备用 auto_incremet 开始,您可以使用 ALTER TABLE tbl AUTO_INCRMENT = 100000; 这只适用于整个表“tbl”。 pid 的 auto_increment 应该是 50000000,iid 的 auto_increment 应该是 80000000

我们希望避免将其分成 3 个表,关系为 master ->; table.pid 和 master ->表.iid。

更改表不起作用的原因 /* SQL 错误 (1075):表定义不正确;只能有一个自动列,并且必须将其定义为键 */

是否可能或您推荐什么替代方案?

For a project we do the database design at the moment. We think we should use two auto_increment fields in one table.

table master:

`pid` int(10) NOT NULL auto_increment,
`iid` int(10) NOT NULL auto_increment,
...

To start with a alternate auto_incremet you can use ALTER TABLE tbl AUTO_INCREMENT = 100000; This will work only for the whole table 'tbl'.
auto_increment for pid should be 50000000 and auto_increment for iid should be 80000000

We want to avoid splitting it into 3 tables with relations master -> table.pid and master -> table.iid.

altering the table is not working cause
/* SQL Error (1075): Incorrect table definition; there can be only one auto column and it must be defined as a key */

Is it possible or what alternative do you recommend?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

享受孤独 2024-10-08 06:10:36

如果您不能使用两个自动列,我认为您必须重新设计数据库。你到底需要什么?

If you cannot use two auto columns I think you must redesign your database. What do you need exactly?

红尘作伴 2024-10-08 06:10:36

我不完全理解你的问题,但你可以使用触发器来维护关键值,如下所示:

drop table if exists grid;
create table grid
(
grid_id int unsigned not null auto_increment primary key,
name varchar(255) not null,
next_token_id int unsigned not null default 0,
next_node_id int unsigned not null default 0
)
engine = innodb;

drop table if exists grid_token;
create table grid_token
(
grid_id int unsigned not null,
token_id int unsigned not null,
name varchar(255) not null,
primary key (grid_id, token_id) -- note clustered PK order (innodb only)
)
engine = innodb;

drop table if exists grid_node;
create table grid_node
(
grid_id int unsigned not null,
node_id int unsigned not null,
name varchar(255) not null,
primary key (grid_id, node_id) -- note clustered PK order (innodb only)
)
engine = innodb;

-- TRIGGERS

delimiter #

create trigger grid_token_before_ins_trig before insert on grid_token
for each row
begin

declare tid int unsigned default 0;

  select next_token_id + 1 into tid from grid where grid_id = new.grid_id;
  set new.token_id = tid;
  update grid set next_token_id = tid where grid_id = new.grid_id;

end#

create trigger grid_node_before_ins_trig before insert on grid_node
for each row
begin

declare nid int unsigned default 0;

  select next_node_id + 1 into nid from grid where grid_id = new.grid_id;
  set new.node_id = nid;
  update grid set next_node_id = nid where grid_id = new.grid_id;

end#

delimiter ;

-- TEST DATA

insert into grid (name) values ('g1'),('g2'),('g3');

insert into grid_token (grid_id, name) values
(1,'g1 t1'),(1,'g1 t2'),(1,'g1 t3'),
(2,'g2 t1'),
(3,'g3 t1');

insert into grid_node (grid_id, name) values
(1,'g1 n1'),(1,'g1 n2'),
(2,'g2 n1'),
(3,'g3 n1'),(3,'g3 n2');

select * from grid;
select * from grid_token;
select * from grid_node;

I dont fully understand your question but you can use triggers to maintain key values like the following:

drop table if exists grid;
create table grid
(
grid_id int unsigned not null auto_increment primary key,
name varchar(255) not null,
next_token_id int unsigned not null default 0,
next_node_id int unsigned not null default 0
)
engine = innodb;

drop table if exists grid_token;
create table grid_token
(
grid_id int unsigned not null,
token_id int unsigned not null,
name varchar(255) not null,
primary key (grid_id, token_id) -- note clustered PK order (innodb only)
)
engine = innodb;

drop table if exists grid_node;
create table grid_node
(
grid_id int unsigned not null,
node_id int unsigned not null,
name varchar(255) not null,
primary key (grid_id, node_id) -- note clustered PK order (innodb only)
)
engine = innodb;

-- TRIGGERS

delimiter #

create trigger grid_token_before_ins_trig before insert on grid_token
for each row
begin

declare tid int unsigned default 0;

  select next_token_id + 1 into tid from grid where grid_id = new.grid_id;
  set new.token_id = tid;
  update grid set next_token_id = tid where grid_id = new.grid_id;

end#

create trigger grid_node_before_ins_trig before insert on grid_node
for each row
begin

declare nid int unsigned default 0;

  select next_node_id + 1 into nid from grid where grid_id = new.grid_id;
  set new.node_id = nid;
  update grid set next_node_id = nid where grid_id = new.grid_id;

end#

delimiter ;

-- TEST DATA

insert into grid (name) values ('g1'),('g2'),('g3');

insert into grid_token (grid_id, name) values
(1,'g1 t1'),(1,'g1 t2'),(1,'g1 t3'),
(2,'g2 t1'),
(3,'g3 t1');

insert into grid_node (grid_id, name) values
(1,'g1 n1'),(1,'g1 n2'),
(2,'g2 n1'),
(3,'g3 n1'),(3,'g3 n2');

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