1

I am working on redesigning of a legacy db and I have set new names to columns of old db. So, for instance, if olddb.oldtable under dbold has column descr, I have set it as description in new newdb.netable for column.

How can I mention individual columns in my query?

I am using MYSQL

Update: Both Databases are on different IP Addresses and I am using Navicat to transfer data.

Volatil3
  • 14,253
  • 38
  • 134
  • 263

2 Answers2

0

You can insert all columns at once without the need to mention the names using this:

INSERT INTO newtable (SELECT * FROM oldtable);

It will make an 1x1 match independently of column names.

If types don't match then will insert default values (not checked for all the type combination).

Note that column number must be the same on both tables otherwise an error like this will occur:

 #1136 - Column count doesn't match value count at row 1
Kostas Mitsarakis
  • 4,772
  • 3
  • 23
  • 37
0

You can try like this:

INSERT INTO newtable (col1, col2, ..., )
SELECT col1, col2, ..., FROM oldtable

By trying the above query you can insert the specific column. So for example if your newtable has a column as description and old table as descr then you can mention it like:

INSERT INTO newtable (col1, col2, `description`,  ..., )
SELECT col1, col2, `descr` ,..., FROM oldtable

Also if the table column list is large and you want to copy all the columns and its data then you can simply use the wildcard charater * as:

INSERT INTO newtable
SELECT * FROM oldtable;
Rahul Tripathi
  • 168,305
  • 31
  • 280
  • 331