Page 396 - Beginning PHP 5.3
P. 396
Part III: Using PHP in Practice
Deleting Data from a Table
Deleting works in a similar way to updating. To delete rows, you use the DELETE statement. If you add a
WHERE clause, you can choose which row or rows to delete; otherwise all the data in the table are deleted
(though the table itself remains). Here ’ s an example:
mysql > DELETE FROM fruit WHERE id = 2;
Query OK, 1 row affected (0.02 sec)
mysql > SELECT * from fruit;
+----+--------+--------+
| id | name | color |
+----+--------+--------+
| 1 | banana | yellow |
| 3 | plum | purple |
+----+--------+--------+
2 rows in set (0.00 sec)
Deleting Tables and Databases
To delete a table entirely, use the DROP TABLE statement. Similarly, you can delete an entire database
with DROP DATABASE .
First, here ’ s how to use DROP TABLE :
mysql > SHOW TABLES;
+----------------------+
| Tables_in_mydatabase |
+----------------------+
| fruit |
+----------------------+
1 row in set (0.00 sec)
mysql > DROP TABLE fruit;
Query OK, 0 rows affected (0.25 sec)
mysql > SHOW TABLES;
Empty set (0.00 sec)
DROP DATABASE works in a similar fashion:
mysql > SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| information_schema |
| mydatabase |
| mysql |
+--------------------+
3 rows in set (0.40 sec)
mysql > DROP DATABASE mydatabase;
Query OK, 0 rows affected (0.14 sec)
358
9/21/09 9:11:13 AM
c12.indd 358 9/21/09 9:11:13 AM
c12.indd 358