INSERT statements, as with DELETE and UPDATE, behave in an atomic manner. That is, if an insert of more than one row fails, then all insertions of previous rows by the same statement are rolled back.
INSERT ON DUPLICATE KEY UPDATE
Zen v13 R2 extends INSERT with INSERT ON DUPLICATE KEY UPDATE. This insert capability automatically compares unique keys for values to be inserted or updated with those in the target table. If either a duplicate primary or an index key is found, then for those rows the values are updated. If no duplicate primary or index key is found, then new rows are inserted. In popular jargon, this behavior is called an “upsert.”
The INSERT can use either a values list or a SELECT query. As with all INSERT commands, the behavior is atomic.
Inserting Data Longer Than the Maximum Literal String
The maximum literal string supported by Zen is 15,000 bytes. You can handle data longer than this using direct SQL statements, breaking the insert into multiple calls. Start with a statement like this:
INSERT INTO table1 SET longfield = '15000 bytes of text' WHERE restriction
Then issue the following statement to add more data:
INSERT INTO table1 SET longfield = notefield + '15000 more bytes of text' WHERE restriction
The following INSERT statement uses a SELECT clause to retrieve from the Student table the ID numbers of students who have taken classes.
The statement then inserts the ID numbers into the Billing table.
INSERT INTO Billing (Student_ID)
SELECT ID FROM Student WHERE Cumulative_Hours > 0
============
The following example illustrates the use of the CURTIME(), CURDATE() and NOW() variables to insert the current local time, date, and time stamp values inside an INSERT statement.
CREATE TABLE Timetbl (c1 time, c2 date, c3 timestamp)
INSERT INTO Timetbl(c1, c2, c3) values(CURTIME(), CURDATE(), NOW())
============
The following example demonstrates basic usage of default values with INSERT and UPDATE statements.
This topic illustrates INSERT ON DUPLICATE KEY UPDATE. For simple INSERT, see Examples for INSERT.
For clarity, the query results in these examples show inserted values in black and updated values in red. Each example builds on the previous one, so you can execute them in series to see the behavior.
============
INSERT INTO with VALUES clause and without a column list. Unique index segment column values are available.
Subquery expression in UPDATE SET clause to update using values from the Person table in the Demodata sample database.
INSERT INTO t1 VALUES ( 1, 2, 3, 4, 5, 6, 7, 8 )
ON DUPLICATE KEY UPDATE t1.a = 10, t1.b = 20, t1.c = ( SELECT TOP 1 id FROM demodata.person ORDER BY id ), t1.d = ( SELECT TOP 1 id FROM demodata.person ORDER BY id DESC, last_name );
The following example shows possible error conditions because a column is defined as NOT NULL with no default value defined:
CREATE TABLE t1 (c1 INT DEFAULT 10, c2 INT NOT NULL, c3 INT DEFAULT 100 NOT NULL)
INSERT INTO t1 DEFAULT VALUES -- Error: No default value assigned for column <c2>.
INSERT INTO t1 VALUES (DEFAULT, DEFAULT, 10) -- Error: No default value assigned for column <c2>.
INSERT INTO t1 (c1,c2,c3) VALUES (1, DEFAULT, DEFAULT) -- Error: No default value assigned for column <c2>.
INSERT INTO t1 (c1,c3) VALUES (1, 10) -- Error: Column <c2> not nullable.
============
The following example shows what occurs when you use INSERT for IDENTITY columns and columns with default values.
CREATE TABLE t (id IDENTITY, c1 INTEGER DEFAULT 100)
INSERT INTO t (id) VALUES (0)
INSERT INTO t VALUES (0,1)
INSERT INTO t VALUES (10,10)
INSERT INTO t VALUES (0,2)
INSERT INTO t (c1) VALUES (3)
SELECT * FROM t
The SELECT shows the table contains the following rows:
1, 100
2, 1
10, 10
11, 2
12, 3
The first row illustrates that if zero is specified in the values clause for an IDENTITY column, then the value inserted is 1 if the table is empty.
The first row also illustrates that if no value is specified in the values clause for a column with a default value, then the specified default value is inserted.
The second row illustrates that if zero is specified in the values clause for an IDENTITY column, then the value inserted is one greater than the largest value in the IDENTITY column.
The second row also illustrates that if a value is specified in the values clause for a column with a default value, then the specified value overrides the default value.
The third row illustrates that if a value other than zero is specified in the values clause for an IDENTITY column, then that value is inserted. If a row already exists that contains the specified value for the IDENTITY column, then the message “The record has a key field containing a duplicate value(Btrieve Error 5)” is returned and the INSERT fails.
The fourth rows shows again that if zero is specified in the values clause for an IDENTITY column, then the value inserted is one greater than the largest value in the IDENTITY column. This is true even if gaps exist between the values (that is, the absence of one or more rows with IDENTITY column values less than the largest value).
The fifth row illustrates that if no value is specified in the values clause for an IDENTITY column, then the value inserted is one greater than the largest value in the IDENTITY column.