In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)05/31 Report--
This article will explain in detail the role of the ALTER command in MySQL, and the content of the article is of high quality, so the editor will share it with you for reference. I hope you will have some understanding of the relevant knowledge after reading this article.
1. First create a table named testalter_tbl.
2. Delete, add or modify table fields
The following command uses the ALTER command and the DROP clause to delete the I field of the table created above:
3. If there is only one field left in the data table, you cannot use DROP to delete the field.
The ADD clause is used in MySQL to add columns to the data table. The following example adds the I field to the table testalter_tbl and defines the data type:
4. After executing the above command, the I field is automatically added to the end of the Datasheet field.
5. If you need to specify the location of the new field, you can use the keyword FIRST (set to locate the first column) and the AFTER field name (set after a word) provided by MySQL.
Try the following ALTER TABLE statement, and after successful execution, use SHOW COLUMNS to see the changes in the table structure:
6. The FIRST and AFTER keywords are only used in the ADD clause, so if you want to reset the position of the datasheet field, you need to use DROP to delete the field and then use ADD to add the field and set the position. The FIRST and AFTER keywords are used only in the ADD clause, so if you want to reset the position of the datasheet field, you need to use DROP to delete the field and then use ADD to add the field and set the location.
7. Modify the field type and name
If you need to change 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:
8. Using the CHANGE clause, the syntax is very different. The CHANGE keyword is followed by the name of the field you want to modify, and then specify the new field name and type. Try the following example:
9. The influence of ALTER TABLE on Null value and default value
When you modify a field, you can specify whether to include only or set the default value.
The following example specifies that field j is NOT NULL and the default value is 100.
10. If you do not set the default value, MySQL will automatically set the field to NULL by default.
Modify field default values
You can use ALTER to modify the default values of fields. Try the following example:
11. You can also use the ALTER command and the DROP clause to delete the default value of the field, as shown in the following example:
12. Modify the data table type by using the ALTER command and the TYPE clause. Try the following example, and we change the type of table testalter_tbl to MYISAM:
Note: you can use the SHOW TABLE STATUS statement to view the data table type.
13. Modify the table name
If you need to change the name of the data table, you can use the RENAME clause in the ALTER TABLE statement to do so.
Try the following example to rename the datasheet testalter_tbl to alter_tbl:
Note:
Modify storage engine: modify to myisam
Alter table tableName engine=myisam
Delete foreign key constraint: keyName is a foreign key alias
Alter table tableName drop foreign key keyName
Modify the relative position of the field: here name1 is the field you want to modify, type1 is the original type of the field, and the choice between first and first should be obvious. First is placed first, and after is placed after the name2 field.
Alter table tableName modify name1 type1 first | after name2; has shared so much about the role of the ALTER command in MySQL. I hope the above content can be helpful to you and learn more. If you think the article is good, you can share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.