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

  • Home
  • SEARCH
  • 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 607633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T17:22:50+00:00 2026-05-13T17:22:50+00:00

In a table I’ve got 3 columns: id tag1 tag2 id is a primary

  • 0

In a table I’ve got 3 columns:

id
tag1
tag2

id is a primary key.

And i only want one unique tag1-tag2-combination in that table.

eg if one entry looks like:

id: 1
tag1: cat
tag2: dog

I dont want a second entry like this one beneath to get inserted:

id: 2
tag1: cat
tag2: dog

So i made all 3 columns primary keys but the problem is that then the second entry would get inserted since it looks in the combination of all 3 of them.

How do i solve this so that only the combination of the tag1 and tag2 is unique?


UPDATE: I added a unique contraint on tag1 and tag2. however, its still possible to insert:

id: 3
tag1: dog
tag2: cat

Is there a way to prevent this?

  • 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. Editorial Team
    Editorial Team
    2026-05-13T17:22:50+00:00Added an answer on May 13, 2026 at 5:22 pm

    You should leave ID as the primary key, and then can create a unique constraint for the tag1 and tag2:

    ALTER TABLE my_table ADD CONSTRAINT uc_tags UNIQUE (tag1, tag2)
    

    With the unique constraint, you will be guaranteed that you will never have two rows with duplicate tag1 and tag2 values.


    EDIT:

    Further to your last update, you cannot enforce that with unique constraints. Keep in mind that for the database a record with (tag1 = dog, tag2 = cat) is totally different from a record with (tag1 = cat, tag2 = dog).

    Probably your best bet is to redesign your database schema, as follows:

    • Table “tags”
    • Table “messages” (or whatever you are tagging)
    • Table “tags_messages” with the following fields (message_id, tag_id)

    Then you can simply set (message_id, tag_id) of the “tag_messages” table as a primary key. This will automatically enforce that there cannot be any message with a duplicate tag.

    Some sample data:

    Table: messages
    
    message_id   |   title
    -------------+------------------
    1            |   some message
    2            |   another message
    
    
    Table: tags
    
    tag_id       |   tags
    -------------+-------------------
    1            |   cat
    2            |   dog
    3            |   duck
    4            |   horse
    
    
    Table: messages_tags
    
    message_id   |   tag_id
    -------------+-------------------
    1            |   1
    1            |   2
    2            |   3
    2            |   4
    2            |   1
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Table/Entity 1: RetrievedDataRecords Columns: id record_key Table/Entity 2: SourceKeys Columns: id key_name (unique key)
table data of 2 columns category and subcategory i want to get a collection
table 'sample' contaning fields [AffiliateId] as a primary key, [AffiliateCode] L, [Name] , [AddressId]
Table IMAGES : columns- imageID, Image, and categoryID*(this being the foreign key) Table CATEGORIES
I have a script that appends some rows to a table. One of the
my table looks like this: create table foos( id uniqueidentifier primary KEY DEFAULT (newsequentialid()),
Table: UserId, Value, Date. I want to get the UserId, Value for the max(Date)
A table exists that someone else loaded. I need to query against the table,
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
Table has about 8 million rows. There is a non-unique index for X. Showing

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.