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 1095395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T00:07:02+00:00 2026-05-17T00:07:02+00:00

I have a mysql table with the following columns: group_id game_id user_id message last_update

  • 0

I have a mysql table with the following columns:

group_id
game_id
user_id
message
last_update

I want to make it so that no two rows can exist where the value of group_id for row x is equal to the value of group_id for row y AND the value of user_id for row x is also equal to the value of user_id for row y.

So, for example, let’s say I insert the following following values:

group_id = 783
game_id = 34
user_id = 29237
message = none
last_update = 11233452

The above data, even if a mysql query tries to insert it, should not create a new row if a row already exists with the same combination of group_id and user_id. Is there a way to do this? Basically, I’m trying to get two columns to work together kind of like an unique index.

  • 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-17T00:07:03+00:00Added an answer on May 17, 2026 at 12:07 am

    Yes, MySQL provides the ability to do this.

    ALTER TABLE MyTable
    ADD UNIQUE KEY `my_unique_key` (`group_id`, `user_id`)
    

    I don’t know what you’re using this table for, but it sounds like this unique key might be a strong candidate for primary key of the table. A primary key is automatically a unique key as well. If you decide to make it the primary key, then do the following instead:

    ALTER TABLE MyTable
    ADD PRIMARY KEY (`group_id`, `user_id`)
    

    (If you receive an error message that there is already a primary key, then issue ALTER TABLE MyTable DROP PRIMARY KEY and then repeat the above command.)

    Edit: In response to user comment

    You cannot have multiple rows with identical non-NULL values for the columns covered by the unique key. So you cannot have two rows where group_id = 0 AND user_id = 5, for example. 0 is a value. But if you make one or both of the columns nullable, you can have multiple rows that are identical up to positioning of NULLs. So you could have two (or more) rows where group_id IS NULL AND user_id = 1234.

    Proviso: The above is true for both commonly-used MySQL storage engines (MyISAM and InnoDB). MySQL does have storage engines in which NULL is regarded as a unique value, but you are probably not using them.

    If you make one or both columns nullable, then your unique key cannot be the primary key – a primary key has to be on columns that are NOT NULL.

    Let’s suppose you had made this key your primary key and you now want to allow NULL in the group_id column. I don’t know what datatype group_id is at the moment; I’ll assume it’s currently INT UNSIGNED NOT NULL, but you will have to modify the below if it’s not this. You would no longer be able to use this key as your primary key. Here is a command you could run to make the desired changes:

    ALTER TABLE MyTable
        DROP PRIMARY KEY,
        MODIFY group_id INT UNSIGNED,
        ADD UNIQUE KEY `my_unique_key_with_nulls` (`group_id`, `user`)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table in MySQL That looks like the following: date |storenum |views
I have columns in a mysql table that stores names of people as combinations
I have a MySQL table with the following columns: id(int), date (timestamp), starttime(varchar), endtime(varchar),
This is for MySQL and PHP I have a table that contains the following
Hello I have following columns in mysql table: rating1, rating2, price, cond, approved Is
I have created a mysql table with the following columns... item_price, discount, delivery_charge, grand_total
I have a (mysql) database table with the following columns: NAME | String (Unique)
I have a MySQL(innodb) table 'items' with the following characteristics Large number of rows,
I have 2 mysql tables : Question with the following columns : id, question,
I have 2 mysql tables 1. questions: with the following columns: id, title, answer1,

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.