📜  MySQLi-ALTER命令(1)

📅  最后修改于: 2023-12-03 15:17:48.894000             🧑  作者: Mango

MySQLi ALTER Command

MySQLi is the MySQL Improved Extension for PHP. It is a better and more robust version of the traditional MySQL extension. MySQLi provides an object-oriented interface to work with MySQL databases, and it also supports prepared statements, transactions, and many more features.

In this article, we are going to talk about the ALTER command in MySQLi. The ALTER command is used to modify the structure of a MySQL table. It allows you to add, modify, or delete columns in a table, change the data type of a column, rename a table or column, and add or remove indexes, among other things.

Syntax

The syntax of the ALTER command in MySQLi is as follows:

ALTER TABLE table_name
ADD column_name data_type [NOT NULL] [DEFAULT value],
MODIFY column_name data_type [NOT NULL] [DEFAULT value],
DROP COLUMN column_name,
CHANGE COLUMN old_column_name new_column_name data_type [NOT NULL] [DEFAULT value],
RENAME new_table_name;

Let's take a closer look at each of these clauses.

ADD

The ADD clause is used to add a new column to a table. Here's an example:

$sql = "ALTER TABLE products
        ADD product_price decimal(10,2) NOT NULL DEFAULT 0.0";

This will add a new column named product_price to the products table, with a data type of decimal(10,2). The column will not allow NULL values, and its default value will be set to 0.0.

MODIFY

The MODIFY clause is used to modify the data type or other attributes of an existing column. Here's an example:

$sql = "ALTER TABLE products
        MODIFY product_price int(11) DEFAULT 0";

This will change the data type of the column product_price in the products table to int(11). The column will allow NULL values, and its default value will be set to 0.

DROP

The DROP clause is used to delete an existing column from a table. Here's an example:

$sql = "ALTER TABLE products
        DROP COLUMN product_price";

This will delete the column product_price from the products table.

CHANGE COLUMN

The CHANGE COLUMN clause is used to rename an existing column or change its data type. Here's an example:

$sql = "ALTER TABLE products
        CHANGE COLUMN product_price price decimal(10,2) NOT NULL DEFAULT 0.0";

This will change the name of the column product_price in the products table to price, and also change its data type to decimal(10,2). The column will not allow NULL values, and its default value will be set to 0.0.

RENAME

The RENAME clause is used to rename an existing table. Here's an example:

$sql = "ALTER TABLE products
        RENAME new_products";

This will rename the products table to new_products.

Conclusion

The ALTER command in MySQLi is a powerful tool for modifying the structure of MySQL tables. It allows you to add, modify, or delete columns in a table, change the data type of a column, rename a table or column, and add or remove indexes, among other things. Understanding how to use the ALTER command is essential for any MySQL developer.