Database Reference
In-Depth Information
9
Deleting our Data
Keeping Data Accurate
After spending so much time designing our databases and putting data into them, it seems
a pity to discuss deleting them, but deleting records and tables is still an important part of
using MySQL. If we are trying to keep our data valid and accurate we will have to remove
data from time to time. This chapter will show you how to remove rows from tables, and
then how to remove whole tables.
You may remember back in Chapter 5 we created the webpage 2 table by inserting data
from the webpage table. If for some reason you no longer have the webpage 2 table in your
database, you can create it again quickly by running the following:
CREATE TABLE webpage2 (ID MEDIUMINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
Content text, Title text)
Now copy the data into this table by executing the following:
INSERT INTO webpage2
SELECT * from webpage
Select everything from webpage 2 and you will see the sacrificial table in Figure 9.1.
DELETE FROM
To DELETE rows from a table, you use the following query:
DELETE FROM
tablename
WHERE
condition
As we demonstrated earlier, this command will quickly and unsympathetically remove
everything from a table without a WHERE clause, so be careful when you are using it. For
instance, if we wanted to delete the visitorbook row from our webpage 2 table we would use
the following:
103
Search WWH ::




Custom Search