Query to insert rows from CSV to MySQL - c#

I have 2 tables employees and job in a MySQL server. I have 2 CSV files with the same schema as the two tables. Something as below.
Job table(MySQL)
Job(CSV)
Employees(MySQL)
Employees(CSV)
I need to insert the rows from CSV into the MySQL tables. Note that the Id fields in both the tables are on AUTOINCREMENT. I am aware that MySQL does not support merge query and instead has INSERT.. ON DUPLICATE UPDATE. Now the issue with using this is that the Primary keys i.e., the Ids are definitely duplicate but I need them to be inserted as new rows with new Ids. For example, the Admin role should have the Id as 3 and so on. Also, as you can see, the job table is being referenced by the employees table. So the the JobId column in the table should be updated with the respective values. I am not sure how this can be achieved.
Any help appreciated!

Related

Fastest way to get the distinction/delta between a local table and the values of a database table

I have the following requirements:
I have many different database tables for example a table which shows different materialnumbers. There are many thousands rows in the database table (>100.000)
A user could import data with an excel file with thousends of rows.
The distinction/ difference between the excel file and the database-table should be added. The allready existing materialnumbers should be updated.
Is there a faster way than this to get the allready existing values in entity framework:
var allreadyExistingMaterialnumbersInDatabase = myDbContext.Materialnumbers.Where(x=>myExcelListMaterialnumbers.Contains(x.Materialnumber)).ToList();
After that step i have to do a update for all the data which is returned in the code example and then i have to insert all the difference between the list allreadyExistingMaterialNumbersInDatabase and myExcelListMaterialnumbers.
Thanks for your help.

How to insert data from 2 related tables into another 2 related tables using triggers?

I need help with this one..
I have 2 tables that are related in a one-to-many relationship.
Table 1:
IDHeader(PK), name, number,..... so on
Table 2:
IDItem(PK), IDHeader(FK), names, numbers,... so on..
I've created 2 identical tables which serve as "backup" tables:
Backup Table 1:
Backup ID, IDHeader, name, number,..... so on
Backup Table 2:
Backup ID, IDItem, IDHeader, name, number,..... so on
(Note: I added BackupID so that would be a PK - FK relation in those backup tables. Or is it wrong? I can use the original relations from original tables? If so, how do I fill backup tables and keep the relations)
Now, the first 2 tables are almost identical to "backup" tables.. They only differentiate in few additional columns.
First 2 tables have relations, and they work fine (they are filled with data from SharePoint, but that's not important now).
I've written a trigger on Table1 that will copy the data from it to it's backup counter part.. So trigger that fills Backup Table 1 with data from Table 1..
I also wrote a trigger on Table2 that does the same thing, copy from Table 2 to Backup table 2..
My question is:
How to insert data from Table1 and Table2 into Backup Table 1 and Backup Table 2 so that those backup tables have relations as the Tables 1 and 2...
I am quite novice at SQL, so I'm interested if this kind of thing can be executed programmatically with triggers? Should it be 2 triggers for each table, or 1 trigger on Table 1 that will also fill data like: Table 1 -> Backup Table 1 | Table 2 -> Backup Table 2
I use GUID's for id's (I know, but not my idea)
Any comment, guidance or different approach or solution is welcome!!
Sorry for long text!

SQL batch insert, avoid duplicates, no PK

I was given a task to insert over 1000 rows with 4 columns. The table in question does not have a PK or FK. Let's say it contains columns ID, CustomerNo, Description. The records needed to be inserted can have the same CustomerNo and Description values.
I read about importing data to a temporary table, comparing it with the real table, removing duplicates, and moving new records to the real table.
I also could have 1000 queries that check if such a record already exists and insert data if it does not. But I'm too ashamed to try that out for obvious reasons.
I'm not expecting any specific code, because I did not give any specific details. What I'm hoping for is some pseudocode or general advice for completing such tasks. I can't wait to give some upvotes!
So the idea is, you don't want to insert an entry if there's already an entry with the same ID?
If so, after you import your data into a temporary table, you can accomplish what you're looking for in the where clause of a select statement:
insert into table
select ID, CustomerNo, Description from #data_source
where (#data_source.ID not in (select table.ID from table))
I would suggest to you to load the data into a temp table or variable table. Then you can do a "Select Into" using the distinct key word which will removed the duplicated records.
you will always need to read the target table, unless you bulk load the target table into a temp table(in this point you will have two temp tables) compare both, eliminate duplicates and then insert in target table, but even this is not accurate, because you can have a new insert in the target table while you do this.

UPDATE TableA.FK VALUES TableB.PK WHERE TableA.Column = TableB.Column

I'm trying to merge tables from different databases, ServerDB and ClientDB and save to ClientDB where the client's tables hold the master's tables records plus any records the user might add. On the other hand server tables could also be updated (new records inserted).
The database has relationships. The column in TableA I want to INSERT INTO values is a ForeignKey linking to TableB's PrimaryKey which is an auto-incremental column.
I'm saving all records from both databases in a merged dataset, and due to database design restrictions, I need to clear both tables on the client before inserting the merged tables from the dataset.
I first update TableB (the one with the PrimaryKey auto-increment column), but now the values of this column have nothing to do with the ForeignKey on TableA, so I update TableA and temporary inserting in the ForeignKey column the value of the first record of TableB's PK. Now I need to update TableA foreignKey column with the correct values from TableA PK column. Theres also a third column on each table that have the same values.
What the syntax of the sql statement should be? If I don't make much sense let me know and I'll post a better description.
It was a confussing question but i think you are talking about inserting values from table 1 to table 2 where table 1 value is equal to table 2 values the sql query for this operation is
INSERT INTO emp (empno,ename)
SELECT t2.deptno,
t2.dname
FROM dept t2
LEFT JOIN emp t1
ON t2.deptno = t1.deptno
in this query table 1 (emp) will insert 2 values into columns(empno and ename) from
table 2 (dept) and join is on (deptno) which is present in both tables.
You can further ask if this was not helpful.
Thank you all for your help. After struggling for a while with the "OledbException Operation must use an updateable query" I found out a solution in a similar topic: SQL Update woes in MS Access - Operation must use an updateable query
Thats the query that did the trick:
UPDATE DISTINCTROW PlaylistsSongs
INNER JOIN PlaylistsNames ON PlaylistNames.PlaylistName = PlaylistsSongs.PlaylistName
SET PlaylistID = PlaylistNames.ID

Clone DB table row through MVC in SQL Server

Is there a simple solution for duplicating table rows in SQL Server as well as all table rows with foreign keys pointing to the cloned table row? I've got a "master" table and a bunch of "child" tables which have a foreign key into the ID of the master table. I need to not only create a perfect copy of the master table, but clone each and every child table referencing the master table. Is there a simpler way to do this than creating a new row in the master table, copying in the information from the row to be cloned, then going through each child table and doing the same with each row pointing to the cloned row in the master table?
I'm using a SQL Server 2005 Database accessed through C# ASP.net MVC 1.0.
If by "simple" you mean is there is a procedure that can be called to do it, no there is not. However, you can use the INFORMATION_SCHEMA views such as INFORMATION_SCHEMA.COLUMNS and INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS to query for the columns in a table or the list of related tables and dynamically build your INSERT statements to copy one row to another. Of course, this does not account for other uniqueness constraints that might be on the tables (e.g. a table with a Name column which requires that the values be unique).

Categories

Resources