Insert query for Foregin key mysql python Script - mysql

I want to write a script for data migration on another DB, my python script is first to read and then insert in bulk, but I have an issue with the foreign key, where incident_number is the primary key for the reading table and the foreign key for Insert table, how to insert proper for incident_number
mycursor.execute("SELECT incident_category_id,incident_number,org_id_id FROM `internal_incident_incidentcreationinfo`")
IncidentMaster=[]
columns=tuple([d[0] for d in mycursor.description])
for row in mycursor:
IncidentMaster.append(dict(zip(columns, row)))
for incident_data in IncidentMaster:
sql= "INSERT INTO internal_incident_historicalmultiplecategorylist(incident_category_id,incident_number_id,org_id_id) VALUES(%s,%s, %s)"
values=[(str(incident_data['incident_category_id']), str(incident_data['incident_number']), str(incident_data['org_id_id']))]
mycursor1.executemany(sql,values)
connection1.commit()
the problem within select table row field incident_number
while in INSERT TABLE row in incident_number_id

You should collect all the values into a single list, then call executemany() once with that complete list. mycursor.fetchall() will do this automatically for you.
There's no need to convert the values to strings.
mycursor.execute("SELECT incident_category_id,incident_number,org_id_id FROM `internal_incident_incidentcreationinfo`")
values = mycursor.fetchall()
sql= "INSERT INTO internal_incident_historicalmultiplecategorylist(incident_category_id,incident_number,org_id_id) VALUES(%s,%s, %s)"
mycursor1.executemany(sql,values)
connection1.commit()

Related

Insert or update (if row with key exists) a pack of rows

My aim is to upload csv data to myisam table: if any row in myisam table has primary key duplicate, update some fields, otherwise - insert new row. For simple inserting i was reading csv line by line, and after every 5000th line i was making an INSERT query like
INSERT INTO table VALUES (), (), (), () ..5k times..,()
For my current aim, i should use query like
INSERT INTO table VALUES (), (), ()...,() ON DUPLICATE KEY UPDATE field1=value1, field2=value2, ...
but it will use value1 and value2 for each row to update. I want it to update with respective values. Hope you understand all written before...
You can use references to VALUES to do this:
INSERT INTO `user`(`username`, `password`) VALUES ('ersks','Nepal'), ('segesg','esgessgs'), ('segsegss','segsege')
ON DUPLICATE KEY UPDATE `username`='master',`password`=VALUES(b);
See docs here https://dev.mysql.com/doc/refman/8.0/en/insert-on-duplicate.html

Insert a row only if doesn't exist without getting its id

I followed this tutorial with 'INSERT IGNORE' first and 'INSERT ... ON DUPLICATE KEY UPDATE' then. But it doesn't work.
I'm using NodeJS to get some data from an API, and store these data into a mySQL database. Before storing data, I want to know if this row already exists.
The ID is AUTO_INCREMENT and I d'ont know this one. Instead of using async/await or promises in NodeJS, I wanted to treat this point with mySQL without knowing the ID.
I tried this one but it adds a new row with a new ID instead of another row already exists:
INSERT INTO test (nom, date, heure) VALUES ('123456', '2018-08-23', '10:45:00') ON DUPLICATE KEY UPDATE nom='123456', date='2018-08-23', heure='10:45:00';
After that, I tested this one, same result:
INSERT IGNORE INTO test (nom, date, heure) VALUES ('123456', '2018-08-23', '10:45:00')
Thanks for your help!
Set a unique index for the 3 columns:
ALTER TABLE test ADD UNIQUE un_index(nom, date, heure);
Then execute:
INSERT IGNORE INTO test (nom, date, heure) VALUES ('123456', '2018-08-23', '10:45:00');
If the 3 values already exist, the row will not be inserted.

IF NOT EXISTS then INSERT

