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

What is the statement to add a column in mysql

青灯夜游
青灯夜游Original
2022-04-14 15:43:1820297browse

Mysql statement to add a column is "ALTER TABLE", and the syntax is "ALTER TABLE table name ADD new column name data type [constraints] [FIRST];". The FIRST keyword is optional. If omitted, the column is added at the end of the table; if not omitted, the column is added at the beginning of the table.

What is the statement to add a column in mysql

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

MySQL data tables are composed of rows and columns. Usually the "columns" of the table are called fields (Field), and the "rows" of the table are called records (Record). As your business changes, you may need to add new fields (columns) to existing tables.

In mysql, the statement to add a column is "ALTER TABLE"; this statement can change the structure of the original table, such as adding or deleting columns, changing original columns Type, rename columns or tables, etc.

Add the syntax format of the column:

ALTER TABLE 表名 ADD 新列名 数据类型 [约束条件] [FIRST] ;

The description of the syntax format is as follows:

  • ##Table name is the data table Name;

  • New column name is the name of the column (field) to be added;

  • data Type is the data type that can store data in the field to be added;

  • [Constraints] is optional and is used to perform operations on the added field. constraint.

  • [FIRST] is optional and is used to add columns at the beginning of the table; if omitted, the column will be added at the end of the table by default,

Example: We have a student data table, its table structure is like this

What is the statement to add a column in mysql

Use the ALTER TABLE statement Add an INT type field age at the end

ALTER TABLE student ADD age INT(4);

View a table structure

What is the statement to add a column in mysql

You can see that the age field has been added to the student table, and this field At the last position of the table, the field is added successfully.

Now, try to use the FIRST keyword to add the INT type field stuId in the first column of the table

ALTER TABLE student ADD stuId INT(4) FIRST;

View a table structure

What is the statement to add a column in mysql

You can see that the stuId field has been added to the student table, and the field is at the first position in the table. The field was added successfully.

[Related recommendations:

mysql video tutorial]

The above is the detailed content of What is the statement to add a column 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