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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T10:02:57+00:00 2026-05-18T10:02:57+00:00

My table structure is: CREATE TABLE IF NOT EXISTS `users_settings_temp` ( `id` int(10) unsigned

  • 0

My table structure is:

CREATE TABLE IF NOT EXISTS `users_settings_temp` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `userid` int(10) unsigned DEFAULT NULL,
  `type` enum('type1','type2')
  `data` text,
  `date_created` int(11) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

What I am trying to do is:

Let say I want to insert a new entry, but I dont want it to be duplicate, after google around, i found this format:

INSERT INTO users_settings_temp(...)
ON DUPLICATE KEY UPDATE data = '{$data}'

I guess the problem is in my table, the primary key => id. How do I alter the table, so that I could use the:

INSERT INTO ... ON DUPLICATE KEY UPDATE

Can I use user_id + type as primary key? If yes, could you please show me how to do it?

  • 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-18T10:02:58+00:00Added an answer on May 18, 2026 at 10:02 am
    CREATE TABLE IF NOT EXISTS `users_settings_temp` (
      `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
      `userid` int(10) unsigned DEFAULT NULL,
      `type` enum('type1','type2'),
      `data` text,
      `date_created` int(11) DEFAULT NULL,
      PRIMARY KEY (`id`, `type`)
    ) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;
    

    When you do it like this then

    a) specifying id works

    mysql> INSERT INTO users_settings_temp VALUES (1, 2, 'type1', 'keks', 5);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> INSERT INTO users_settings_temp VALUES (1, 2, 'type2', 'keks', 5);
    Query OK, 1 row affected (0.00 sec)
    

    b) of course primary key is guaranteed to be unique

    mysql> INSERT INTO users_settings_temp VALUES (1, 2, 'type2', 'keks', 5);
    ERROR 1062 (23000): Duplicate entry '1-type2' for key 'PRIMARY'
    

    c) letting database pull a new id works

    mysql> INSERT INTO users_settings_temp VALUES (NULL, 2, 'type2', 'keks', 5);
    Query OK, 1 row affected (0.00 sec)
    
    mysql> INSERT INTO users_settings_temp VALUES (NULL, 2, 'type1', 'keks', 5);
    Query OK, 1 row affected (0.00 sec)
    

    but will increase them always

    mysql> SELECT * FROM users_settings_temp;
    +----+--------+-------+------+--------------+
    | id | userid | type  | data | date_created |
    +----+--------+-------+------+--------------+
    |  1 |      2 | type1 | keks |            5 |
    |  1 |      2 | type2 | keks |            5 |
    |  2 |      2 | type2 | keks |            5 |
    |  3 |      2 | type1 | keks |            5 |
    +----+--------+-------+------+--------------+
    4 rows in set (0.00 sec)
    

    NOTES:

    You should think if your id should still be autoincrement or not.
    Also, can not think of a reason why date_created should be int(11) instead of datetime

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

Sidebar

Related Questions

I have the following table structure CREATE TABLE `table` ( `id` int(11) NOT NULL
Given a table structure like this: CREATE TABLE `user` ( `id` int(10) unsigned NOT
Simplified Table structure: CREATE TABLE IF NOT EXISTS `hpa` ( `id` bigint(15) NOT NULL
Here is my DB Structure: CREATE TABLE IF NOT EXISTS `UserItems` ( `id` bigint(20)
Hi this is my table structure CREATE TABLE IF NOT EXISTS `sms_report` ( `R_id`
I've got a table structure I'm not really certain of how to create the
Data table structure is: id1,id2,id3,id4,... (some other fields). I want to create summary query
My table structure looks like this: tbl.users tbl.issues +--------+-----------+ +---------+------------+-----------+ | userid | real_name
Assume a table structure of MyTable(KEY, datafield1, datafield2...) . Often I want to either
In the following table structure: Fruits ( fruit_id, fruitName ) Vegetables ( vegetable_id, vegetableName

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.