I have a table for updating times for a game, in which columns are unique to avoid duplicates.
My problem is the following: how to update a particular column only and avoid inserting a new row if they are the same?
Here is the query.
I did this code and it still enters a new row instead of updating the Time Column only:
INSERT INTO `leaderboard` (`Persona`, `Level`, `Track`, `Car`, `Mode`, `Time`,
`Date`, `Timestamp`, `HMS`)
VALUES ('STIG', 80, 'SPA FRANCORCHAMPS',
'AUDI R8 LMS ULTRA', 'HD', '02:17.332',
'2014-12-06', '1417825487', '1:24:47')
ON DUPLICATE KEY UPDATE `Time` = '02:17.332';
You are actually creating a composite unique key in:
ALTER TABLE `leaderboard`
ADD UNIQUE KEY `Persona` (`Persona`,`Level`,`Track`,`Car`,`Mode`,`Time`,`Date`,`Timestamp`,`HMS`);
It means that the tuple (Persona, Level, Track, Car, Mode, Time, Date, Timestamp, HMS) will be unique. However, the time columns are to be updated, and it is very likely they won't be unique.
Maybe what you want to do is:
ALTER TABLE `leaderboard`
ADD UNIQUE KEY `Persona` (`Persona`,`Level`,`Track`,`Mode`);
Related
Need to insert same Date of Birth for Different person. Ie. I have created a web page where student registers. in MySQL I have created DOB field and if any student with same dob inserted it tells Duplicate entry and record is not inserted. i need to insert the record for DOB
INSERT INTO `degree` (`Candidate`, `Father`, `Course`, `Year`, `DOB`, `Roll`)
VALUES ('AAAA\r\n', 'AAAAA\r\n', 'AAA', '2199', '1933-06-21', 'AAAAAAA')
MySQL said:
Documentation
#1062 - Duplicate entry '1933-06-21' for key 'DOB'
You have a unique key on the DOB field that needs to be removed.
Find the name of the index in the output of
show create table degree
and then remove the unique key with:
alter table degree drop index NAME_OF_INDEX
There's a good chance you still want an index on the field, just not a unique one in which case you can re-add it wth
alter table degree add index (DOB)
Hello I am using the "INSERT ON DUPLICATE KEY UPDATE" sql statement to update my database.
All was working fine since I always inserted an unique id like this:
INSERT INTO devices(uniqueId,name)
VALUES (4,'Printer')
ON DUPLICATE KEY UPDATE name = 'Central Printer';
But for now, I need to insert elements but I don't insert a unique id, I only insert or update the values like this:
INSERT INTO table (a,b,c,d,e,f,g)
VALUES (2,3,4,5,6,7,8)
ON DUPLICATE KEY
UPDATE a=a, b=b, c=c, d=d, e=e, f=f, g=g;
Have to say that an autoincrement primary key is generated always that I insert a row.
My problem is that now the inserted rows are duplicated since I don't insert the primary key or unique id explicitly within the sql statement.
What I am supposed to do?
For example, maybe I need to insert the primary key explicitly? I would like to work with this primary autoincremented key.
For recommendation from Gordon I am adding a sample case the you can see in the next image
Rows Output
In this case I add the first three rows, and then I try to update the three first rows again with different information.... Ok I am seeing the error... There is no key to compare to...... :$
Thanks for your answers,
If you want to prevent columns from being duplicated, then create a unique index or constraint on them. For instance:
create unique index unq_table_7 on table(a, b, c, d, e, f, g);
This will guarantee that the 7 columns -- in combination -- are unique.
I am trying to check if a session exists for a customer so that I can either update the session with new session details or insert a session for a customer. I am using the statement below:
INSERT INTO sessions (customerid, productlist, date)
VALUES('33', '{"68":1,"72":1}', '2016-03-03 13:54:56')
ON DUPLICATE KEY UPDATE customerid=VALUES(customerid)
When I run this, the statement inserts a session for customer even if a session already exists.
ON DUPLICATE KEY UPDATE requires an UNIQUE INDEX on the table. It allows updating the already existing row when the INSERT will fail because of the UNIQUE INDEX.
The candidate for UNIQUE INDEX in your sessions table is customerid. Do you have such an index?
Anyway, the query you posted doesn't make any sense. ON DUPLICATE KEY UPDATE kicks in when you want to insert a new row and the value you want to put in customerid already exists in the table. The UNIQUE INDEX prevents the insertion and ON DUPLICATE KEY UPDATE allows the updating of some of the other columns of the existing row.
When customerid is the UNIQUE INDEX, customerid=VALUES(customerid) is a no-op.
What you probably want is:
INSERT INTO sessions (customerid, productlist, date)
VALUES('33', '{"68":1,"72":1}', '2016-03-03 13:54:56')
ON DUPLICATE KEY UPDATE productlist=VALUES(productlist), date=VALUES(date)
The SQL statement to make customerid an UNIQUE INDEX of table sessions is:
ALTER TABLE `sessions`
ADD UNIQUE INDEX `customerid` (`customerid`)
Or, even better, make is the PRIMARY KEY of the table (if the table doesn't already have one:
ALTER TABLE `sessions`
ADD PRIMARY KEY (`customerid`)
Make sure you create an UNIQUE key by using: customerid and pdoructlist columns.
I've been reading up on how to use MySQL insert on duplicate key to see if it will allow me to avoid Selecting a row, checking if it exists, and then either inserting or updating. As I've read the documentation however, there is one area that confuses me. This is what the documentation says:
If you specify ON DUPLICATE KEY UPDATE, and a row is inserted that would cause a duplicate value in a UNIQUE index or PRIMARY KEY, an UPDATE of the old row is performed
The thing is, I don't want to know if this will work for my problem, because the 'condition' I have for not inserting a new one is the existence of a row that has two columns equal to a certain value, not necessarily that the primary key is the same. Right now the syntax I'm imagining is this, but I don't know if it will always insert instead of replace:
INSERT INTO attendance (event_id, user_id, status) VALUES(some_event_number, some_user_id, some_status) ON DUPLICATE KEY UPDATE status=1
The thing is, event_id and user_id aren't primary keys, but if a row in the table 'attendance' already has those columns with those values, I just want to update it. Otherwise I would like to insert it. Is this even possible with ON DUPLICATE? If not, what other method might I use?
The quote includes "a duplicate value in a UNIQUE index". So, your values do not need to be the primary key:
create unique index attendance_eventid_userid on attendance(event_id, user_id);
Presumably, you want to update the existing record because you don't want duplicates. If you want duplicates sometimes, but not for this particular insert, then you will need another method.
If I were you, I would make a primary key out of event_id and user_id. That will make this extremely easy with ON DUPLICATE.
SQLFiddle
create table attendance (
event_id int,
user_id int,
status varchar(100),
primary key(event_id, user_id)
);
Then with ease:
insert into attendance (event_id, user_id, status) values(some_event_number, some_user_id, some_status)
on duplicate key
update status = values(status);
Maybe you can try to write a trigger that checks if the pair (event_id, user_id) exists in the table before inserting, and if it exists just update it.
To the broader question of "Will INSERT ... ON DUPLICATE respect a UK even if the PK changes", the answer is yes: SQLFiddle
In this SQLFiddle I insert a new record, with a new PK id, but its values would violate the UK. It performs the ON DUPLICATE and the original PK id is preserved, but the non-UK ON DUPLICATE KEY UPDATE value changes.
I am trying to use INSERT IGNORE INTO to add a row to a table if it doesn't already exist.
Here is the statement as it stands right now:
INSERT IGNORE INTO my_table (integer, date) VALUES (11111, CURDATE())
However, since I have an auto-incrementing primary key on the table (that is not part of the insert of course), it always does the insert. Is there a way to disregard the primary key so that if the integer and date are already in the table it will not insert another row with them?
Put a UNIQUE key on the integer and the date, or the combination of the two - whichever fits your needs. That will prevent INSERT IGNORE from inserting values that violate the UNIQUE index.
For example if you want to make the combination of the two unique:
alter table my_table add unique index(integer, date)