0

I have two MySQL tables: one called Audit and the other Audit2. I need to copy the column names from Audit2 to Audit. For example table Audit has columns 1, 2 and 3 and Audit2 has columns 4, 5 and 6 and I need Audit to have columns 1, 2, 3, 4, 5 and 6.

I tried the following to no success:

ALTER TABLE  `Audit` ADD  (select `COLUMN_NAME` from information_schema.columns
where table_schema = 'BMS' and `TABLE_NAME` = 'Audit2') (select `DATA_TYPE` from information_schema.columns
where table_schema = 'BMS' and `TABLE_NAME` = 'Audit2') NOT NULL
2
  • MySQL doesn't have such feature. Just use a GUI tool that implements it, e.g. HeidiSQL. You don't need to do it programmatically, do you? Commented Aug 13, 2015 at 10:18
  • Thanks will try it out. No need to do it programmatically just once Commented Aug 13, 2015 at 10:21

3 Answers 3

1

You can do it by

create table new_audit as
select t1.*,t2.* from audit as t1 inner join audit_2 as t2 on 1=0;

The table new_audit will have all the columns

Sign up to request clarification or add additional context in comments.

Comments

0

Have you tried:

SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='your_table_name';  

It'll return all the column names in a single column from the table you want.

Comments

0

If you have a PHP environment set up, you can download this tool: http://www.mysqldiff.org/downloads.php It's brilliant - will compare two mysql database schema's and generate the mysql code that will sync the structures together, sounds just like what you need and handy for the future too.

If not. I'd do just what you're doing, but probably just simplify it a bit for the sake of a one off instance and 3 columns.

Something like ALTER TABLE Audit ADD yourcolumnname VARCHAR(100);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.