首先,如果这应该在代码审查部分或其他地方,请告诉我。
我正在尝试构建数据库架构,而且我已经找到了一些棘手的补丁。我想要三种类型的帐户SINGLE,ORGANIZATION&多。我需要弄清楚登录应该在哪里。很可能是俱乐部和组织
一个单一账户将允许一个俱乐部注册和管理俱乐部的详细信息。
ORGANIZATION帐户将允许您注册许多俱乐部。在组织层面,您应该能够看到所有俱乐部及其成员。在俱乐部级别,您只能控制自己的俱乐部详情和您自己的会员。
最后一个MULTI(至少对我来说是棘手的一点)。该帐户将允许您针对这些甚至单个俱乐部注册多个组织和多个俱乐部。最高级别将显示所有组织及其俱乐部和独立俱乐部。
从我的架构中可以看出,单个账户每个只有一个俱乐部。这个组织有很多俱乐部。 Multi可以拥有组织和俱乐部。
在下面的示例中,我希望帐户4有2个站在俱乐部和一个有3个俱乐部的组织。
ACCOUNTS
==============
id, name, type (single, organisation, multi)
1 | Single Account 1 | SINGLE
2 | Single Account 2 | SINGLE
3 | Org Account 1 | ORGANISATION
4 | Multi Org Account 1 | MULTI
CLUBS
==============
id, account_id, organisation_id, name
1 | 1 | (NULL) | Single Club 1
2 | 2 | (NULL) | Single Club 1
3 | 3 | 1 | Org 1 Club 1
4 | 3 | 1 | Org 1 Club 2
5 | 3 | 1 | Org 1 Club 2
6 | 4 | 2 | (Multi) Org 2 Club 1
7 | 4 | 2 | (Multi) Org 2 Club 2
8 | 4 | 3 | (Multi) Org 2&3 Club 3
9 | 4 | 3 | (Multi) Org 2&3 Club 4
10 | 4 | 3 | (Multi) Org 2&3 Club 5
ORGANISATIONS
==============
id, account_id, organisation_id, name
1 | 3 | (NULL) | ORG 1
2 | 4 | (NULL) | ORG 2
3 | 4 | 2 | ORG 3
MEMBERS
==============
id, club_id, name
答案 0 :(得分:0)
这是我能想到的最好的......
create table account (
account_id int unsigned primary key,
account_name varchar(20) not null,
is_org tinyint(1) unsigned not null,
parent_account_id int unsigned
);
create table club (
club_id int unsigned auto_increment primary key,
club_name varchar(20) not null,
account_id int unsigned,
foreign key (account_id) references account(account_id)
);
insert into account values (1, 'multi', 0, null);
insert into account values (2, 'm org 1', 1, 1);
insert into account values (3, 'm org 2', 1, 1);
insert into account values (4, 'org', 1, null);
insert into club values (null, 'disco lemonade', null);
insert into club values (null, 'billy club', 1);
insert into club values (null, 'm o 1 club A', 2);
insert into club values (null, 'm o 2 club A', 3);
insert into club values (null, 'm o 2 club B', 3);
insert into club values (null, 'o club A', 4);
insert into club values (null, 'o club B', 4);
select p.account_name as parent_name,
a.account_name,
c.club_name
from account as a
left outer join account as p
on p.account_id=a.parent_account_id
/* a right outer join is generally bad practice, btw,
but you should get the point */
right outer join club as c
on c.account_id=a.account_id;
/*
+-------------+--------------+----------------+
| parent_name | account_name | club_name |
+-------------+--------------+----------------+
| NULL | NULL | disco lemonade |
| NULL | multi | billy club |
| multi | m org 1 | m o 1 club A |
| multi | m org 2 | m o 2 club A |
| multi | m org 2 | m o 2 club B |
| NULL | org | o club A |
| NULL | org | o club B |
+-------------+--------------+----------------+
*/
select a.parent_account_id,
p.account_name as parent_name,
a.account_id,
a.account_name,
a.is_org,
c.club_id,
c.club_name
from account as a
left outer join account as p
on p.account_id=a.parent_account_id
join club as c
on c.account_id=a.account_id
where p.account_id=1 or a.account_id=1;
/*
+-------------------+-------------+------------+--------------+--------+---------+--------------+
| parent_account_id | parent_name | account_id | account_name | is_org | club_id | club_name |
+-------------------+-------------+------------+--------------+--------+---------+--------------+
| NULL | NULL | 1 | multi | 0 | 2 | billy club |
| 1 | multi | 2 | m org 1 | 1 | 3 | m o 1 club A |
| 1 | multi | 3 | m org 2 | 1 | 4 | m o 2 club A |
| 1 | multi | 3 | m org 2 | 1 | 5 | m o 2 club B |
+-------------------+-------------+------------+--------------+--------+---------+--------------+
*/
我认为多元组织本身与俱乐部不同,并且您需要存储关于俱乐部的更多信息(列)而不是其他事情。如果不是这种情况,您可以只使用一个表并对其进行完全分层...或者您可以使用club_details
表或其他内容。
create table thing (
thing_id int unsigned primary key,
thing_name varchar(20),
thing_type char(1),
parent_id int unsigned
);
insert into thing values (1, 'multi', 'm', null);
insert into thing values (2, 'm org 1', 'o', 1);
insert into thing values (3, 'm org 2', 'o', 1);
insert into thing values (4, 'org', 'o', null);
insert into thing values (5, 'disco lemonade', 'c', null);
insert into thing values (6, 'billy club', 'c', 1);
insert into thing values (7, 'm o 1 club A', 'c', 2);
insert into thing values (8, 'm o 2 club A', 'c', 3);
insert into thing values (9, 'm o 2 club B', 'c', 3);
insert into thing values (10, 'o club A', 'c', 4);
insert into thing values (11, 'o club B', 'c', 4);
select p2.thing_name as grand_parent_name,
p2.thing_type as grand_parent_type,
p1.thing_name as parent_name,
p1.thing_type as parent_type,
club.thing_name as club_name
from thing as club
left outer join thing as p1
on p1.thing_id=club.parent_id
left outer join thing as p2
on p2.thing_id=p1.parent_id
where club.thing_type='c'
/*
+-------------------+-------------------+-------------+-------------+----------------+
| grand_parent_name | grand_parent_type | parent_name | parent_type | club_name |
+-------------------+-------------------+-------------+-------------+----------------+
| NULL | NULL | NULL | NULL | disco lemonade |
| NULL | NULL | multi | m | billy club |
| multi | m | m org 1 | o | m o 1 club A |
| multi | m | m org 2 | o | m o 2 club A |
| multi | m | m org 2 | o | m o 2 club B |
| NULL | NULL | org | o | o club A |
| NULL | NULL | org | o | o club B |
+-------------------+-------------------+-------------+-------------+----------------+
*/
答案 1 :(得分:0)
我不确定您是否在询问如何为其设计表格。那么您的示例数据显示了实体之间的关系。因此,俱乐部将拥有组织和账户的外键(我假设一个俱乐部只能是1个组织/ 1个账户的一部分,否则我们将需要一个桥接表来容纳多对多的关系)。组织将有一个外键帐户。另外,我不理解组织表中需要单独的organization_id列。
id,account_id,organisation_id,name
1 | 3 | (NULL)| ORG 1
2 | 4 | (NULL)| ORG 2
3 | 4 | 2 | ORG 3
organization_id在这里是什么意思?