简体   繁体   中英

primary and foreign key in same table or not

i have a category field. each category has at most one parent. which is better design?

1)one table: category_parent is Foreign key (related to category_id) and category_id is primary key

category_id | category_parent | category_name

2)two table

table1

catgory_id | category_name

table2

id | category | category_parent

category,category_parent are Foreign key (related to category_id) and category_id is primary key

You say that each node will only have parent. For your requirement, the first option is the better design.

The second option is only really mandated if you need a node to have multiple parents.

Obviously the first one as each node will only have one parent. So,there will be no redundancy as the category name cannot be repeated..

If there can be more than one parent then the category name would be repeated in the table. In this case, you would create two tables.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM