Once you've entered your data into a database table, you might like to change it at some point. Whether you want to correct a spelling mistake, or change the date attached to a joke, such alterations are made using the UPDATE command. This command contains elements of the INSERT command (that set column values) and of the SELECT command (that pick out entries to modify). The general form of the UPDATE command is as follows:

mysql> UPDATE table_name SET
    -> col_name = new_value, ...
    -> WHERE conditions;

So, for example, if we wanted to change the date on the joke we entered above, we'd use the following command:

mysql> UPDATE Jokes SET JokeDate="1990-04-01" WHERE ID=1;

Here's where that ID column comes in handy. It allows us to easily single out a joke for changes. The WHERE clause here works just like it does in the SELECT command. This next command, for example, changes the date of all entries that contain the word "chicken":

mysql> UPDATE Jokes SET JokeDate="1990-04-01"
    -> WHERE JokeText LIKE "%chicken%";

Deleting Stored Data

The deletion of entries in SQL is dangerously easy (if you can't tell by now, this is a recurring theme). Here's the command syntax:

mysql> DELETE FROM table_name WHERE conditons;

So to delete all chicken jokes from your table, you'd use the following query:

mysql> DELETE FROM Jokes WHERE JokeText LIKE "%chicken%";

One thing to note is that the WHERE clause is actually optional. However, you should be very careful if you leave it off, as the DELETE command will then apply to all entries in the table. This command will empty the Jokes table in one fell swoop:

mysql> DELETE FROM Jokes;

Scary, huh?


There's a lot more to the MySQL database system and the SQL language than the few basic commands we've looked at here, but these commands are by far the most commonly used. So far we've only worked with a single table. To realize the true power of a relational database, we'll also need to learn how to use multiple tables together to represent potentially complex relationships between database entities.

All this and more will be covered in Chapter 5, where we'll discuss database design principles, and look at some more advanced examples. For now, though, we've hopefully accomplished our objective, and you can comfortably interact with MySQL using the command line interface.