简体   繁体   中英

Check if a value exists before updating a column for a already existing record in mysql

Lets say I have table called Tokens , with columns: id , refresh_token , and token . refresh_token column should be unique. But we may have many refresh_token fields that are null. These null refresh_token fields should be updated later, but there is a possibility that we may try to enter a refresh_token value that is contained in another field, and that will cause a conflict.

So, what I wanted to do is: first check if the refresh_token value is contained in any other fields. If it is not contained or if unique, the that new refresh_token value can be used to update a specific row with a given id. If the value is not unique then we can do something else.

This is how I did it, but i am not sure if it is the best way. Maybe you can advise and give a little explanation about your answer.

if con.query("(SELECT id FROM Tokens WHERE refresh_token='#{refresh_token}') LIMIT 1;").count == 1
          # do something important here
        else
          con.query("UPDATE Tokens SET refresh_token='#{refresh_token}' WHERE id='#{id}';")
        end

this is done using ruby.

You can use the following statements to perform a check before updating.

IF (NOT EXISTS(SELECT * FROM Tokens WHERE Token = ''))  
    BEGIN 
        INSERT INTO Tokens(token,..,..) 
        VALUES(val1,val2,val3) 
    END 
    END

OR

  IF (EXISTS(SELECT * FROM Tokens WHERE Token = ''))  
        BEGIN 
            update Tokens
           SET token = myVal
        END 
        END

Not sure what you need. Hope any one of the above helps you :)

You can perform the update like this:

UPDATE Tokens
SET refresh_token = 'C08Rbs'
WHERE id = '2'
AND NOT EXISTS (SELECT refresh_token FROM Tokens WHERE refresh_token = 'C08Rbs')

Use ROW_COUNT() to determine if any rows were actually updated. Assuming the row where id = 2 actually exists, it will return 0 if no rows were updated (because the value already exists).

The ROW_COUNT() function is also provided in the MySQL API (as mysql_affected_rows() ), so your programming language of choice will have a function to return the same thing, and generally the result of an UPDATE will return the number of affected rows.

Normally, you use a unique index to avoid duplicates on a particular column or columns and simply try to update:

UPDATE IGNORE Tokens
SET refresh_token = 'C08Rbs'
WHERE id = 2

Using IGNORE keeps you from getting an error on a duplicate value, the update won't occur, and it will still return the number of rows actually updated. It will return 1 on success and 0 on failure.

It's still strange that you're changing a "NULL" value rather than inserting a new record.

You haven't said what you want to do if the value already exists.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM