MySQL replicates table structure, table data, and modify primary key

Source: Internet
Author: User
Tags mysql view

MySQL View table structure command

Show columns from table name;

1, copy table structure and data to new Table CREATE table new Table select * from the old table This method will copy all the contents of oldtable, of course, we can use delete from newtable; But one of the worst things about this approach is that the new table has no properties such as primary key, Extra (auto_increment), and so on.     You need to use "alter" to add, and it's easy to mistake. 2. Copy table structure to new Table CREATE table new Table select * from old table where 1=2 or CREATE table new table like old table, this also need to change primary key change primary key: ALTER TABLE TB add primary ke Y (Id,di2);

ID,ID2 indicates that both columns are treated as primary keys, as well as one as the primary key.

Add field 3 alter table tbname add  field 3 varchar (10); Delete primary key   alter table  tbname drop primary key; Add primary key  alter table tbname add primary key (field-by-column);

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.