Adding new fields in MySQL
To add a new field in MySQL, you can use the ALTER TABLE statement.
Here is the basic syntax for adding new fields:
ALTER TABLE table_name ADD column_name datatype;
in:
-
table_name
is the name of the table in which you want to add a new field. -
column_name
is the name of the new field. -
datatype
is the data type of the new field. For example,VARCHAR
、INT
orDOUBLE
wait.
Here is an example ALTER TABLE statement that demonstrates how to nameuser
Add a table namedname
The new field of the data type is VARCHAR(10):
ALTER TABLE users ADD phone_number VARCHAR(10);
Note that if you have to place a new column in a specific position in the table, you can use the following syntax:
ALTER TABLE table_name ADD column_name datatype AFTER another_column;
inanother_column
is the name of the column that already exists, and the new column will be placed after the column.
For example, the following command will give the nameuser
Add a table namedname
new column with data typeVARCHAR(10)
and place it inid
After column:
ALTER TABLE user ADD name VARCHAR(10) AFTER id;
Add a new field at the last position of the table (default)
grammar:
ALTER TABLE Table name ADD New field name Data Type Constraints ;
Add new fields at the beginning of the table
grammar:
ALTER TABLE Table name ADD New field name Data Type Constraints FIRST ;
Add a new field after a field
grammar:
ALTER TABLE Table name ADD New field name Data Type Constraints AFTER Already field names ;
MySQL table add fields sql statement
Add fields to tables in MySQL database using ALTER TABLE
Syntax Rules
ALTER TABLE table_name ADD COLUMN column_name VARCHAR(100) DEFAULT NULL COMMENT 'Add a new field' AFTER old_column;
Statement content
-
table_name
: Table name; -
column_name
: The field name that needs to be added; -
VARCHAR(100)
: The field type is varchar, length 100; -
DEFAULT NULL
: Default value NULL; -
AFTER old_column
: Add new fields after the old_column field, and if you don't write them, the default end of the table will be the default;
example:
ALTER TABLE xyd_case_info ADD COLUMN `deleted` TINYINT(1) NULL DEFAULT NULL COMMENT '0, normal, 1 delete'; ALTER TABLE xyd_case_info ADD COLUMN `create_by` VARCHAR(50) NULL DEFAULT NULL COMMENT 'Create person' COLLATE 'utf8mb4_general_ci'; ALTER TABLE xyd_case_info ADD COLUMN `create_time` DATETIME NULL DEFAULT NULL COMMENT 'Create time'; ALTER TABLE xyd_case_info ADD COLUMN `update_by` VARCHAR(50) NULL DEFAULT NULL COMMENT 'Update person' COLLATE 'utf8mb4_general_ci'; ALTER TABLE xyd_case_info ADD COLUMN `update_time` DATETIME NULL DEFAULT NULL COMMENT 'Update time';
Summarize
The above is personal experience. I hope you can give you a reference and I hope you can support me more.