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

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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T23:25:39+00:00 2026-06-14T23:25:39+00:00

Ok, I have a table where I will collect users devices. When a user

  • 0

Ok, I have a table where I will collect users devices. When a user logs in, I want it to register the device, if it already exists, it would just want to update the timestamp. This is only when a user logs in.

The table looks like this:

device_id => BIGINT AUTOINCREMENT PRIMARY KEY
user_id => BIGINT FOREIGN KEY users(id)
device_name => VARCHAR(40) (Will be like 'Donald ducks iphone')
device_type => VARCHAR(10) (Will be defined by the client, like "IOS")
last_usage => TIMESTAMP (On update, new timestamp and all that)

So, when a user logs in, I have the user_id, device_name, device_type.

What I want:
if the id, name and type already exists in a row, just update timestamp, else insert the values.

Since this has nothing to do with the primary key I dont know how to do this. Of course I could do something like first select these values, return that and do the update/insert afterwards, but this does not feel right 🙂

  • 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-14T23:25:40+00:00Added an answer on June 14, 2026 at 11:25 pm

    First, make device_name and device_type unique.

    Now you have a couple of options. You can use:

    INSERT IGNORE INTO devices (...) VALUES(...)
    

    or if you’re worried important errors might get ignored, use:

    INSERT INTO devices (...) VALUES(...)
    ON DUPLICATE KEY UPDATE device_id = device_id
    

    Note that this is different from REPLACE in that this doesn’t affect the old records at all, whereas REPLACE will create a new ID and timestamp.

    EDIT: Since you now want to update the timestamp, use:

    INSERT INTO devices (...) VALUES(...)
    ON DUPLICATE KEY UPDATE last_usage = NOW()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 200 users each user will eventually have a reviewINFO table with certain
I have a table that potentially will have high number of inserts per second,
What I can think of is: Algo: Have a hash table which will store
I have created an archive table which will store data for selecting only. Daily
I have a table called pollData. It will always contain only 1 row. It
I have a table 'customertransactions' with a column 'transactiondate' of type DateTime. I will
if i have a table which has columns with fixed lenght, Will mySQL count
is is possible in postgres to have a trigger on CREATE TABLE that will
I have a MySQL table, in which I will be storing the email_id of
I have a pivot table, well of course every row will be included in

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.