Changing Data

Objective

After completing this lesson, you will be able to change data.

Introduction

The following list provides an overview of the statements used to modify data:

INSERT
The INSERT statement adds rows to a database table.
UPDATE
The UPDATE statement changes values of existing rows in a table.
DELETE
The DELETE statement permanently removes existing rows from a table.
UPSERT

The UPSERT statement changes existing rows of a table if found, otherwise it inserts the new rows. It is a combination of INSERT and UPDATE. REPLACE is a synonym for UPSERT.

Adding Data

The INSERT statement is used to add new rows to a database table. If the table already contains rows with the same primary key values, or if you try to add several rows with the same primary key values, the DBMS checks and raises an exception.

You can use three different syntax versions to insert data. The first two options allow you to insert single rows only, the third option allows you to insert several rows with a single statement.

Single Row Insert, Pass Values by Column Order

Using INSERT you can specify a value for each column. If the sequence of the values corresponds to the sequence of columns, you do not need to list the column names. The order of the columns is set implicitly when you create the table.

Unlike some SQL dialects, HANA SQL requires the INTO keyword.

Insert a new row into the Official table:

Code Snippet
12
INSERT INTO Official VALUES ('P10', 'Ms J', 20, 'A10', 'P04');
PNRNAMEOVERTIMESALARYMANAGER
P10Ms J20A10P04

Single Row Insert, Pass Values by Name

Using INSERT you can specify a value for each column. If the sequence of the values does not match the sequence of the corresponding columns, you must list the column names in the correct sequence of the values.

Insert a new row into the Official table:

Code Snippet
12
INSERT INTO Official (Salary, Overtime, Name, PNr, Manager) VALUES ('A12', 50, 'Mr K', 'P11', 'P09');
PNRNAMEOVERTIMESALARYMANAGER
P11Ms K50A12P09

Omitting Values on Insert

You do not have to specify a value for each column of the INSERT. You must list the column names in the correct sequence for each column where you specify a value. Missing values will be replaced by NULL or the default value of the column.

Insert a new row into the Official table:

Code Snippet
12
INSERT INTO Official (PNr, Name) VALUES ('P12', 'Ms L');
PNRNAMEOVERTIMESALARYMANAGER
P12Ms L?A06?

Inserting Multiple Rows

You can INSERT data read from another table. The structure, number, and order of the columns selected from the source table must match the columns specified in the INSERT.

INSERT all owner IDs and names from city Wiesloch into the Official table:

Code Snippet
1234
INSERT INTO Official(PNr, Name) SELECT OwnerID, Name FROM Owner WHERE City = 'Wiesloch';
PNRNAMEOVERTIMESALARYMANAGER
H01Ms T?A06?
H06Ms W?A06?

Modifying Data

You can change the values of existing rows of a table with the UPDATE statement. The basic syntax of the UPDATE statement is:

Code Snippet
1234
UPDATE <table> SET <column> = <value> [, <column> = <value>, ...] [WHERE <condition>]

You can use the UPDATE statement to change only certain rows in a table by adding a WHERE clause.

The overtime value of officials in salary group A09 should be doubled:

Code Snippet
123
UPDATE Official SET Overtime = Overtime * 2 WHERE Salary = 'A09';
PNRNAMEOVERTIMESALARYMANAGER
P01Mr A20A09P04
P02Mr B10A10P04
P03Ms C40A09P04
P04Ms D?A12P09

Note

The WHERE clause is optional. However, omitting the WHERE clause updates all rows of the table, which may not be the intention.

In practice, the WHERE clause often specifies the primary key values of the rows to be updated. Note that "missing rows" do not lead to an exception, they lead to no row being updated.

You can use the UPDATE statement to change values of multiple columns.

The overtime value of officials in salary group A09 should be tripled. In addition their salary group should be changed to A10.

Code Snippet
1234
UPDATE Official SET Overtime = Overtime * 3, Salary = 'A10' WHERE Salary = 'A09';
PNRNAMEOVERTIMESALARYMANAGER
P01Mr A30A10P04
P02Mr B10A10P04
P03Ms C60A10P04

The WHERE clause supports the same features as the SELECT statement, including subqueries. You can use an uncorrelated subquery on the WHERE clause of the UPDATE statement.

Officials who are contacts for the car owners are moved to salary group A15.

Code Snippet
1234
UPDATE Official SET Salary = 'A15' WHERE PNr IN (SELECT PersNumber FROM Contact);
PNRNAMEOVERTIMESALARYMANAGER
P07Ms G22A11P08
P08Ms H?A15P09
P09Mr I?A15?

You can also use correlated subqueries, such as functions in the WHERE clause. Usually a subquery is part of the WHERE clause, but you can also use a (correlated or uncorrelated) subquery in the SET clause.

For the following SQL statement, we assume that the table, Car, has an additional column, Ownername.

Code Snippet
1234
UPDATE Car c SET Ownername = (SELECT o.Name FROM Owner o WHERE o.OwnerID = c.Owner);
CARIDPLATENUMBERBRANDCOLORHPOWNEROWNERNAME
F01HD-V 106Fiatred75H06Ms W
F02HD-VW 4711VWblack120H03SAP AG
F03HD-JA 1972BMWblue184H03SAP AG

