Trying to copy or somehow move the contents (values) in a TEXT column 3k+ large rows to another table in the same database without success - mysql

I have created a new column in the "destination" table with the same name, datatype and other values as appear in the "source" column in a different table. I have tried many suggested solutions as found on stackoverflow. This one appeared to work (found on Quora) but when I went to the destination table the previously empty column remains empty with nothing but NULL values noted. This is the Quora suggestion:
you can fill data in column from another existing one by using INSERT INTO statement and SELECT statement together like that
INSERT INTO `table1`(column_name)
SELECT column_name FROM `table2`
here you filled a single column in table 1 with data located in a single column in table 2
so if you want to fill the whole table 1 (all columns) with data located in table 2 you can make table 1 like a copy of table 2 by using the same code but without column name
INSERT INTO `table1`
SELECT * FROM `table2`
but note to do this (copy table content to another one) ensure that both of tables have the same column count and data types.
I'm not sure what is meant by column count (do the two table have to have the same number of columns?)
When I run it I get error # 1138.
Any help greatly appreciated. -JG

Related

SSIS- Update few columns of a row for which the primary key already exists

The following is an example to better explain my scenario. My database table has following columns
Column -1: Operating_ID (which is the primary key)
Column -2: Name
Column -3: Phone
Column -4: Address
Column -5: Start Date
Column -6: End Date
The values for the columns 1,2,3,4 come from an extract and this extract is pushed to the database daily using SSIS data flow task
The values for the columns 5 and 6 are user inputted from a web applicxation and saved to the database.
Now in SSIS process instead of throwing violation of primary key error, i need to update Columns 2,3,4 if the primary key i.e column 1 already exists.
First i considered replace but that deletes the user inputted data columns 4,5.
I would like to keep the data in columns 4,5 and update columns 2,3,4 when column 1 already exists.
Do a LOOKUP for Operating_ID. Change that lookup from "FAIL ON NOT FOUND" to "REDIRECT ROWS TO NO MATCH"
If match not found, go to INSERT
If match found, go to UPDATE. You can run OLAP commands to update, but if it is a large set of data, you are better off putting into a table, and doing an UPDATE with a JOIN
This is what I would do. I would put all the data in a staging table. Then I woudl use a data flow to insert the new records and the source of that dataflow would be the staging table with a not exists clause referencing the prod table.
Then I would use an Execute SQL task in the control flow to update the data for existing rows.

Replace one column of a table with another column of another table in SQL

I have a table with several columns Table1(Col A, Col B)
Now I have one more table with one column. Table2 (Col C)
What I want to do is:
Replace Col B of table1 with Col C of tabl 2.
Is it possible in SQL? I am using phpmyadmin to execute queries
Why I need to do this?
- I was playing around with the database structure and changed the type of text to integer which messed up the entries in the column
- Good thing: I have a backup excel file so now i am planning to replace the effected column to by the orginal values in the backedup excel file.
No can do.
You seem to be making an incorrect assumption, namely that the order of rows in a table is significant. Else what's confusing some of the commenters would be clear to you: there's no information in table2 to relate it to table1.
Since you still have the data in Excel, drop table2 and re-create it with rows having the key to table1. Then write a view to join them. Easiest is probably to insert that join result into a third table, and then drop the first two and rename the third.

Mysql column count doesn't match value count fix for unattended

I have to import loads of files into a database, the problem is, with time it got more columns.
The files are all insert-lines from SQLite, but i need them in MySQL, SQLIte doesn't provide column-names in their sql files, so the MySQL-script crashes when there are more or less columns as in the insert statement.
Is there a solution for this? Maybe over a join?
The new added columns are in the end, so the first are ALWAYS the same.
Is there any possibility to insert the sql-file in a temporary table, then make a join on an empty table (or 1 ghost record) to get the right amount of columns, and then do a insert on each line from that table to the table i want to have the data in?
Files looks like:
INSERT into theTable Values (1,1,Text,2913, txt,);
And if columns were added the file is like
INSERT into theTable Values (1,1,Text,2913, txt,added-Text);

MySQL insert into table not inserting all fields

I have 2 existing tables in a MySql DB. The tables have identical structures. I want to copy data from table to another.
insert into `Table1`
select * from Table2
where department = "engineering"
the above code seemed to work and it copied the data correctly except for 1 column. The "department" column did not copy over so it was blank. All the other fields seemed to copy over correctly for all of the records.
What can be causing this? As I mentioned both tables have identical structures, same number of columns and everything...
Any ideas?
Note:I just realized that there are actually 2 columns that are not copying over. The "department" and "Category" fields come over blank. So basically when I am inserting the data from table 2 into table 1, 12 out of 14 columns are successfully copied over but then there are 2 columns that remain blank.
Below is the DESCRIBE of Table1 and Table2
The only difference I can see when I do a Describe on both tables is that the 2 fields in question have a data type of enum (.....) but they have differences in between the parenthesis. Could this be causing the issue and if so is there a simple way around it? I'm thinking I might have to do an update query after I do the initial insert that will bring in the "department" and "category" fields from table 2 into table 1 by joining in the ID field.
From the docs:
If you insert an invalid value into an ENUM (that is, a string not
present in the list of permitted values), the empty string is
inserted instead as a special error value.
Read about ENUM.

INSERT ... SELECT to same table

I have a table that has a number of columns. For each row, I'd like to select three columns (PAR_BOOK, PAR_PAGE, PAR_LINE) and concatenate the contents of those three columns into a new fourth column (APN).
So, if PAR_BOOK=0108, PAR_PAGE=291 and PAR_LINE=07, APN should be 010829107
Make sense?
But, I'm unsure of what query I should use to do this. I need the results stored back in the same table as it needs to be ultimately exported out as a csv to work with the program that's going to map the data.
Assuming your fourth column is already in the table, you would use the following update query:
UPDATE YourTable
SET APN = CONCAT(PAR_BOOK, PAR_PAGE, PAR_LINE)
If your fourth column is not present in the table yet, you should use the ALTER TABLE statement to add it first before running the UPDATE statement:
ALTER TABLE YourTable
ADD APN VARCHAR(256) NULL
Inserting into the same table with INSERT INTO ... SELECT ... is no problem at all. MySQL holds the selected rows in a temporary table.