我对使用 SQL 还很陌生,但我在 Stack Overflow 上遇到过这个关于使用标签的问题。
推荐用于标记或标记的 SQL 数据库设计 https://stackoverflow.com/questions/20856/recommended-sql-database-design-for-tags-or-tagging/20871#20871
这导致我在数据库中创建了以下表:
Doc
---------------------------------------------------------------
| Doc Id (PK)(int) | Doc Title (varchar) | Doc Link (varchar) |
---------------------------------------------------------------
| 1 | Printing | http://example.com |
---------------------------------------------------------------
| 2 | Format | http://example.com |
---------------------------------------------------------------
Tag
--------------------------------------
| Tag Id (PK)(int) | Title (varchar) |
--------------------------------------
| 1 | print |
--------------------------------------
| 2 | guide |
--------------------------------------
| 3 | support |
--------------------------------------
DocTag
---------------------------------
| DocId (int) | TagId (varchar) |
---------------------------------
| 1 | 1, 3 |
---------------------------------
| 2 | 2, 3 |
---------------------------------
但是,我无法在DocTag
表和其他两个,因为我需要在中创建一个排序数组TagId
of DocTag
因为一个文档可以有多个标签。我在尝试创建主键时遇到错误,我认为这是因为 varchar 变量TagId
.
有哪些建议可以克服这个问题?
DocTag
---------------------------------
| DocId (int) | TagId (int) |
---------------------------------
| 1 | 1 |
---------------------------------
| 1 | 3 |
---------------------------------
| 2 | 2 |
---------------------------------
| 2 | 3 |
---------------------------------
外键必须与其引用的列的数据类型匹配。
每个外键引用必须是单个值。没有逗号分隔的列表。
将每个 TagId 值放在其自己的行中,即使您必须重复 DocId。
See 我的回答是:“在数据库列中存储分隔列表真的那么糟糕吗?” https://stackoverflow.com/questions/3653462/is-storing-a-delimited-list-in-a-database-column-really-that-bad/3653574#3653574
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)