In addition to the INSERT and the UPDATE statements, SAP HANA also supports an UPSERT statement. This statement either updates rows in a table or inserts new rows.

UPSERT Statement Syntax

Due to its support for inserting data, the UPSERT statement comes in three syntax variants similar to the INSERT syntax variants:

  • UPSERT <table>(<column List>) VALUES (<value list>) WHERE <condition>

    If the condition evaluates to true, the matching rows are updated. If not, a new row with the values provided is inserted.

  • UPSERT <table>(<column List>) VALUES (<value list>) WITH PRIMARY KEY

    Updates the row with the primary key contained in the value list if present. If not, a new row with the values provided is inserted.

  • UPSERT <table>(<column List>) SELECT ... FROM ... WHERE ...

    For each row returned by the subquery, it checks if the table contains a row with the same primary key value. If yes, the existing row is updated with the row returned by the subquery. If not, a new row with the values provided is inserted.

The first variant looks similar to the INSERT statement, with the key word changed to UPSERT and a WHERE clause added.

The third variant looks like the INSERT statement in its SET variant and behaves much in the same way.

Alternatively, you can also use the REPLACE keyword. UPSERT and REPLACE are synonymous.

UPSERT Statements

UPSERT statements may act as either an INSERT or an UPDATE, depending on the existing data in the table.

The Employee table originally contains the rows:

DNUMBERNAMEDATEOFBIRTHREMAINDERDAYSDEPID
D100001Mr AJan 31, 197210A01
D100002Ms BFeb 29, 197215A01
D100003Mr CMar 31, 197220A01
D100004Ms DApr 30, 197210A02
D100005Ms EMay 31, 197210A02
D100006Mr FJun 30, 197240A03
D100007Ms G???
D100008Mr H???
D100009Ms IApr 1, 197210A04
D100010Ms JApr 30, 19720A04
D100011Mr KMay 1, 197220A04
D100012Mr LMay 31, 197220A04

UPSERT Values

Code Snippet
123
UPSERT Employee VALUES ('D100001', 'Mr X', '1975-01-31', 10, 'A01') WHERE DNumber = 'D100001';
Code Snippet
123
UPSERT Employee VALUES ('D100099', 'Ms X', '1975-01-31', 10, 'A01') WHERE DNumber = 'D100099';
DNUMBERNAMEDATEOFBIRTHREMAINDERDAYSDEPID
D100001Mr XJan 31, 197510A01
D100002Ms BFeb 29, 197215A01
D100003Mr CMar 31, 197220A01
D100004Ms DApr 30, 197210A02
D100012Mr LMay 31, 197220A04
D100099Ms XJan 31, 197510A01

Removing Data

Existing rows of a table are removed with the DELETE statement. The basic syntax for the DELETE is:

Code Snippet
12
DELETE FROM <table> [WHERE <condition>]

Deleting Selected Rows

You can DELETE selected rows from a table.

Delete all cars with horsepower less than 180:

Code Snippet
123
DELETE FROM Car WHERE HP < 180;
CARIDPLATENUMBERBRANDCOLORHPOWNER
F20?Audigreen84?

Note

The WHERE clause is optional. However, omitting the WHERE clause deletes all rows of the table, which may not be intended.

In practice, the WHERE clause often specifies the primary key values of the rows to be updated. Note that "missing rows" do not lead to an exception, but they lead to no row being removed.

The WHERE clause supports the same features as for the SELECT statement, including subqueries.

The WHERE Clause Works as Usual

You can use an uncorrelated subquery in the DELETE statement.

Delete all cars that are reported as stolen.

Code Snippet
1234
DELETE FROM Car WHERE PlateNumber IN (SELECT PlateNumber FROM Stolen);
CARIDPLATENUMBER
F02HD-VW 4711
F03HD-JA 1972
F04HD-AL 1002
F05HD-MM 3206
F07HD-ML 3206
F08HD-IK 1002
F09HD-UP 13
F10HD-MT 507
F11HD-MM 208
F12HD-XY 4711
F13HD-IK 1001
F14HD-MM 1977
F15HD-MB 3030
F16?
F18HD-MQ 2006
F19HD-VW 2012
F20?

You can also use correlated subqueries, such as functions in the WHERE clause.

Truncating Tables

A faster, more permanent form of DELETE is the TRUCNATE TABLE command. Unlike the DELETE command, TRUCNATE TABLE does not allow a WHERE clause and will always destroy all existing data in the target table. Additionally, it is possible to revert changes made by INSERT, UPDATE, UPSERT, or DELETE commands by use of transactions (to be discussed in a later unit), but TRUNCATE TABLE does not allow such reversion. Rows removed with TRUNCATE TABLE may not be recovered in the case of a mistake, so it should only be used in cases where an instant purge of a table is required.

The complete syntax for TRUCNATE TABLE is:

Code Snippet
1
TRUNCATE TABLE <table>

Note

There is actually one variant of the TRUCNATE TABLE command for tables that are partitioned, but the use and behavior of partitioned tables is outside the scope of this course.