To add a column before an existing column in a MySQL table, you can use the ALTER TABLE statement along with the ADD COLUMN clause and the BEFORE keyword. Here's the syntax:
ALTER TABLE table_name
ADD COLUMN new_column_name column_definition
BEFORE existing_column_name;
Example
Suppose you have a table employees with the following structure:
Now, you want to add a column age before the name column.
ALTER TABLE employees
ADD COLUMN age INT
BEFORE name;
Resulting Table
Notes
column_definition includes the data type and any constraints (e.g., NOT NULL, DEFAULT values).
Use AFTER instead of BEFORE to add the column after a specific column.
If you want to add the column at the end of the table, omit the BEFORE or AFTER clause.
No comments:
Post a Comment