Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 87969
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T22:29:10+00:00 2026-05-10T22:29:10+00:00

I am trying to so something like Database Design for Tagging , except each

  • 0

I am trying to so something like Database Design for Tagging, except each of my tags are grouped into categories.

For example, let’s say I have a database about vehicles. Let’s say we actually don’t know very much about vehicles, so we can’t specify the columns all vehicles will have. Therefore we shall ‘tag’ vehicles with information.

1. manufacture: Mercedes    model: SLK32 AMG    convertible: hardtop  2. manufacture: Ford    model: GT90    production phase: prototype  3. manufacture: Mazda    model: MX-5    convertible: softtop 

Now as you can see all cars are tagged with their manufacture and model, but the other categories don’t all match. Note that a car can only have one of each category. IE. A car can only have one manufacturer.

I want to design a database to support a search for all Mercedes, or to be able to list all manufactures.

My current design is something like this:

vehicles   int vid   String vin  vehicleTags   int vid   int tid  tags   int tid   String tag   int cid  categories   int cid   String category 

I have all the right primary and foreign keys in place, except I can’t handle the case where each car can only have one manufacturer. Or can I?

Can I add a foreign key constraint to the composite primary key in vehicleTags? IE. Could I add a constraint such that the composite primary key (vid, tid) can only be added to vehicleTags only if there isn’t already a row in vehicleTags such that for the same vid, there isn’t already a tid in the with the same cid?

My guess is no. I think the solution to this problem is add a cid column to vehicleTags, and make the new composite primary key (vid, cid). It would look like:

vehicleTags   int vid   int cid   int tid 

This would prevent a car from having two manufacturers, but now I have duplicated the information that tid is in cid.

What should my schema be?

Tom noticed this problem in my database schema in my previous question, How do you do many to many table outer joins?

EDIT
I know that in the example manufacture should really be a column in the vehicle table, but let’s say you can’t do that. The example is just an example.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-10T22:29:11+00:00Added an answer on May 10, 2026 at 10:29 pm

    This is yet another variation on the Entity-Attribute-Value design.

    A more recognizable EAV table looks like the following:

    CREATE TABLE vehicleEAV (   vid        INTEGER,   attr_name  VARCHAR(20),   attr_value VARCHAR(100),   PRIMARY KEY (vid, attr_name),   FOREIGN KEY (vid) REFERENCES vehicles (vid) ); 

    Some people force attr_name to reference a lookup table of predefined attribute names, to limit the chaos.

    What you’ve done is simply spread an EAV table over three tables, but without improving the order of your metadata:

    CREATE TABLE vehicleTag (   vid         INTEGER,   cid         INTEGER,   tid         INTEGER,   PRIMARY KEY (vid, cid),   FOREIGN KEY (vid) REFERENCES vehicles(vid),   FOREIGN KEY (cid) REFERENCES categories(cid),   FOREIGN KEY (tid) REFERENCES tags(tid) );  CREATE TABLE categories (   cid        INTEGER PRIMARY KEY,   category   VARCHAR(20) -- 'attr_name' );  CREATE TABLE tags (   tid        INTEGER PRIMARY KEY,   tag        VARCHAR(100) -- 'attr_value' ); 

    If you’re going to use the EAV design, you only need the vehicleTags and categories tables.

    CREATE TABLE vehicleTag (   vid         INTEGER,   cid         INTEGER,     -- reference to 'attr_name' lookup table   tag         VARCHAR(100, -- 'attr_value'   PRIMARY KEY (vid, cid),   FOREIGN KEY (vid) REFERENCES vehicles(vid),   FOREIGN KEY (cid) REFERENCES categories(cid) ); 

    But keep in mind that you’re mixing data with metadata. You lose the ability to apply certain constraints to your data model.

    • How can you make one of the categories mandatory (a conventional column uses a NOT NULL constraint)?
    • How can you use SQL data types to validate some of your tag values? You can’t, because you’re using a long string for every tag value. Is this string long enough for every tag you’ll need in the future? You can’t tell.
    • How can you constrain some of your tags to a set of permitted values (a conventional table uses a foreign key to a lookup table)? This is your ‘softtop’ vs. ‘soft top’ example. But you can’t make a constraint on the tag column because that constraint would apply to all other tag values for other categories. You’d effectively restrict engine size and paint color to ‘soft top’ as well.

    SQL databases don’t work well with this model. It’s extremely difficult to get right, and querying it becomes very complex. If you do continue to use SQL, you will be better off modeling the tables conventionally, with one column per attribute. If you have need to have ‘subtypes’ then define a subordinate table per subtype (Class-Table Inheritance), or else use Single-Table Inheritance. If you have an unlimited variation in the attributes per entity, then use Serialized LOB.

    Another technology that is designed for these kinds of fluid, non-relational data models is a Semantic Database, storing data in RDF and queried with SPARQL. One free solution is RDF4J (formerly Sesame).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 81k
  • Answers 81k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer It may be worth you looking up Mvc.Net Model Binding.… May 11, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer Yes. You have to wrap the .dll in a .jar… May 11, 2026 at 4:28 pm
  • Editorial Team
    Editorial Team added an answer I am yet to see a special tool beyond what… May 11, 2026 at 4:28 pm

Related Questions

I am trying to extract the schema of an .mdb database, so that I
I am trying to paginate the results of an SQL query for use on
I am trying to get data from different sever, and the sever name might
I am working on an application that will have online and offline components and

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.