Csv to Mysql INSERT INTO the last row - mysql

I have a problem, when I import a data from .csv file to Mysql, this data is inserted in a new row. I want this data next to the other cells, in the same row.
I have an auto-incremental Id,and the default/expression of the columns is NULL. I don't know where is the problem.
Can you help me?
Thanks!!

Assuming that the imported data rows have some columns that have values in common with the existing data (auto-incrementing IDs won't do it), import the data into a temporary table, then use an UPDATE statement to join your base table to the temporary table on the common columns, and transfer the data from the temporary table to the base table.

Related

How can I import a table by adjusting the record IDs to the existing table?

By default, in phphmyadmin, when exporting a table, there is a string "DROP TABLE IF EXISTS table name;", which means that when importing an exported table, it will replace an existing one (i.e., it will erase the records and insert new ones).
I need to import additional data into an existing table.
But, as I said, the imported table completely replaces the existing one in the database.
And I need THE NEW TABLE TO COMPLEMENT THE "OLD" ONE.
I believe that if the "old" table has the last id=10, then the next id of the imported table will be -11.
(I import the mysqlfile)
Please tell me how to implement this?
I decided to do this:
rename a table that already exists in the database
importing a new table
combine with UNION
You know the easiest way-write

Import Data into MySQL table from CSV file without Overwriting Existing Data

I have a csv file with data that I want to import into a table. Some of the data already exists in the table and I don't want to add a duplicate row.
For example, I have apple in a row in the table, and apple in a row in the CSV file with additional information.
I don't want a new row added to the table or to overwrite any of the data in the existing row; just add the additional data for that row from the CSV file into the table row.
How can I avoid a duplicate row being added or overwriting data, or do I even have to worry about this.
I would use LOAD DATA INFILE IGNORE.
See the docs: http://dev.mysql.com/doc/refman/5.7/en/load-data.html
If you specify IGNORE, rows that duplicate an existing row on a unique key value are discarded.

Duplicate data in SQL

I have to keep duplicate data in my database so my question is...Is it preferable to keep the duplicate data in the same table and just add a column to identify the original data or I have to create another table to hold the copied data?
I suggest to save the duplicate data in a different table or even a different schema so it won't be confusing to keep working with this table.
Imagine yourself in six months form now trying to guess what are all this duplicate rows for.
In addition those duplicate rows does not reflect the business purpose of this table.
It will be nicer to store them in a table named [table_name]_dup or a schema named [schema_name]_dup
To create a backup you should read this
To duplicate a website with it's content. Bad solution but you still have to make a backup and restore it in a different database.
Duplicate a table in mysql:
CREATE TABLE newtable LIKE oldtable;
INSERT newtable SELECT * FROM oldtable;

Customised data load in SSIS

I am trying to design a SSIS Package, where if records in the files come for the first time than
load it to primary table,
else to be load to secondary table.
files from different customer has unique customer id.
i am not sure if it is possible in SSIS.... can anyone please help me on this.
Thanks
You can read data in staging table and then compare ID from staging table with the ID in target table, if matches then insert in secondary table else insert into primary table.
If you add a lookup step in your dataflow, you can compare the data to the table1, if the data does not exist, put it in table1 if so put in table2

mysql: import csv in existing table

Imagine you have a CSV file with only text in it and line ending \r\n.
like this:
abc
def
hij
...
xyz
You want to import this file in an existing multi-column table, where each line of text needs to go in a line of one specific (currently empty) column (lets name it needed) of the table. Like this:
| a | b | c |needed|
|foo|bar|baz|______|<-- abc
|foo|bar|baz|______|<-- def
...
|foo|bar|baz|______|<-- xyz
The data from the CSV file does not need to be inserted in a certain order. It really does not matter which field of needed has which data from the CSV in it, as long as every line gets imported, everything is fine.
I've tried lots of things and its driving me mad, but I can't figure out, how this could be done. Can this be solved somehow with LOAD DATA INFILE and update/replace/insert command? What would you do in a case like this? Is it even possible with mysql? Or do I need a custom php script for this?
I hope the question is clear enough. Please ask, if something is unclear to you.
OK, here you go...
Add a new column to stuff populated with 1-600,000
ALTER TABLE stuff ADD COLUMN newId INTEGER UNIQUE AUTO_INCREMENT;
Create a temp table for your CSV file
CREATE TABLE temp (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
data VARCHAR(32)
);
I'm guessing the required length of the data.
Import into the temporary table
LOAD DATA INFILE <wherever> INTO TABLE temp;
Add the data to stuff
UPDATE stuff AS s
JOIN temp AS t ON t.id=s.newId
SET s.needed=t.data;
Tidy up
DROP TABLE temp;
ALTER TABLE stuff DROP COLUMN newId;
I must admit that I haven't tested the LOAD DATA INFILE or the UPDATE statements, but I have checked all the table fiddling. In particular, MySQL will populate the newId column with consecutive numbers.
I don't think there will be a problem if the number of rows in stuff doesn't match the number of lines in your csv file - obviously, there will be a few rows with needed still NULL if there are fewer lines than rows.
Most easiest way I found was with Libreoffice Calc+Base.
Import/Export Data in Base.
But becarefull,if you have "not null" options in columns and by mistake there is a cell which has no data in it, that row will be skiped.
So first disable the not null options from columns.
Oh and there is Microsoft Office Excel way,but I did not done it since it required a plugin to install and I was lazy.