Wu Yuxiong - natural born MySQL study notes: MySQL ALTER command

When the need to modify the data table or modify data table field, you need to use the MySQL ALTER command. 
@ Host the root # MySQL -u -p the root password; 
the Enter password: ******* 
MySQL > use RUNOOB; 
Database changed 
MySQL > Create Table testalter_tbl
     -> (
     -> I the INT,
     -> C CHAR (. 1 )
     - > ); 
Query the OK, 0 rows affected ( 0.05 sec) 
MySQL > SHOW the COLUMNS the FROM testalter_tbl;
 + ------ + ------- + --------- + ---- - + --------- + ------- + 
| Field, | Type | Null | Key | the Default | Extra | 
+ ------- + -------- - + ------ + ----- + --------- + ------- + 
| i | int (11) | YES | | NULL | |
| c     | char(1) | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
Delete, add or modify a table field 
follows the ALTER command and command fields are created DROP clause i delete the above table: 
MySQL > ALTER TABLE testalter_tbl DROP i; 
if only the remaining data field in the table can not be used to remove DROP field. 

MySQL ADD clause used to add a column to the data table, the following examples are added to the table testalter_tbl i field and defined data types: 
MySQL > the ALTER TABLE ADD i testalter_tbl the INT; 
After the above Run, i field is automatically added to the data table at the end of the field. 
MySQL > SHOW the COLUMNS the FROM testalter_tbl;
 + ------- + --------- + --------- + ------ + ----- + - + ------ 
| Field, | Type | Null | Key | the Default | Extra | 
+ ------- + --------- + ------ + ---- - + --------- + ------- + 
| c | char (1) | YES | | NULL | |  
| I | int (. 11) | YES | | NULL | |
+ ------- + ------- - + ------ + ----- + --------- + ------- +
If you need to specify the location of the new field, you can use the keyword FIRST MySQL provided (set bit in the first column), AFTER field names (after setting a field located). 
Try the following ALTER TABLE statement, after the successful implementation, use SHOW COLUMNS see the change table structure: 
ALTER TABLE testalter_tbl DROP i; 
ALTER TABLE testalter_tbl the ADD i INT FIRST; 
ALTER TABLE testalter_tbl DROP i; 
ALTER TABLE testalter_tbl the ADD i INT an AFTER c; 

FIRST and AFTER keywords can be used for ADD and MODIFY clause, so if you want to reset the data table field position on the need to use DROP then delete the field to add the field and set the position using the ADD.
Modify the field type and name 
if you need to modify the field type and name, you can use MODIFY or CHANGE clauses in the ALTER command. 
For example, a field c of the type from CHAR ( changed. 1) CHAR (10 ), execute the following command: 
MySQL > the ALTER TABLE testalter_tbl the MODIFY c CHAR (10 ); 

the CHANGE clause grammar are very different. After the CHANGE keyword, followed by the field name is to be modified, and then specify a new field name and type. Try following examples: 
MySQL > the ALTER TABLE testalter_tbl ij of the CHANGE BIGINT; 
MySQL > the ALTER TABLE testalter_tbl the CHANGE JJ the INT;
ALTER TABLE impact on the Null value and default value 
when modifying a field, you can specify whether to include the value or whether to set the default value. 
The following examples, j is specified field NOT NULL and the default value is 100. 
MySQL > the ALTER TABLE testalter_tbl 
     -> MODIFY J BIGINT the NOT NULL the DEFAULT 100 ; 
if you do not set a default value, MySQL will automatically set the field defaults to NULL.
Modify the default value field 
can be used to modify the default values ALTER field, try the following examples: 
MySQL > ALTER TABLE I testalter_tbl the SET ALTER the DEFAULT 1000 ; 
MySQL > SHOW the COLUMNS the FROM testalter_tbl;
 + ------- + ----- + ------ + ----- + ---- + --------- + ------- 
| Field, | Type | Null | Key | the Default | Extra | 
+ + --------- + ------ + ------- ----- + --------- + ------- + 
| C | char (. 1) | YES | | NULL | | 
| I | int (. 11) | YES | | 1000 | | 
+ ------- + --------- + ---- - + ----- + --------- + ------- +
ALTER command may be used to remove and DROP clause field's default value, the following examples: 
MySQL > ALTER TABLE I testalter_tbl ALTER DROP the DEFAULT; 
MySQL > SHOW the COLUMNS the FROM testalter_tbl;
 + ------- + ----- + ------ + ----- + ---- + --------- + ------- 
| Field, | Type | Null | Key | the Default | Extra | 
+ + --------- + ------ + ------- ----- + --------- + ------- + 
| C | char (. 1) | YES | | NULL | | 
| I | int (. 11) | YES | | NULL | | 
+ ------- + --------- + ---- - + ----- + --------- + ------- +
Modify the data table type, and can use the ALTER command to finish TYPE clause. Try the following example, the type of table testalter_tbl amended as MYISAM: 
Note: See the data table types can use the SHOW TABLE STATUS statement. 
MySQL > = the ALTER TABLE testalter_tbl ENGINE MYISAM; 
MySQL > TABLE SHOW the STATUS the LIKE ' testalter_tbl ' \ G
 *************************** 1. Row **************** 
           the Name: testalter_tbl 
           Type: MyISAM 
     ROW_FORMAT: Fixed 
           the Rows: 0 
 AVG_ROW_LENGTH: 0 
    Data_length: 0 
Max_data_length: 25,769,803,775 
   Index_length: 1024 
      Data_free: 0 
 AUTO_INCREMENT: NULL 
    create_time:2007-06-03 08:04:36
    Update_time: 2007-06-03 08:04:36
     Check_time: NULL
 Create_options:
        Comment:
Modify the table name 
if you need to modify the name of the data table, you can use the RENAME clause in ALTER TABLE statement to achieve. 
Try the following examples of data tables testalter_tbl rename alter_tbl: 
MySQL > the ALTER TABLE RENAME testalter_tbl the TO alter_tbl; 
the ALTER command can also be used to create and delete MySQL data tables index

 

Guess you like

Origin www.cnblogs.com/tszr/p/12114434.html