I'm pretty new to working with SQL but I've come across this question on Stack Overflow for working with tags.
Recommended SQL database design for tags or tagging
This led me to create the following tables in my database:
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 |
---------------------------------
However, I cannot create foreign keys between the DocTag
table and the other two because I need to create an array of sorts in the TagId
of DocTag
because one doc can have many tags. I am getting an error when trying to create the primary key and I assume it's because of the varchar variable in TagId
.
What are some suggestions for overcoming this?