Mysql copies the table structure, table data, and modifies the primary key.

Source: Internet
Author: User
Mysql command to view the table structure showcolumnsfrom table name; 1. Copy the table structure and data to the new table CREATETABLE new table SELECT * FROM old table this method will copy all content in oldtable, of course, we can use deletefromnewtable; to delete it. However, the worst thing about this method is that the new table does not have

Mysql command to view the TABLE structure show columns from TABLE name; 1. Copy the TABLE structure and data to the new table create table new table select * FROM old TABLE this method will copy all content in oldtable, of course, we can delete it using delete from newtable. However, the worst thing about this method is that the new table does not have

Mysql Command for viewing table Structures

Show columns from table name;

1. Copy the table structure and data to the new table
Create table new table select * FROM old TABLE
This method will copy all content in oldtable. Of course, we can use delete from newtable; to delete it.
However, the worst thing about this method is that the new table does not have the primary key, Extra (auto_increment) and other attributes of the old table. You need to use "alter" to add it yourself, and it is easy to make a mistake.


2. Only copy the table structure to the new table
Create table new table select * FROM old table where 1 = 2
Or create table new table like old TABLE, which also needs to change the primary key

Change primary key: Alter table tb add primary key (id, di2 );

Id. id2 indicates that the two columns are treated as the primary key at the same time, or you can use one of them as the primary key.

Add Field 3 alter table tbname add field 3 varchar (10 );
Delete the primary key alter table tbname drop primary key;
Add the primary key alter table tbname add primary key (Field 1, 2, 3 );

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.