I want to copy a column (definition, not data) from one table to another. Add the column if it doesn't exist in destination table, or better yet, modify it accordingly if it exists.
Tried to google but everything seems to be about either copying the data rather than the definition, or about copying an entire table using CREATE TABLE ... LIKE ...
.
Thus far the closest I could find is this:
CREATE TABLE table2 AS
SELECT field4, field7 -- only the columns you want
FROM table
WHERE FALSE; -- and no data
But I need to copy columns to existing tables. So I tried:
ALTER TABLE table2 AS
SELECT field4, field7 -- only the columns you want
FROM table
WHERE FALSE; -- and no data
But apparently it refused to work.
Is there something like this?
ALTER TABLE table1 ADD COLUMN column2 LIKE table2.column5
Any way to achieve this in MySQL?
Click the tab for the table with the columns you want to copy and select those columns. From the Edit menu, click Copy. Click the tab for the table into which you want to copy the columns. Select the column you want to follow the inserted columns and, from the Edit menu, click Paste.
Note, that you can specify the columns to be copied. INSERT INTO target_table (`column1`, `column2`) SELECT `column1`, `column2` FROM source_table; This method works perfectly well if you have already copied the table structure like we did in the example above and now need to copy values.
Right-click on the database name, then select "Tasks" > "Export data..." from the object explorer. The SQL Server Import/Export wizard opens; click on "Next". Provide authentication and select the source from which you want to copy the data; click "Next". Specify where to copy the data to; click on "Next".
Move or copy just the contents of a cell You can also edit and select cell data in the formula bar. Select the row or column that you want to move or copy. In the cell, click where you want to paste the characters, or double-click another cell to move or copy the data. or press Ctrl+V.
The best way is to Dump the table and then you can use ALTER command to create the new table.
Dumping the table gives back the query to CREATE the table.
CREATE TABLE IF NOT EXISTS customer (
id int(11) NOT NULL AUTO_INCREMENT,
customerId varchar(50) NOT NULL,
firstName varchar(30) NOT NULL,
middleName varchar(30) DEFAULT NULL,
lastName varchar(30) CHARACTER SET utf8 COLLATE utf8_estonian_ci DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
USE the same to ALTER an existing table, (say customerinfo
)
`ALTER TABLE customerinfo ADD COLUMN (
customerId varchar(50) NOT NULL,
firstName varchar(30) NOT NULL,
middleName varchar(30) DEFAULT NULL,
lastName varchar(30) CHARACTER SET utf8 COLLATE utf8_estonian_ci DEFAULT NULL
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With