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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T04:57:14+00:00 2026-06-10T04:57:14+00:00

Having a bit of a problem. Firstly, here’s my table structure. Table doc_perms id

  • 0

Having a bit of a problem. Firstly, here’s my table structure.

Table doc_perms

id    user_id    doc_id   can_view    can_edit    can_delete

ID being the PK and user_id and doc_id being FK’s of other tables.

Basically, what I need is to prevent an INSERT query from executing if the INSERT data for user_id and doc_id already exists in a row regardless of the rest of the columns.

For example

if you have the data

user_id  doc_id
   1        1
   1        2

And then you try do an INSERT with user_id = 1 and doc_id = 2, the query fails because there’s already a row with that data in it.

Hope this makes sense.

Appreciate your help in advance.

  • 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-06-10T04:57:16+00:00Added an answer on June 10, 2026 at 4:57 am

    Assuming there are no duplicates in doc_perms, you can create a unique index on (user_id, doc_id):

    CREATE UNIQUE INDEX doc_perms_index 
      ON doc_perms (user_id,doc_id)
    

    If there are duplicates, you can use ALTER IGNORE … ADD UNIQUE INDEX will remove them:

    ALTER IGNORE TABLE doc_perms 
      ADD UNIQUE INDEX doc_perms_index (user_id, doc_id)
    

    (Among all rows that share the same (user_id, doc_id), all the rows except one will be dropped. The row that is kept is unspecified.)

    After you create the unique index, INSERT INTO doc_perms ... will raise an error if (user_id, doc_id) is a duplicate.

    If you wish to INSERT, but UPDATE other columns when (user_id, doc_id) is a duplicate, then (as @moopet has already mentioned) you can use INSERT … ON DUPLICATE KEY UPDATE. For example:

    INSERT INTO doc_perms (user_id, doc_id, foo) 
      VALUES (1, 2, 3) ON DUPLICATE KEY UPDATE foo = 3'''
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i'm having a bit of a problem here. I found this script on the
I'm having a bit of a problem here. We have 2 urls let me
I am having a bit of a problem here, I am let the user
I am having a bit of a problem here with this statement below using
Having a bit of CSS problem here. First off, my HTML code: <p> <span
I am having a bit of a problem here. I have two int values,
having a bit of a problem with a recent project. The goal here is
Having a bit of a problem here on a matrix multiplication code. I seem
I'm having a bit problem with reading argument value in haskell: I'm having options
Having a bit of a problem, Due to the source-control set-up we use, each

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.