In mysql, alter is used to modify the data table structure. This is the same as the mssql method. Next I will introduce several instances of modifying data tables using alter.
Use the alter command in mysql to edit the table structure.
Modify Table Name
The Code is as follows: |
Copy code |
Alter table test rename test1; |
Modify Field Type
The Code is as follows: |
Copy code |
Alter table employee change depno int (5) not null; |
Add Index
The Code is as follows: |
Copy code |
Alter table name add index name (field name 1 [, field name 2…]); Example:
Alter table employee add index emp_name (name ); |
Index with primary keywords
The Code is as follows: |
Copy code |
Alter table name add primary key (field name ); Example:
Alter table employee add primary key (id ); |
Add an index with unique conditions
The Code is as follows: |
Copy code |
Alter table name add unique index name (field name ); Example:
Alter table employee add unique emp_name2 (cardnumber ); |
View the index of a table
The Code is as follows: |
Copy code |
Show index from table name; Example:
Show index from employee; |
Delete An index
The Code is as follows: |
Copy code |
Alter table Name drop index name; Example:
Alter table employee drop index emp_name; |
Add Table fields:
The Code is as follows: |
Copy code |
Alter table table_name ADD field_name field_type; View table: SELECT * FROM table_name; |
Modify the original field name and type:
The Code is as follows: |
Copy code |
Alter table table_name CHANGE old_field_name new_field_name field_type; |
Delete field:
1: Delete Columns
The Code is as follows: |
Copy code |
Alter table [TABLE name] DROP [column name] Alter table table_name DROP field_name;
|