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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T03:44:20+00:00 2026-06-11T03:44:20+00:00

Description: I am trying to insert user’s preferences into a database. If the user

  • 0

Description:

I am trying to insert user’s preferences into a database. If the user hasn’t yet placed any, I want a insert, otherwise, I want an update. I know I can insert default values in the creation of the user and than exclusively use update, but that adds another query (I think)

Problem:

I have read up on ON DUPLICATE KEY UPDATE but I don’t understand it. This is almost the exact question I have but without the answer. The answer says:

It does sound like it will work for what you want to do as long as you hav the proper column(s) defined as UNIQUE KEY or PRIMARY KEY.

If I do a simple insert like so:

INSERT INTO table (color, size) VALUES ('blue', '18') ... 

How will that ever produce at DUPLICATE KEY? As far as mysql knows it’s just another insert and the id is auto-incremented. I have the primary key in the table set to unique, but the insert isn’t going to check against that, right?

Table:

CREATE TABLE `firm_pref` (
    `id` int(9) NOT NULL AUTO_INCREMENT,
    `firm_id` int(9) NOT NULL, //user_id in this case
    `header_title` varchar(99) NOT NULL,
    `statement` varchar(99) NOT NULL,
    `footer_content` varchar(99) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1
  • 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-11T03:44:21+00:00Added an answer on June 11, 2026 at 3:44 am

    Well, unless you want your application to be used by a single person only, you would have to specify someone’s user_id in that INSERT – when this ‘someone’ guy or girl updates his/her preferences, right?

    This field (user_id) is exactly what would be checked by ON DUPLICATE KEY UPDATE clause.

    And if you want to insert a new record, just send NULL instead:

    INSERT INTO table (user_id, color, size) VALUES (NULL, 'blue', 18);
    

    … so auto-increment will have a chance to move on and save the day. )

    UPDATE: Take note that to understand that some field should be considered a unique identifier, you should mark it as such. Usually it’s done naturally, as this field is used as a PRIMARY KEY. But sometimes it’s not enough; it means some work for UNIQUE constraint. For example, in your table it can be used like this:

    CREATE TABLE `prefs` (
        `id` int(9) NOT NULL AUTO_INCREMENT,
        `firm_id` int(9) NOT NULL,
         ...
         PRIMARY KEY (`id`),
         UNIQUE KEY (`firm_id`)
    );
    

    (or you can add this constraint to the existing table with ALTER TABLE prefs ADD UNIQUE (firm_id) command)

    Then insert/update query will look like…

    INSERT INTO prefs(firm_id, header_title, statement, footer_content) 
               VALUES(17, 'blue', '18', 'some_footer')
     ON DUPLICATE KEY UPDATE 
               header_title = 'blue', 
               statement = '18', 
               footer_content = 'some_footer';
    

    I’ve built a sort of demo in SQL Fiddle. You can play with it some more to better understand that concept. )

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

Sidebar

Related Questions

I am trying to insert a lot of fields into a MySQL database, some
This is the query I am trying: INSERT INTO Product (ProductName, Description, Brand, Size,
I am trying to insert a bit of text into my MYSQL database via
We're trying a basic insert statement: INSERT INTO HOLIDAY (HOLIDAY_TYPE_CODE, CALENDAR_NAME, HOLIDAY_DATE, DESCRIPTION, CREATE_TS,
I am trying to insert some values into a database from a form submitted
I am trying to insert data into two different tables in the same database,
I'm having some issues trying to insert a query into the database. I have
I'm trying to insert a partial into a jquery dialog box. The dialog box
Hi I'm trying to submit a webform and insert the data into mysql. The
I'm trying to create a function to insert data into a table. The query

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.