Learning Mysql


-> SET album_name = UPPER(album_name)



Download 4.24 Mb.
View original pdf
Page267/366
Date04.08.2023
Size4.24 Mb.
#61806
1   ...   263   264   265   266   267   268   269   270   ...   366
Learning MySQL
-> SET album_name = UPPER(album_name)
-> WHERE artist.artist_id = album.artist_id AND
-> album.artist_id = track.artist_id AND
-> album.album_id = track.album_id AND
-> track.artist_id = played.artist_id AND
-> track.album_id = played.album_id AND
-> track.track_id = played.track_id;
Query OK, 2 rows affected (0.00 sec)
Rows matched 11 Changed 2 Warnings The method that you choose to use is just personal preference, and that might be driven by the amount of typing you’re prepared to do!
As with multiple-table deletes, there are some limitations on updates:
Updates and Deletes with Multiple Tables | 291


• You can’t use
ORDER
BY
• You can’t use LIMIT You can’t update a table that’s read from in a nested subquery.
Other than that, multiple-table updates are much the same as single-table ones.
Replacing Data
You’ll sometimes want to overwrite data. You can do this in two ways using the techniques we’ve shown previously Delete an existing row using its primary key and then insert anew replacement with the same primary key Update a row using its primary key, replacing some or all of the values (except the primary key).
The
REPLACE
statement gives you a third, convenient way to change data. This section explains how it works.
The
REPLACE
statement is just like INSERT, but with one difference. You can’t
INSERT
a new row if there is an existing row in the table with the same primary key, You can get around this problem with a
REPLACE
query, which first removes any existing row with the same primary key and then inserts the new one.
Let’s try an example, where we’ll replace the row for "Nick Cave & The Bad Seeds ":
mysql> REPLACE artist VALUES (2, "Nick Cave and The Bad Seeds");
Query OK, 2 rows affected (0.02 sec)
You can see that MySQL reports that two rows were affected first, the old row was deleted, and, second, the new row was inserted. You can see that the change we made was minor—we just changed the & to an “and”—and therefore, it could easily have been accomplished with an UPDATE. Because the tables in the music database contain few columns, it’s difficult to illustrate an example in which
REPLACE
looks simpler than
UPDATE
You can use the different
INSERT
syntaxes with REPLACE, including using
SELECT
queries.
Here are some examples:
mysql> REPLACE INTO artist VALUES (2, "Nick Cave and The Bad Seeds");
Query OK, 2 rows affected (0.00 sec)
mysql> REPLACE INTO artist (artist_id, artist_name)

Download 4.24 Mb.

Share with your friends:
1   ...   263   264   265   266   267   268   269   270   ...   366




The database is protected by copyright ©ininet.org 2024
send message

    Main page