In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
Editor to share with you mysql how to add new columns in the table, I believe that most people do not understand, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!
In mysql, you can use the "ALTER TABLE" statement to add new columns to the table. The syntax format is "ALTER TABLE table name ADD new field name data type [constraint];"; the default is to add a new column at the end of the table, or you can add a new column at the beginning if you use the FIRST keyword.
Mysql method to add new columns in the table:
1. Add a new column (field) at the end
By default, MySQL adds a new field at the end of the table.
A complete field includes the field name, data type, and constraints.
The syntax format for MySQL to add fields at the end is as follows:
ALTER TABLE table name ADD new field name data type [constraint]
The syntax format is described as follows:
Table name: the name of the data table
New field name: the name of the field to be added
Data type: the data type that can store data for the field to be added
[constraint]: is optional and is used to constrain the added fields.
By default, this syntax format adds a new field at the end of the table (after the last column).
Example:
Use DESC to view the structure of an student table
Mysql > DESC student +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | name | varchar (20) | YES | | NULL | | sex | char (1) | YES | | NULL | | +-+-+ 2 rows in set (0.01 sec)
Add a field of type INT age at the end of the table
Mysql > ALTER TABLE student ADD age INT (4); Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0mysql > DESC student +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | name | varchar (20) | YES | | NULL | sex | char (1) | YES | | NULL | | age | int (4) | YES | | NULL | | +-+-+ 3 rows in set (0.00 sec)
Add a new column (field) at the beginning
If you want to add a new field at the beginning (before the first column), you can use the FIRST keyword, which has the following syntax format:
ALTER TABLE table name ADD new field name data type [constraint] FIRST
Note: the FIRST keyword is usually placed at the end of the statement.
Example:
Add a field of type INT id to the first column of the table
Mysql > ALTER TABLE student ADD id INT (4) FIRST;Query OK, 0 rows affected (0.14 sec) Records: 0 Duplicates: 0 Warnings: 0mysql > DESC student +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | id | int (4) | YES | | NULL | | name | varchar (20) | YES | | NULL | | sex | char (1) | YES | | NULL | | age | int (4) | YES | | NULL | | +-- -+ 4 rows in set (0.00 sec)
Add a new column (field) in the middle
MySQL not only allows fields to be added at the beginning and end of the table, but also allows fields to be added in the middle (after the specified field). In this case, the AFTER keyword is required, and the syntax format is as follows:
ALTER TABLE table name ADD new field name data type [constraint] Field name that already exists in AFTER
The role of AFTER is to add a new field to the end of an existing field.
Note that you can only add a new field after an existing field, not before it.
This is how mysql adds all the new columns in the table. Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
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.