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

How to add fields in a table in a database

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

I would like to share with you how to add fields in the table in the database. I believe most people don't know much about it, so share this article for your reference. I hope you can learn a lot after reading this article. Let's learn about it together.

Method: 1, use "ALTER TABLE table name ADD new field name data type;" statement to add fields at the end; 2, use "ALTER TABLE table name ADD new field name data type FIRST;" statement to add fields at the beginning.

The method of adding fields in table in Mysql database

1. Add a field at the end

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

[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).

2. Add a field at the beginning

By default, MySQL adds a new field at the end of the table. If you want to add a new field at the beginning (before the first column), you can use the FIRST keyword with the syntax format as follows:

ALTER TABLE ADD [constraint] FIRST

The FIRST keyword is usually placed at the end of the statement.

3. Add a field in the middle

MySQL not only allows fields to be added at the beginning and end of the table, but also allows fields to be added in the middle (after the specified field). In this case, the AFTER keyword is required, and 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.

(recommended tutorial: mysql video tutorial)

Example:

Use DESC to view the structure of student tables

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)

1. Add a field of type INT age at the end

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)

2. Add the field stuId of type INT to the first column of the table

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

3. Add a field named stuno and data type INT after the name field

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) above are all the contents of the article "how the database adds fields in the table" Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

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