How to add a field to a table in mysql database?

青灯夜游
Release: 2020-11-03 10:04:00
Original
39605 people have browsed it

Mysql database method to add a field to a table: You can use the "ALTER TABLE" statement to add a field, the syntax format is "ALTER TABLE table name ADD new field name data type [constraints];", the default is Add a new field at the last position of the table.

How to add a field to a table in mysql database?

(Recommended tutorial:mysql video tutorial)

Add new fields to the data table in mysql

You can use the "ALTER TABLE" statement to achieve this. A complete field includes field name, data type and constraints. The syntax format for adding fields in MySQL is as follows:

ALTER TABLE 表名 ADD 新字段名 数据类型 [约束条件];
Copy after login

The description of the syntax format is as follows:

  • Table name: is the name of the data table;

  • New field name: the name of the field to be added;

  • Data type: the data type of the field to be added that can store data;

  • [Constraints]: It is optional and used to constrain the added fields.

This syntax format adds a new field at the last position of the table (after the last column) by default.

Example:

Use DESC to view the student table structure. The SQL statement and running results 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.01 sec)
Copy after login

Use the ALTER TABLE statement to add an INT type field age, and then use DESC to view the student table structure to verify whether the age field is added successfully.

mysql> ALTER TABLE student ADD age INT(4); Query OK, 0 rows affected (0.16 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> 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)
Copy after login

You can see from the running results that the age field has been added to the student table, and the field is at the last position of the table. The field was added successfully.

For more programming-related knowledge, please visit:Programming Teaching! !

The above is the detailed content of How to add a field to a table in mysql database?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!