Speaking SQL (part 1) - Old Data For New
(Page 7 of 7 )
And finally, there's an UPDATE command designed to help you change existing values in a table; it looks like this:
UPDATE <table_name> SET <field_name> = <new_value>
The above command would act on all values in the field
<field_name>, changing them all to <new_value>. If you'd like to alter the value in a single field only, you can use the WHERE clause, as with the DELETE command.
Using this information, I could update John Doe's email address in the table:
mysql>
UPDATE members SET email = 'john@somewhere.com' WHERE member_id = 1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1 Changed: 1 Warnings: 0
You can alter multiple fields by separating them with
commas.
mysql>
UPDATE members SET email = 'john@somewhere.com', lname = 'Doe The
First
WHERE member_id = 2;
Query OK, 1 row affected (0.05 sec)
Rows matched: 1 Changed: 1 Warnings: 0
And your table will now look something like this:
+-----------+-------+---------------+---------+--------------------------+
| member_id | fname | lname | tel | email |
+-----------+-------+---------------+---------+--------------------------+
| 1 | John | Doe The First | 1234567 | john@somewhere.com |
| 2 | Jane | Doe | 8373728 | jane@site.com |
| 3 | Steve | Klingon | 7449373 | steve@alien-race.com |
| 4 | Santa | Claus | 9999999 | santa@the-north-pole.com |
+-----------+-------+---------------+---------+--------------------------+
4 rows in set (0.00 sec)
And that's about it for the first part of this article. Next
time, I'll be showing you how to get your data out of the table with a variety of SELECT statements - so make sure you come back for that!
| DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware. |