MySQL comparing existing records - mysql

I just want to get suggestions about what is the best way to do the following:
I have a csv file, this file contains users and their information. Before I enter that to the MySQL database, I need to compare the email column of the CSV file with the email column in the database, it if exist, I don't insert it, if it doesn't then I insert it. How would I do this process other than manually? I would highly appreciate ideas.
Just to mention, the way I am doing it is so manually (I know it is stupid but I am not that good with SQL) what I do is, I sign in to my wordpress, go to users, and in the search bar I search for every email to make sure it does not exist.

If there's a unique index on the email column, you can use INSERT IGNORE. This will skip any records that have duplicate keys.

Insert the csv to a tempoary table. then insert into the target table by select all the records that is not exist in the target table from source table by using insert into select.

Related

Insert record into table, if foreign key doesnt exist insert also into another

I have created this database:
and I want to insert data into it.
Those data will come from array and will always be ad_system, ad_campaing, ad_group, keyword, impressions, click, price, conversion.
I think the structure of DB is good. But my app purpose is only to load report data, format them and then save them into DB. So for example I will load first report, and I would want to insert It into DB. That means I need to probably check if foreign key exists in another table(because I am mainly inserting only in Report table, and then insert it into that table and so on ? Is that possible ? and is that right approach ? it seems kinda weird but right.

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;

Cant copy unique records from one database table to another?

Hi,
I am trying to copy unique records from a database table to another table of the same name but different database. The source database contains some records that are already present in the destination database, so those I dont need, only the other ones. Database destination is called "test" and the source database is "forums". The table name is store for both cases. I am using this query:
INSERT INTO test.store (cs_key, cs_value, cs_array, cs_updated,cs_rebuild)
SELECT DISTINCT cs_key, cs_value, cs_array, cs_updated,cs_rebuild
FROM forums.store
But I am getting many errors as I try to run this query. Why?
Thank you.

INSERT INTO statement in MySQL

I'm trying to work with YEAR function on one column in the DB and then add the results to a different table in the DWH.
What am I doing wrong?
INSERT INTO example_dwh1.dim_time (date_year)
SELECT YEAR(time_taken)
FROM exampledb.photos;
When removing the INSERT INTO line, I get the results I want, but I'm not able to insert them into the dwh table.
Thanks for your help!
The following select works, but I don't see the data in the table after the insert:
INSERT INTO example_dwh1.dim_time (date_year)
SELECT YEAR(time_taken)
FROM exampledb.photos;
There is rather broad. Assuming you have no errors in the insert, you might have:
You are incorrectly querying dim_time, so the data is there but your check is wrong.
You are inserting into dim_time in one database but querying it in another.
Assuming you have errors but are missing them, here are some possibilities:
The database does not exist.
The table does not exist.
The column is misnamed.
Other columns are declared NOT NULL.
Triggers defined on the table are preventing the insert.
Unique constraints/indexes on the table are preventing the insert.
Your question does not provide enough information to be more specific. However, it seems highly suspicious to be inserting a bunch of years -- which might include many duplicates -- into a dimension table.

MYSQL: Load Data Infile but update if same key found?

I have a members table. Half the data/fields are populated through an online CMS.
But for the member's core contact detail fields, they come from a CSV exported from a desktop database.
I wanted to be able to upload this CSV and use the LOAD DATA command to update the members contact detail fields (matching on id) but without touching/erasing the other fields.
Is there a way to do this or must I instead loop through each row of the CSV and UPDATE... (if that's the case, any tips for the best way to do it?)
The Load Data Infile command supports the REPLACE keyword. This might be what you're looking for. From the manual:
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
The Load Data Infile command also has options where you can specify which columns to update, so perhaps you can upload the data, only specifying the columns which you want to update.