Use of 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 a field type
The code is as follows |
Copy Code |
ALTER TABLE employee change depno depno Int (5) is not null; |
Add index
The code is as follows |
Copy Code |
ALTER TABLE name add index index name (field name 1[, field Name 2 ...]); Example:
ALTER TABLE employee ADD index emp_name (name); |
Index of the Primary keyword
The code is as follows |
Copy Code |
ALTER TABLE name Add primary key (field name); Example:
ALTER TABLE employee ADD primary key (ID); |
Index with unique restriction criteria
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 the index from table name; Example:
Show index from employee; |
Delete an index
The code is as follows |
Copy Code |
ALTER TABLE name DROP INDEX index name; Example:
ALTER TABLE employee DROP INDEX emp_name; |
To add a table field:
The code is as follows |
Copy Code |
ALTER TABLE table_name ADD field_name Field_type; View table: SELECT * FROM table_name; |
To 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; |
To delete a field:
1: Delete Column
code is as follows |
copy code |
ALTER TABLE Table name drop Column name ALTER TABLE table_name drop field_name |