INSERT
There are two methods for inserting data into a table from within the database:
When specifying a column list using either method, any non-nullable fields not included in the list will be given default values--empty string for strings, and 0 for numerics. The fields in the column list and the values or fields selected must align.
Tip
If Multi-Head Ingest has been enabled on the database server, INSERT operations will automatically leverage it, when applicable.
See Loading Data for inserting data into a table from sources external to the database.
INSERT INTO ... VALUES
Records with literal values can be inserted into the database.
|
|
|
|
INSERT INTO ... SELECT
Records that are the result set of a query can be inserted into the database.
|
|
|
|
Upserting
To upsert records, inserting new records and updating existing ones (as denoted by primary key), use the KI_HINT_UPDATE_ON_EXISTING_PK hint. If the target table has no primary key, this hint will be ignored.
|
|
Important
By default, any record being inserted that matches the primary key of an existing record in the target table will be discarded, and the existing record will remain unchanged. The KI_HINT_UPDATE_ON_EXISTING_PK hint overrides this behavior, favoring the source records over the target ones.
UPDATE
Updates can set columns to specified constant values or expressions.
|
|
For example, to update employee with ID 5 to have a new manager, with ID 3, and to have a 10% salary increase:
|
|
Subqueries can be used in the expression list and within the SET clause. To update all the bottom earners in each department with a 5% salary increase using a subquery in the expression list:
|
|
To update a backup table to match the source table that was just updated for new salary values using a subquery within the SET clause:
|
|
Joins can be used within an UPDATE statement to update records based on the results of a JOIN.
Important
The JOIN statement cannot be part of the SET clause. Also, primary key column updates are not supported using JOIN operations. The table to be updated must be in the FROM clause.
For example, to update a backup table to match the source table that was just updated for new manager and salary values using an INNER JOIN:
|
|
Tip
Simplified JOIN syntax is also supported:
|
|
DELETE
Deletes records from a table.
|
|
For example, to delete employee with ID 6:
|
|
Subqueries can also be used in the expression list. To delete all the most recent hires in each department:
|
|