开发者

how to import the attributes from one table to another in mysql

Is it possible to import the attributes of one table, then I put it into another table using a query in mysql?

For example I have table1 with attributes lname, fname, mname And I want to put those attributes into table2.

开发者_如何学Go

Is there any query that could do that? I'm imagining that the table2 has one attribute that could later be dropped so that it will be the same as table1.


I am not entirely sure what you are asking.

If you want to copy the structure of table1 into a new table, do something like this:

CREATE TABLE table2 LIKE table1;

If you want to copy existing values from one table to another, you can then use the INSERT...SELECT syntax as follows:

INSERT INTO table2 (lname, fname, mname)
     SELECT t1.lname, 
            t1.fname, 
            t1.mname
       FROM table1 t1;


Do you want TABLE2 to look just like an empty TABLE1? If so, you could do

CREATE table2 LIKE table1;


If you use

SHOW CREATE TABLE table1

It will return most of the column syntax.

Then add ADD in front of all the columns you want to append to table 2, and change CREATE TABLE table1 to ALTER TABLE table2

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