Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mysql insert on duplicate FIELD instead of KEY

Tags:

------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1       | foo       | 5           | ------------------------------------- | 2       | bar       | 12          | -------------------------------------

user_id: auto increament, user_visits: default 1

INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux');

the above statement will of course insert 3 new records, as the result:

------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1       | foo       | 5           | ------------------------------------- | 2       | bar       | 12          | ------------------------------------- | 3       | baz       | 1           | ------------------------------------- | 4       | bar       | 1           | ------------------------------------- | 5       | qux       | 1           | -------------------------------------

but what I'm trying to achieve is:

------------------------------------- | user_id | user_name | user_visits | ------------------------------------- | 1       | foo       | 5           | ------------------------------------- | 2       | bar       | 13          | ------------------------------------- | 3       | baz       | 1           | ------------------------------------- | 4       | qux       | 1           | -------------------------------------

so literally,

if field user_name exists, update user_visits, else insert a new record.

is it possible to achieve this with a single insert statement?

like image 781
pepsi600 Avatar asked Apr 26 '11 05:04

pepsi600


People also ask

How do I use insert on duplicate key update?

The Insert on Duplicate Key Update statement is the extension of the INSERT statement in MySQL. When we specify the ON DUPLICATE KEY UPDATE clause in a SQL statement and a row would cause duplicate error value in a UNIQUE or PRIMARY KEY index column, then updation of the existing row occurs.

How do I ignore duplicate keys in MySQL?

Use the INSERT IGNORE command rather than the INSERT command. If a record doesn't duplicate an existing record, then MySQL inserts it as usual. If the record is a duplicate, then the IGNORE keyword tells MySQL to discard it silently without generating an error.

Can primary key have duplicate values in MySQL?

When you insert a new row into a table if the row causes a duplicate in UNIQUE index or PRIMARY KEY , MySQL will issue an error. However, if you specify the ON DUPLICATE KEY UPDATE option in the INSERT statement, MySQL will update the existing row with the new values instead.

How do I insert duplicate rows in SQL?

Use the context menu on the same table, to get another script: "Script Table as | SELECT To | New Query Window". This will be a totally standard select list, with all your fields listed out. Copy the whole query and paste it in over the VALUES clause in your first query window. This will give you a complete INSERT ...


2 Answers

Sure there is but it has nothing to do with your insert statement. You need to add a unique index on the user_name column:

create unique index user_name_idx on yourtable (user_name); 

Then afterward in your code that tracks the count will have to decide whether to do an insert or an update.

like image 73
Wes Avatar answered Oct 18 '22 21:10

Wes


You have to create a key for your username field and then use INSERT ON DUPLICATE query to update the columns values.

For example your query must be,

   INSERT INTO table (user_name) VALUES ('baz'), ('bar'), ('qux')     ON DUPLICATE KEY UPDATE user_visits=user_visits+1; 

For further reference visit http://dev.mysql.com/doc/refman/5.0/en/insert-on-duplicate.html

like image 41
Vijay Avatar answered Oct 18 '22 19:10

Vijay