Table of Contents
Reply content:
Home Backend Development PHP Tutorial I would like to ask everyone, when writing an article in the forum, there will be a function to add tags to the article. How should this database be designed?

I would like to ask everyone, when writing an article in the forum, there will be a function to add tags to the article. How should this database be designed?

Aug 04, 2016 am 09:19 AM
java mysql oracle php

I have thought that there is an article table, and the tags entered by the user cannot be controlled. In this case, there is no way to manage the tags. For example, if I want to use tags to make a conditional query, it will not work.

Reply content:

I have thought that there is an article table, and the tags entered by the user cannot be controlled. In this case, there is no way to manage the tags. For example, if I want to use tags to make a conditional query, it will not work.

Create a tag table. The tag table contains tag ID, Name, etc. Create a correlation table, then save ArticleID, TagID, and then extract the tag when entering or crawling the article. First determine whether the tag table has already obtained tags, update if not, then store the tag ID in the association table, and then add the article ID. When the time comes to update, it can be taken out directly through the association table, so that it can be displayed successfully.

There are three major paradigms in database design,
1. The fields in the database are atomic and cannot be split. The tags seem to be consistent, but the tags also have separate attributes for the tags. It is inconvenient if the list needs to be filtered out separately based on the tags later.

<code>CREATE TABLE `user_label` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '类型',
  `lable_name` varchar(255) NOT NULL COMMENT '标签名称',
  `user_id` int(11) NOT NULL COMMENT '创建者',
  `create_time` datetime NOT NULL COMMENT '创建时间',
  PRIMARY KEY (`id`),
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COMMENT='标签表';</code>
Copy after login

The

tags are placed in a separate data table, and the post data table is placed in a tags field, separated by |. The ‘|’ character is not allowed in the tags.

Create a table to store tags. Add a field to the article table to store the tag id. When the user adds a tag, check whether the tag is duplicated and store it in the tag table. Then update the tag field in the article table

Two tables

  • Create a tag table

  • An article tag association table, the fields are article ID and tag ID
    In this way, when querying, you only need to query the article ID corresponding to the tag in the association table.

This should be a many-to-many relationship in database design. Design three tables: article table tags - article association table tag table. Set dual primary keys in the tag article table, which are the IDs of the tag table and the article table. You can query them Use the join statement to directly filter out the desired results. There is another way to use json to store tag data in the new mysql5.7. This can avoid setting up multiple tables. If the answer is wrong, please point it out. If you are satisfied, please give me a thumbs up. Thank you

I think @ivanilla is right, this table design is almost usable. There is no need to query tag fields associated with multiple tables, and it is simple to use.

I happened to have paper on hand, so I drew a simple E-R diagram
I would like to ask everyone, when writing an article in the forum, there will be a function to add tags to the article. How should this database be designed?

I don’t know if it’s better not to step on it randomly. Although my answer didn’t directly help him solve it, it is still a very good idea. Idiot, I took a detour, thank you

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24
What is the significance of the session_start() function? What is the significance of the session_start() function? May 03, 2025 am 12:18 AM

session_start()iscrucialinPHPformanagingusersessions.1)Itinitiatesanewsessionifnoneexists,2)resumesanexistingsession,and3)setsasessioncookieforcontinuityacrossrequests,enablingapplicationslikeuserauthenticationandpersonalizedcontent.

Composer: The Package Manager for PHP Developers Composer: The Package Manager for PHP Developers May 02, 2025 am 12:23 AM

Composer is a dependency management tool for PHP, and manages project dependencies through composer.json file. 1) parse composer.json to obtain dependency information; 2) parse dependencies to form a dependency tree; 3) download and install dependencies from Packagist to the vendor directory; 4) generate composer.lock file to lock the dependency version to ensure team consistency and project maintainability.

What are the advantages of using MySQL over other relational databases? What are the advantages of using MySQL over other relational databases? May 01, 2025 am 12:18 AM

The reasons why MySQL is widely used in various projects include: 1. High performance and scalability, supporting multiple storage engines; 2. Easy to use and maintain, simple configuration and rich tools; 3. Rich ecosystem, attracting a large number of community and third-party tool support; 4. Cross-platform support, suitable for multiple operating systems.

What are the advantages of using Java for web applications that need to run on different servers? What are the advantages of using Java for web applications that need to run on different servers? May 03, 2025 am 12:13 AM

Java is suitable for developing cross-server web applications. 1) Java's "write once, run everywhere" philosophy makes its code run on any platform that supports JVM. 2) Java has a rich ecosystem, including tools such as Spring and Hibernate, to simplify the development process. 3) Java performs excellently in performance and security, providing efficient memory management and strong security guarantees.

What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence? What role does Java play in the development of IoT (Internet of Things) devices, considering platform independence? May 03, 2025 am 12:22 AM

JavaplaysasignificantroleinIoTduetoitsplatformindependence.1)Itallowscodetobewrittenonceandrunonvariousdevices.2)Java'secosystemprovidesusefullibrariesforIoT.3)ItssecurityfeaturesenhanceIoTsystemsafety.However,developersmustaddressmemoryandstartuptim

How does platform independence simplify deployment of Java applications? How does platform independence simplify deployment of Java applications? May 02, 2025 am 12:15 AM

Java'splatformindependenceallowsapplicationstorunonanyoperatingsystemwithaJVM.1)Singlecodebase:writeandcompileonceforallplatforms.2)Easyupdates:updatebytecodeforsimultaneousdeployment.3)Testingefficiency:testononeplatformforuniversalbehavior.4)Scalab

MySQL vs. phpMyAdmin: Understanding the Key Differences MySQL vs. phpMyAdmin: Understanding the Key Differences May 06, 2025 am 12:17 AM

MySQL is a database management system, and phpMyAdmin is a web tool for managing MySQL. 1.MySQL is used to store and manage data and supports SQL operations. 2.phpMyAdmin provides a graphical interface to simplify database management.

MySQL vs. Oracle: Understanding Licensing and Cost MySQL vs. Oracle: Understanding Licensing and Cost May 03, 2025 am 12:19 AM

MySQL uses GPL and commercial licenses for small and open source projects; Oracle uses commercial licenses for enterprises that require high performance. MySQL's GPL license is free, and commercial licenses require payment; Oracle license fees are calculated based on processors or users, and the cost is relatively high.

See all articles