How to Rename a Column of a MySQL Table Using PHP



PHP







In this article, we show how to rename a column of a MySQL table using PHP.

We rename a column using PHP to write the drop table MySQL query to delete the table.

The general format of the MySQL query to rename a column of a MySQL table is shown below.





So the above is the MySQL code to rename a column originall named emailadress to email.

Even though we mean to simply rename the column and not change the structure of the column, nevertheless, we must specify all of the attributes of the renamed column including type and whether it is not null, unique, etc.

We'll now show how to tie this in with PHP to rename a column of a table.



So, first, we must connect to the database.

After we do this, we create a variable named $renamecolumn. This $renamecolumn variable is set equal to a mysql_query() function that has the parameter "ALTER TABLE customers CHANGE emailaddress email VARCHAR(50) NOT NULL UNIQUE". This renames the column, emailaddress, to email in the customers table.

Again, even though we are simply changing the name, we must specify all parameters of the renamed name of the column, including the type, length, NOT NULL, UNIQUE, PRIMARY, etc.

This renamed column is of type VARCHAR(50), is NOT NULL, and is UNIQUE. UNIQUE means that the email must be unique. In other words, no 2 emails entered into this column can be the same exact emaail.

So this is how you can rename a column in a MySQL table using PHP.


Related Resources

How to Add a Column to a MySQL Table Using PHP

How to Add a Primary Key to a Column of a MySQL Table Using PHP

How to Make a Column of a MySQL Table Unique Using PHP

How to Rename a Column of a MySQL Table Using PHP

How to Delete a Column of a MySQL Table Using PHP

How to Set the Default Value of a Column of a MySQL Table Using PHP

How to Drop the Default Value of a Column Of a MySQL Table Using PHP

How to Modify the Defintion or Data Type of a Column of a MySQL Table Using PHP

HTML Comment Box is loading comments...