SQL: Creating Databases and Tables - UPDATE

UPDATE

The UPDATE function changes the values of the columns in a table.

Syntax:

-- Update
UPDATE table
SET column1 = value1,
    column_2 = value2, ...
WHERE condition;

Example: Update Link

-- Update
UPDATE link
SET description = 'Empty Description';
## data frame with 0 columns and 0 rows
-- Select
SELECT * FROM link;
##   id            url   name       description  rel
## 1  1 www.google.com Google Empty Description <NA>
## 2  2 www.google.com Google Empty Description <NA>
## 3  3  www.yahoo.com  Yahoo Empty Description <NA>
## 4  4   www.bing.com   Bing Empty Description <NA>
## 5  5 www.amazon.com Amazon Empty Description <NA>

Example: Update Link with WHERE

-- Update description with A
UPDATE link
SET description = 'Name starts with an A'
WHERE name LIKE 'A%';
## data frame with 0 columns and 0 rows
-- Select
SELECT * FROM link;
##   id            url   name           description  rel
## 1  1 www.google.com Google     Empty Description <NA>
## 2  2 www.google.com Google     Empty Description <NA>
## 3  3  www.yahoo.com  Yahoo     Empty Description <NA>
## 4  4   www.bing.com   Bing     Empty Description <NA>
## 5  5 www.amazon.com Amazon Name starts with an A <NA>

Example: Update Link with another column

-- Update description with name
UPDATE link
SET description = name;
## data frame with 0 columns and 0 rows
-- Select
SELECT * FROM link;
##   id            url   name description  rel
## 1  1 www.google.com Google      Google <NA>
## 2  2 www.google.com Google      Google <NA>
## 3  3  www.yahoo.com  Yahoo       Yahoo <NA>
## 4  4   www.bing.com   Bing        Bing <NA>
## 5  5 www.amazon.com Amazon      Amazon <NA>