Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to save tags(keywords) in database?

Tags:

mysql

tags

I want to create a simple tags system using php and mysql, so that users can add few tags via form. My question is that should i save the tags as an array in single database column? eg. "tag1, tag2, tag3".. or i should have separate columns in database table where i should save each tag in each column. i hope my question is clear. Thanks.

like image 219
user966582 Avatar asked Dec 21 '10 14:12

user966582


People also ask

How do I store post tags in a database?

The easiest way: create a tag table, create an entity table, create a many-to-many table like { tag_id, entity_id }. For every tag/entity insert that pair into link table. The best way: do not store tag data in relational DB, use mongo, arangodb or other nosql db for this.

How do I store multiple tags in a database?

The classic way to store it is via an embedded array of tags: { PictureUrl: ..., tags: [ "party", "man" ... ] } As long as number of tags is below a few houndreds its fine. You can index tags field with multi key index and simply search terms on tags.

How do database tags work?

In information systems, a tag is a keyword or term assigned to a piece of information (such as an Internet bookmark, multimedia, database record, or computer file). This kind of metadata helps describe an item and allows it to be found again by browsing or searching.

Which keywords is used to create database?

Which keyword is used to create a database? Explanation: The statement 'CREATE DATABASE database_name;' is used to create a database with the name 'database_name'.


2 Answers

I would probably say neither. Use a many-to-many relationship between tags and the object being tagged. For instance, if the thing being tagged is a question, the tables could look like this:

Question:
    QuestionId
    Title
    Body

Tag:
    TagId
    Name

QuestionTags:
    QuestionId
    TagId
like image 155
Klaus Byskov Pedersen Avatar answered Oct 10 '22 18:10

Klaus Byskov Pedersen


came across that issue today as well and gathered some ideas here, and although the question isn't very new, i'll leave my solution as well:

i think the answer Klaus Byskov Hoffmann gave wasn't bad, but I'd add on that and store the tag-list as many-to-many table like he said, but also as serialized string in some form (either via serialize like user466764 said, or just a comma separated thing like you said yourself, which could be handled with implode/explode) in the main table.

yea, i know: storing the same data twice is not very well received with many database perfectionists, as it bears the danger of getting inconsistencies, but i'd do it that way for performance and simplicity:

the many-to-many-table (tag-table) is for search only. to increase search performance, I'd limit access to that table ONLY to search (and of course we need to update it when tags are edited), but never query it just for viewing/listing the tags somewhere. and the serialized tag-list is for every place where you are viewing the article or item in question - when displaying that item, you have the table already, doing yet another query to the tag-table every time you want to display that page is unnecessary when you have the list in the main table already. make sure you are careful when updating the tags to always update them in both places, preferable via a single setter function that does both, and you shouldn't have problems with inconsistency.

like image 35
Amadox Avatar answered Oct 10 '22 16:10

Amadox