MySQL Alter explanation

Source: Internet
Author: User

We need to use the MySQL alter command when we need to modify the data table names or modify the field of the data tables.

Before starting this tutorial, let's create a table with the table named: Testalter_tbl.

[Email protected]# mysql-u root-p password; Enter password:*******mysql> use tutorials;database changedmysql> CREATE TABLE testalter_tbl    - > i INT,    C CHAR (1)    ); Query OK, 0 rows affected (0.05 sec) mysql> SHOW COLUMNS from testalter_tbl;+-------+---------+------+-----+---------+ -------+| Field | Type    | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| I     | int (11) | YES  |     | NULL    |       | | c     | char (1) | YES  |     | NULL    |       | +-------+---------+------+-----+---------+-------+2 rows in Set (0.00 sec)
Delete, add, or modify a table field

The following command uses the ALTER command and the drop clause to delete the I field of the created table above:

mysql> ALTER TABLE testalter_tbl  DROP i;

You cannot use drop to delete a field if only one field is left in the datasheet.

MySQL uses an ADD clause to add columns to the data table, and the following example adds an I field to the table testalter_tbl and defines the data type:

mysql> ALTER TABLE testalter_tbl ADD i INT;

After executing the above command, the I field is automatically added to the end of the data table fields.

Mysql> SHOW COLUMNS from testalter_tbl;+-------+---------+------+-----+---------+-------+| Field | Type    | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| C     | char (1) | YES  |     | NULL    |       | | i     | int (11) | YES  |     | NULL    |       | +-------+---------+------+-----+---------+-------+2 rows in Set (0.00 sec)

If you need to specify the location of the new field, you can use the keyword first provided by MySQL (set to position one column), after field name (After a field is set).

Try the following ALTER table statement, and after successful execution, use show COLUMNS to view the table structure changes:

ALTER TABLE testalter_tbl DROP i; ALTER TABLE testalter_tbl ADD i INT first; ALTER TABLE testalter_tbl DROP i; ALTER TABLE testalter_tbl ADD i INT after C;

The first and after keywords are used only for the ADD clause, so if you want to reset the location of the data table fields, you need to use drop to delete the field and then use Add to add the field and set the position.

Modify field type and name

If you need to modify the field type and name, you can use the MODIFY or change clause in the ALTER command.

For example, to change the type of field C from char (1) to char (10), you can execute the following command:

mysql> ALTER TABLE testalter_tbl MODIFY C CHAR (10);

The syntax differs greatly by using the change clause. After the Change keyword, follow the name of the field you want to modify, and then specify the type and name of the new field. Try the following example:

mysql> ALTER TABLE TESTALTER_TBL change i j bigint;mysql> ALTER TABLE TESTALTER_TBL Change J J INT;
effects of ALTER TABLE on Null values and default values

When you modify a field, you can specify whether to include only or whether to set the default value.

The following example specifies that the field J is not NULL and the default value is 100.

mysql> ALTER TABLE testalter_tbl-     MODIFY J BIGINT not NULL DEFAULT 100;

If you do not set the default value, MySQL automatically sets the word defaults to be considered NULL.

Modify field default values

You can modify the default value of a field by using ALTER to try the following instance:

Mysql> alter TABLE TESTALTER_TBL alter I SET DEFAULT 1000;mysql> SHOW COLUMNS from testalter_tbl;+-------+---------+ ------+-----+---------+-------+| Field | Type    | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| C     | char (1) | YES  |     | NULL    |       | | i     | int (11) | YES  |     | |       | +-------+---------+------+-----+---------+-------+2 rows in Set (0.00 sec)

You can also use the ALTER command and the drop clause to delete the default value of the field, as in the following example:

Mysql> alter TABLE TESTALTER_TBL alter I DROP default;mysql> SHOW COLUMNS from testalter_tbl;+-------+---------+--- ---+-----+---------+-------+| Field | Type    | Null | Key | Default | Extra |+-------+---------+------+-----+---------+-------+| C     | char (1) | YES  |     | NULL    |       | | i     | int (11) | YES  |     | NULL    |       | +-------+---------+------+-----+---------+-------+2 rows in Set (0.00 sec) Changing a Table Type:

To modify a data table type, you can do so by using the ALTER command and the type clause. Try the following example, and we will modify the type of table Testalter_tbl to MYISAM:

Note: You can use the Show Table STATUS statement to view the data table type.

Mysql> ALTER TABLETestalter_tbl TYPE=Myisam;mysql>SHOWTABLESTATUS like 'Testalter_tbl'\g*************************** 1. Row****************name:testalter_tbl type:myisam row_format:fixed Rows:0avg_row_length:0data_length:0max_data_length:25769803775index_length:1024x768Data_free:0auto_increment:NULLCreate_time: -- .-Geneva  ,:Geneva: $Update_time: -- .-Geneva  ,:Geneva: $Check_time:NULLcreate_options:comment:1Rowinch Set(0.00Sec
Modify Table name

If you need to modify the name of the data table, you can do so by using the RENAME clause in the ALTER table statement.

Try the following instance to rename the data table Testalter_tbl to ALTER_TBL:

ALTER TABLE testalter_tbl RENAME TO alter_tbl;

The ALTER command can also be used to create and delete the index of a MySQL data table, which we'll cover in the next section.

Original address: http://www.manongjc.com/mysql/mysql_alter.html

Related reading:

    • MySQL Index
    • MySQL Temp Table

MySQL Alter explanation

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.