SQL ALTER TABLE statement is very important in PHP operation, this article will explain it.
SQL ALTER TABLE syntax
To add columns to the table, use the following syntax:
ALTER TABLE table_name
ADD column_name datatype
To delete a column from a table, use the following syntax:
ALTER TABLE table_name
DROP COLUMN column_name
Comments: Some database systems do not allow this method of deleting columns in database tables (DROP COLUMN column_name).
To change the datatype of a column in a table, use the following syntax:
ALTER TABLE table_name
ALTER COLUMN column_name datatype
SQL ALTER TABLE Example
Now, we want to add a new column named "Birthday" to the table "Persons".
We use the following SQL statement:
ALTER TABLE Persons
ADD Birthday date
Please note that the new column "Birthday" The type is date, which can store dates. The data type specifies the type of data that can be stored in the column.
Change Data Type Example
Now we want to change the data type of the "Birthday" column in the "Persons" table.
We use the following SQL statement:
ALTER TABLE Persons
ALTER COLUMN Birthday year
Please note that the data type of the "Birthday" column is year, Can store year in 2-digit or 4-digit format.
DROP COLUMN Example
Next, we delete the "Birthday" column in the "Person" table:
ALTER TABLE Person
DROP COLUMN Birthday
This article provides relevant explanations on SQL ALTER TABLE. For more learning materials, please pay attention to the php Chinese website to view.
Related recommendations:
Related knowledge about SQL undoing indexes, tables and databases
Relevant knowledge about SQL DEFAULT constraints Knowledge
Related knowledge about SQL CHECK constraints
The above is the detailed content of Related operations of SQL ALTER TABLE statement. For more information, please follow other related articles on the PHP Chinese website!