Database Reference
In-Depth Information
Figure 8.13
Replacing a row with REPLACE.
Figure 8.13 shows the results of running the above query. We now have a row that has all
of the fields populated.
We can also run the REPLACE query with the alternative format:
REPLACE table
SET
column=value,
column=value
I personally prefer using this format of the command as it saves you having to spend
time ensuring that your column list is in the same order as your value list.
CREATE or REPLACE
You can also use the REPLACE command when you are creating a table, function or
datatype. When you are creating one of these objects, if you use:
CREATE OR REPLACE object parameters
instead of just CREATE, the command will delete an existing object before creating a
new one. This stops an error if the object already exists, and also stops you having to delete
the object first if you do want to replace it.
DUPLICATE KEY UPDATE
When you are inserting data into a table with a primary key, you can have problems if you
insert a key that already exists. For example, if we were inserting a new row into our visitor
book shown in Figure 8.13, we could use the following:
Search WWH ::




Custom Search