PHP MySQL Interactive Website Design
Altering Tables
Existing tables can be altered using the mysql_query() function.
Add a Column
To add a column to an existing table the syntax would be:
mysql_query("ALTER TABLE birthdays ADD age CHAR(3)");
This simple bit of code would add a new column to the birthdays table named age. Type and size are also set in the statement.
Modify a Column
Column definitions can be modified using the ALTER method. The following code would change the existing birthday column from 7 to 15 characters.
mysql_query("ALTER TABLE birthdays CHANGE birthday birthday VARCHAR(15)");
In the example the column to alter is first named and then the new definition is supplied which includes the column name.
The syntax is the same used for creating the field or column.
Remove a Column
Columns can be removed from an existing table. The next example of code would remove the birthday column.
mysql_query("ALTER TABLE birthdays DROP birthday");
Remove a Table
Be careful with this code. It will remove an entire table and all of its contents from your database.
mysql_query("DROP TABLE table_name");
Download the Scripts
The Birthdays Database management files can be downloaded in a zip file. The package contains an integrated db management system, with a simple login file and interface. To run the scripts on your PC you must have a localhost server installed along with PHP and the MySQL server.
Download birthdays_db.zip
MySQL Tutorial
To extend your knowledge of MySQL study the Docs and Tutorials at the official MySQL website. MYSQL.com
|