不同品类的数据库结构设计
Database structure design for different categories
我的数据库中有两个table...第一个table命名为categories
包含所有父类别和子类别...如下图所示...
Categories_id name Parent_Categories
1 art 0
2 entertainment 0
3 music 2
4 dance 3
5 songs 3
现在我必须创建第二个 table 名称作为 Users_table
..但条件是...
1)我们可以为每个用户分配多个类别ID ...
2)多个用户可以有相同的类别。
下图是目前的数据库结构..有没有更好的方法来做到这一点??
User_Id Categories_id
1 4,5
2 1,2
3 1,2,4
4 1,5,4
用户:
- UserID
用户类别:
- UserID
- CategoryID
类别:
- CategoryID
- ParentID
create table Users_table (User_Id integer,Categories_id integer, primary key(User_Id,Categories_id), foreign key (Categories_id) references categories(Categories_id);
...
primary key(User_Id,Categories_id)
<-- 可以为每个用户分配多个类别 ID,并且只限制重复具有相同值的行。因此您可以让多个用户具有相同的类别,并且一个用户可以具有多个类别 ID。 . 但不是具有相同用户 ID 和相同类别 ID 的 2 行
foreign key (Categories_id) references categories(Categories_id)
<-- 此类别 ID 引用类别 table
在这种情况下使用规范化如下 -
1) 用户 table
- ID
- 用户名
2) 类别table
- ID
- parent_category_id
3) user_categories table
- ID
- user_id
- categories_id
我的数据库中有两个table...第一个table命名为categories
包含所有父类别和子类别...如下图所示...
Categories_id name Parent_Categories
1 art 0
2 entertainment 0
3 music 2
4 dance 3
5 songs 3
现在我必须创建第二个 table 名称作为 Users_table
..但条件是...
1)我们可以为每个用户分配多个类别ID ...
2)多个用户可以有相同的类别。
下图是目前的数据库结构..有没有更好的方法来做到这一点??
User_Id Categories_id
1 4,5
2 1,2
3 1,2,4
4 1,5,4
用户:
- UserID
用户类别:
- UserID
- CategoryID
类别:
- CategoryID
- ParentID
create table Users_table (User_Id integer,Categories_id integer, primary key(User_Id,Categories_id), foreign key (Categories_id) references categories(Categories_id);
...
primary key(User_Id,Categories_id)
<-- 可以为每个用户分配多个类别 ID,并且只限制重复具有相同值的行。因此您可以让多个用户具有相同的类别,并且一个用户可以具有多个类别 ID。 . 但不是具有相同用户 ID 和相同类别 ID 的 2 行
foreign key (Categories_id) references categories(Categories_id)
<-- 此类别 ID 引用类别 table
在这种情况下使用规范化如下 -
1) 用户 table - ID - 用户名
2) 类别table - ID - parent_category_id
3) user_categories table - ID - user_id - categories_id