I'm trying to add a value to a table but not without checking if the value already exists. This is what I have so far:
IF NOT EXISTS (
SELECT series.seriesName
FROM series
WHERE series.seriesName='Avengers'
)
BEGIN
INSERT INTO series (seriesName) VALUES 'Avengers'
END;
Database is a MySQL db on Ubuntu
You can use IGNORE keyword here.
It could look like:
INSERT IGNORE INTO series (seriesName) VALUES 'Avengers'
The important thing is to create a unique key on seriesName field as it seems that you want it to be unique.
INSERT IGNORE doesn't make the insert when key value already exists.
If you would like to be able to get id (primary key value) for row that wasn't inserted (already existed), you can do the following trick:
INSERT IGNORE INTO series (seriesName) VALUES 'Avengers'
ON DUPLICATE KEY UPDATE seriesID= LAST_INSERT_ID(seriesID)
Then you will be able to get the ID with LAST_INSERT_ID() function no matter if the row was inserted or not.

Insert to table or update if exists using MYSQL database

I have mysql database. I need to update country list on my table. there is some country in my table. I need to check that country if not exist and insert to the table. I'm used following sql script. But this is not working. when execute this code it will duplicate the record.
MySQL Query:
INSERT INTO `moneyexpressstore`.`countries` (`Name`, `Code`, `CurrencyId`) VALUES
('Australia', 'au', NULL) ON DUPLICATE KEY UPDATE Name=VALUES(Name)
thanks,
First make sure your database does not have duplicate records on column countries, then execute this
CREATE UNIQUE INDEX countriesindex ON countries (Name(50))
50 is the amount of characters that the index will search for "unique" values. For example if that number was 3, then these two different string would be considered the same, and a 1062 Duplicate Entry error would occur abcHELLO=abcWORLD and in your case it would force the UPDATE instead of INSERT.
If you get a 1062 error, that means you have duplicates in your db so find them remove them and try again.
After this your query will execute just fine and will update instead of duplicate on "Name"
Have a look in the documentation of mysql https://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html
INSERT INTO table (a,b,c) VALUES (1,2,3)
ON DUPLICATE KEY UPDATE c=c+1;
Try this:
insert into `moneyexpressstore`.`countries` (id, `Name`, `Code`, `CurrencyId`) values(NULL,'Australia', 'au', NULL) on duplicate key update name=values(name)
Please refer this link:
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

How to make insert or delete?

Structure table:
id (int primary key)
name (varchar 100)
date(datetime)
For insert I use query:
INSERT INTO table (name, date) VALUES ('t1','$date');
For delete row I use query:
DELETE FROM table WHERE name = 't1';
I would like want how make 1 query: first insert, if row with it name already exist, than delete row, and insert again.
Tell me please how to make it?
Create a UNIQUE index over your name column:
ALTER TABLE `table` ADD UNIQUE (name);
If you genuinely want to "delete row and insert again", then you can use REPLACE instead of INSERT. As documented:
REPLACE works exactly like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or a UNIQUE index, the old row is deleted before the new row is inserted.
Therefore, in your case:
REPLACE INTO `table` (name, date) VALUES ('t1','$date');
However, if instead of deleting the existing record and then inserting a new one you merely want to update the existing record, you can use INSERT ... ON DUPLICATE KEY UPDATE:
INSERT INTO `table` (name, date) VALUES ('t1','$date')
ON DUPLICATE KEY UPDATE date = VALUES(date);
The most material difference is in the treatment of columns for which you do not provide explicit values (such as id in your example): REPLACE will result in the new record having the default value, whereas INSERT ... ON DUPLICATE KEY UPDATE will result in the old value being retained.
What you want to do is use MySQL's on duplicate update feature.
Can be used like this :
INSERT INTO table (name, date) VALUES ('t1','$date')
ON DUPLICATE KEY UPDATE name=VALUES(name),dateVALUES(date);
Of course for that to happen a dupliate violation must occur.
insert into table (name, date) values('t1','$date') on duplicate key update name=values(name), date=values(date)
Are you looking for an update query?
Update will set a value on an already existing row.
UPDATE table SET date = '$newdate' WHERE name = 't1';
The best way to do this is using the mysql methods together with your query.
If you make the 'name' field unique:
id (int primary key)
name (varchar 100) NOT NULL UNIQUE
date(datetime)
And alter the query to:
INSERT INTO table
(name, date) VALUES ('t1','$date')
ON DUPLICATE KEY UPDATE date = "$date"