Home  >  Article  >  Database  >  What is the statement to add a field in mysql?

What is the statement to add a field in mysql?

WBOY
WBOYOriginal
2022-01-05 10:55:3323770browse

In mysql, the statement to add a field is "ALTER TABLE table name ADD new field name data type constraints". The ALTER command is used to modify the data table name or modify the data table fields. By default, the fields will be added at the end of the data.

What is the statement to add a field in mysql?

The operating environment of this tutorial: windows10 system, mysql8.0.22 version, Dell G3 computer.

What is the statement for adding fields in mysql

MySQL data table is composed of rows and columns. The "columns" of the table are usually called is a field (Field), and the "row" of the table is called a record (Record). As your business changes, you may need to add new fields to existing tables.

MySQL allows adding fields at the beginning, middle and end.

A complete field includes field name, data type and constraints. The syntax format for adding fields in MySQL is as follows:

ALTER TABLE <表名> ADD <新字段名><数据类型>[约束条件];

The syntax format is explained as follows:                                                        

  • 249823cbf09623dd508dcdf0f05edb2a is the name of the data table;

  • 54a54d14e22f2e75e56c180fa5bad895 is the name of the field to be added;

  • 4cd7266a2a3b9d11d200585237d5f70a is the field that can store data Data type;

  • [Constraints] is optional and is 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.

Note: In this section we only add new fields and do not pay attention to its constraints.

Example 1

Create a new student data table in the test database. The SQL statements and running results are as follows:

mysql> USE test;
Database changed
mysql> CREATE TABLE student (
    -> id INT(4),
    -> name VARCHAR(20),
    -> sex CHAR(1));
Query OK, 0 rows affected (0.09 sec)

Use DESC to view the student table structure. The SQL statements 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)

Use the ALTER TABLE statement to add an INT type field age. The SQL statement and running results are as follows:

mysql> ALTER TABLE student ADD age INT(4);
Query OK, 0 rows affected (0.16 sec)
Records: 0  Duplicates: 0  Warnings: 0

Use DESC to view the student table structure and verify whether the age field is added successfully. 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    |       |
| 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 at the last position of the table. The field was added successfully.

Recommended learning: mysql video tutorial

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

Statement:
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