MySQL database alter command explain _MYSQL

Source: Internet
Author: User

MySQL for relational databases (relational database Management System), this so-called "relational" can be understood as the concept of "table", a relational database consists of one or several tables.

We need to use the MySQL alter command when we need to modify the datasheet name or modify the Data table field.

Before starting this chapter tutorial, let's first create a table named: Testalter_tbl.

root@host# mysql-u root-p password;
Enter password:*******
mysql> use tutorials;
Database changed
mysql> create TABLE Testalter_tbl
-> (
-> i INT,
-> C CHAR (1)
->); C9/>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 table created 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.

In MySQL, use the ADD clause to add columns to the datasheet, the following instance adds the I field in the table Testalter_tbl, and defines the data type:

mysql> ALTER TABLE testalter_tbl ADD i INT;

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

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 provided by MySQL (set to the first column), after the field name (set in a field).

Try the following ALTER table statement to view changes in the table structure using show COLUMNS after successful execution:

ALTER TABLE testalter_tbl DROP i;
ALTER TABLE testalter_tbl ADD i INT i;
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 position of the data table field, you need to use drop to remove the fields and then use Add to add the fields and set the location.

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);

With the change clause, the syntax is a lot different. 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;

Effect of ALTER TABLE on Null values and default values

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

The following instance 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 will automatically set the word defaults to assume NULL.

Modify the default value of a field

You can use ALTER to modify the default value of the field, and try the following examples:

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 | | 1000 | |
+-------+---------+------+-----+---------+-------+
2 rows in Set (0.00 sec)

You can also use the ALTER command and the drop clause to remove the default value for a field, as follows:

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:

Modifying a datasheet type can be done using the ALTER command and the type clause. Try the following instance, we modify the type of table Testalter_tbl to MYISAM:

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

mysql> ALTER TABLE testalter_tbl TYPE = MYISAM;
Mysql> show TABLE STATUS like ' testalter_tbl ' \g
*************************** 1. Row ****************
Name: Testalter_tbl
type:myisam
row_format:fixed
rows:0
avg_row_length:0
data_length:0 data_length:25769803775
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
:
1 row in Set (0.00 sec)

Modify Table Name

If you need to modify the name of the datasheet, you can use the RENAME clause in the ALTER table statement to implement it.

Try the following instance to rename the datasheet testalter_tbl to ALTER_TBL:

mysql> ALTER TABLE testalter_tbl RENAME to Alter_tbl;

The ALTER command can also be used to create and delete the index of the MySQL data table, which we will introduce in the following sections.

The above is a small set to introduce the MySQL database Alter command to explain the relevant knowledge, hope to help everyone, if you want to learn more information, please pay attention to cloud Habitat community website!

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.