Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

The method of adding int Field in mysql

2025-04-10 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

This article will explain in detail the method of adding int fields to mysql. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.

Mysql's way to add int fields: use the ALTER TABLE statement to add them in the format "ALTER TABLE table name ADD new field int;" or "ALTER TABLE table name ADD new field int (length) AFTER existing field;"

A complete field includes the field name, data type, and constraints. The syntax format of the MySQL add field is as follows:

ALTER TABLE ADD [constraint]

The syntax format is described as follows:

Is the name of the data table

The name of the field to be added

The data type that can store data for the field to be added can be int,text

[constraints] are optional and are used to constrain added fields.

By default, this syntax format adds a new field at the end of the table (after the last column).

Example

Create a new student data table in the test database, and the SQL statement and run result are as follows:

Mysql > USE test;Database changedmysql > 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 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,SQL statement and run the result 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 structure of the student table and verify that the age field is added successfully. 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 | | 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.

Sometimes you need to add a field in the middle, so if you add a field in the middle?

At this point, you need to use the AFTER keyword. The syntax format is as follows:

ALTER TABLE ADD [constraint] 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.

Example

Use the ALTER TABLE statement to add a field named stuno and data type INT to the student table, with the stuno field after the name field. The SQL statement and the run result are as follows:

Mysql > ALTER TABLE student ADD stuno INT (11) AFTER name;Query OK, 0 rows affected (0.13 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql > DESC student +-+ | Field | Type | Null | Key | Default | Extra | +-+- -+ | stuId | int (4) | YES | | NULL | | id | int (4) | YES | | NULL | | name | varchar (20) | YES | | NULL | | stuno | int (11) | YES | | NULL | | sex | char (1) | YES | | NULL | | age | int (4) | YES | | NULL | | +-+-+ 6 rows in set (0.00 sec)

As you can see from the running results, the stuId field has been added to the student table, and the field is in the position after the name field. The field was added successfully.

So much for sharing the method of adding int fields to mysql. I hope the above content can be of some help to you and can learn more knowledge. If you think the article is good, you can share it for more people to see.

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.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report