【发布时间】:2025-12-01 21:55:02
【问题描述】:
如果我有 tblBookInfo(bookId、title...等)并且我希望它有类别列,那么最好的方法是什么?
选项 1
categories 表会有这样的与 bookId 相关的 FK
tblBookInfobookId title
1 test title
2 test title 2
tblCategories
bookId name
1 Science fiction
1 Mystery
1 Horror
2 Science fiction
2 Mystery
在这种情况下,类别名称会重复多次,很丑
选项 2
像这样在 btlBookInfo 中存储一个 int 数组
tblBookInfobookId title categories
1 test title [1,2,3]
2 test title 2 [1,2]
我听说将数组存储在列中并不是here 提出的最佳实践@
选项 3
让类别表保持原样
tblCategoriesid name
1 Science finction
2 Mystery
3 Horror
然后像这样向 tblBookInfo 添加一个 FK
tblBookInfobookId title categoryId
1 test title 1
2 test title 2 2
最后,添加另一个表,将这些表链接到这样的表
tblCategoryAdapterbookId categoryId
1 1
1 2
1 3
2 1
2 2
现在不用重复分类名但我觉得不正常
【问题讨论】:
标签: sql-server relationship categories jointable