How to add a field to a table in mysql database
This article is about how a mysql database can add a field to a table. The editor thought it was very practical, so I shared it with you as a reference. Let's follow the editor and have a look.
Mysql database method of adding a field to a table: you can use the "ALTER TABLE" statement to add a field, syntax format "ALTER TABLE table name ADD new field name data type [constraint];", the default is to add a new field at the end of the table.
Add a new field to the data table in mysql
This can be done using the "ALTER TABLE" statement, where a complete field includes the field name, data type, and constraints. The syntax format of the MySQL add field 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 student table structure, the SQL statement and the run result are as follows:
Mysql > DESC student +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | id | int (4) | YES | | NULL | name | varchar (20) | YES | | NULL | | sex | char (1) | YES | | NULL | | +-+-+ 3 rows in set (0.01sec)
Use the ALTER TABLE statement to add a field of type INT age, and then use DESC to view the student table structure to verify that the age field has been added successfully.
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 | +-+- -+ | 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)
As you can see from the running results, the age field has been added to the student table, and the field is in the last position of the table, and the field is added successfully.
Thank you for reading! About mysql database how to add a field to the table to share here, I hope the above content can be of some help to you, so that you can learn more knowledge. If you think the article is good, you can share it and let more people see it.