MySQL adds, modifies, and deletes table columns and constraints.

Source: Internet
Author: User

Alter table: add, modify, and delete TABLE columns, constraints, and other TABLE definitions.

View column: desc table name;
Alter table t_book rename to bbb;
Add column: alter table name add column name varchar (30 );
Delete column: alter table Name drop column name;
Alter column name MySQL: alter table bbb change nnnnn hh int;
Modify the column name SQLServer: exec sp_rename 't_ student. name', 'nn ', 'column ';
Modify the column name Oracle: lter table bbb rename column nnnnn to hh int;
Alter table t_book modify name varchar (22 );

Sp_rename: the built-in Stored Procedure of SQL Server, which is used to modify the table definition.

View constraints in MySQL, add constraints, delete constraints, add columns, Modify columns, and delete Columns

View the table field information: desc table name;
View All table information: show create table name;
Add primary key constraint: alter table name add constraint primary key (for example, PK _ table name) primary key table name (primary key field );
Add foreign key constraint: alter table from table add constraint foreign key (for example: FK _ slave table _ master table) foreign key from table (foreign key field) references primary table (primary key field );
Delete primary key constraint: alter table Name drop primary key;
Delete foreign key constraints: alter table Name drop foreign key (case sensitive );

Alter table t_book rename to bbb;
Add column: alter table name add column name varchar (30 );
Delete column: alter table Name drop column name;
Alter column name MySQL: alter table bbb change nnnnn hh int;
Modify the column name SQLServer: exec sp_rename 't_ student. name', 'nn ', 'column ';
Alter column name Oracle: alter table bbb rename column nnnnn to hh int;
Alter table t_book modify name varchar (22 );

Sp_rename: the built-in Stored Procedure of SQL Server, which is used to modify the table definition.

Delete primary key first
Alter table table_test drop primary key;

Then add the primary key.
Alter table table_test add primary key (id );
Alter table test rename test1; -- modify the table name

Alter table test add column name varchar (10) not null; -- add a table column

Alter table wd_express drop column id; -- delete a table column
Alter table wd_express drop column tbid; -- delete a table column
Alter table wd_express change column express_id id int; -- modify the table column name

Alter table wd_express add PRIMARY key (id );

Alter table test modify address char (10) -- modify the table column type
| Alter table test change address char (40)

Alter table test change column address address1 varchar (30) -- modify the table column name

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.