0

我有桌子:

user(org_id, user_id, email, password)
organisation(org_id, org_name)

我希望 an 中的所有用户都organisation以用户 ID 1 开始,并在每次添加另一个用户时增加一个。因此,我可以在一个组织中拥有用户 1、用户 2、用户 3,而在另一个组织中拥有用户 1、用户 2、用户 3 organisation

在几年前的 MySQL 以前的版本中,我是这样做的:

create table users (
  org_id int unsigned, 
  user_id int unsigned auto_increment, 
  email varchar(100), 
  primary key (org_id, user_id), 
  unique key (email, org_id)
);

insert users (org_id, email) values(1, 'user1@example.com'), 
                                   (1, 'user2@example.com'), 
                                   (2, 'user3@example.com'), 
                                   (3, 'user4@example.com'), 
                                   (2, 'user5@example.com');

这给了我下表:

org_id | user_id | email

1 | 1 | user1@example.com

1 | 2 | user2@example.com

2 | 1 | user3@example.com

3 | 1 | user4@example.com

2 | 2 | user5@example.com

如您所见,每个用户中的用户从 1org_id开始,user_id并在每次添加时递增。

但是现在我无法创建表。当我运行查询以创建表时,我收到错误消息“表定义不正确;只能有一个自动列,并且必须将其定义为键”

任何人都可以帮忙吗?

4

1 回答 1

1

通过将引擎类型从 INNODB 更改为 MyISAM,您可以完成这项工作。

create table users (
 org_id int unsigned, user_id int unsigned auto_increment, 
 email varchar(100), 
 primary key (org_id, user_id), 
 unique key (email,  org_id)
)ENGINE=MyISAM;
于 2013-07-30T10:08:39.813 回答