Basic syntax for Mysql table operations

Source: Internet
Author: User
There will always be a time when you suddenly need to add a field in the table, add an index, or add a field, or modify it to the field. You cannot remember which command to use at a time, now, we can write some of the commands that we usually use for ease of use: 1. add the table field altertabletablenameaddnew_fieldtypenotnulldefault0; exa

There will always be a time when you suddenly need to add a field in the table, add an index, or add a field, or modify it to the field. You cannot remember which command to use at a time, now, we can write some of the commands that we usually use for ease of use: 1. add the alter table tablename add new_field type not null default '0'; exa

There will always be a time when you suddenly need to add a field, add an index, or add a field, or modify it to the field,

I can't remember which command to use at one time. Now I will write some of the commands that I usually use for ease of use:


1. Add fields in the table
Alter table tablename add new_field type not null default '0 ';
Example:
Alter table people_final_base add is_del int (11) not null default 0;

2. Modify the field name in the table
Alter table tablename change old_item new_item old_type;
Example:
Alter table people_final_base change name chinese_name varchar (50 );

3. Modify the field type
Alter table tablename change filed_name new_type;
Example:
Alter table people_final_base change create_date varchar (100 );

4. delete a field
Alter table tablename drop column column_name;
Example:
Alter table people_final_base drop column temp0;

5. Add a primary key
Alter table tablename add primary key (item );
Alter table fashion_relation_partner add primary key (pps_key );

6. Modify the primary key
Idea: First Delete the primary key and then add the primary key
Alter table tablename drop primary key;
Alter table tablename add primary key (item );
Example:
Alter table fashion_relation_partner drop primary key;
Alter table fashion_relation_partner add primary key (pps_key );

7. Set the field Default Value
Alter table fashion_warning_work alter column is_del set default 0;



Write this first, and then add it later!

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.