You can insert a new record into a table in a database by using the INSERT statement. CUBRID supports INSERT ... VALUES, INSERT ... SET and INSERT ... SELECT statements.
INSERT ... VALUES and INSERT ... SET statements are used to insert a new record based on the value that is explicitly specified while the INSERT ... SELECT statement is used to insert query result records obtained from different tables. Use the INSERT VALUES or INSERT ... SELECT statement to insert multiple rows by using the single INSERT statement.
<INSERT ... VALUES statement>
INSERT [INTO] table_name [(column_name, ...)]
{VALUES | VALUE}({expr | DEFAULT}, ...)[,({expr | DEFAULT}, ...),...]
[ON DUPLICATE KEY UPDATE column_name = expr, ... ]
INSERT [INTO] table_name DEFAULT [ VALUES ]
<INSERT ... SET statement>
INSERT [INTO] table_name
SET column_name = {expr | DEFAULT}[, column_name = {expr | DEFAULT},...]
[ON DUPLICATE KEY UPDATE column_name = expr, ... ]
<INSERT ... SELECT statement>
INSERT [INTO] table_name [(column_name, ...)]
SELECT...
[ON DUPLICATE KEY UPDATE column_name = expr, ... ]
CREATE TABLE a_tbl1(
id INT UNIQUE,
name VARCHAR,
phone VARCHAR DEFAULT '000-0000'
);
--insert default values with DEFAULT keyword before VALUES
INSERT INTO a_tbl1 DEFAULT VALUES;
--insert multiple rows
INSERT INTO a_tbl1 VALUES (1,'aaa', DEFAULT),(2,'bbb', DEFAULT);
--insert a single row specifying column values for all
INSERT INTO a_tbl1 VALUES (3,'ccc', '333-3333');
--insert two rows specifying column values for only
INSERT INTO a_tbl1(id) VALUES (4), (5);
--insert a single row with SET clauses
INSERT INTO a_tbl1 SET id=6, name='eee';
INSERT INTO a_tbl1 SET id=7, phone='777-7777';
SELECT * FROM a_tbl1;
id name phone
=========================================================
NULL NULL '000-0000'
1 'aaa' '000-0000'
2 'bbb' '000-0000'
3 'ccc' '333-3333'
4 NULL '000-0000'
5 NULL '000-0000'
6 'eee' '000-0000'
7 NULL '777-7777'
INSERT INTO a_tbl1 SET id=6, phone='000-0000'
ON DUPLICATE KEY UPDATE phone='666-6666';
SELECT * FROM a_tbl1 WHERE id=6;
id name phone
=========================================================
6 'eee' '666-6666'
INSERT INTO a_tbl1 SELECT * FROM a_tbl1 WHERE id=7 ON DUPLICATE KEY UPDATE name='ggg';
SELECT * FROM a_tbl1 WHERE id=7;
id name phone
=========================================================
7 'ggg' '777-7777'
In INSERT ... SET syntax, the evaluation of an assignment expression is performed from left to right. If the column value is not specified, then the default value is assigned. If there is no default value, NULL is assigned.
CREATE TABLE tbl (a INT, b INT, c INT);
INSERT INTO tbl SET a=1, b=a+1, c=b+2;
SELECT * FROM tbl;
a b c
===================================
1 2 4
In the above example, b's value will be 2 and c's value will be 4 since a's value is 1.
CREATE TABLE tbl2 (a INT, b INT, c INT);
INSERT INTO tbl2 SET a=b+1, b=1, c=b+2;
In the above example, a's value will be NULL since b's value is not specified yet when assigning a's value.
SELECT * FROM tbl2;
a b c
===================================
NULL 1 3
CREATE TABLE tbl3 (a INT, b INT default 10, c INT);
INSERT INTO tbl3 SET a=b+1, b=1, c=b+2;
In the above example, a's value will be 11 since b's value is not specified yet and b's default is 10.
SELECT * FROM tbl3;
a b c
===================================
11 1 3
If you use the SELECT query in the INSERT statement, you can insert query results which satisfy the specified retrieval condition from one or many tables to the target table.
INSERT [INTO] table_name [(column_name, ...)]
SELECT...
[ON DUPLICATE KEY UPDATE column_name = expr, ... ]
The SELECT statement can be used in place of the VALUES keyword, or be included as a subquery in the column value list next to VALUES. If you specify the SELECT statement in place of the VALUES keyword, you can insert multiple query result records into the column of the table at once. However, there should be only one query result record if the SELECT statement is specified in the column value list.
--creating an empty table which schema replicated from a_tbl1
CREATE TABLE a_tbl2 LIKE a_tbl1;
--inserting multiple rows from SELECT query results
INSERT INTO a_tbl2 SELECT * FROM a_tbl1 WHERE id IS NOT NULL;
--inserting column value with SELECT subquery specified in the value list
INSERT INTO a_tbl2 VALUES(8, SELECT name FROM a_tbl1 WHERE name <'bbb', DEFAULT);
SELECT * FROM a_tbl2;
id name phone
=========================================================
1 'aaa' '000-0000'
2 'bbb' '000-0000'
3 'ccc' '333-3333'
4 NULL '000-0000'
5 NULL '000-0000'
6 'eee' '000-0000'
7 NULL '777-7777'
8 'aaa' '000-0000'
In a situation in which a duplicate value is inserted into a column for which the UNIQUE index or the PRIMARY KEY constraint has been set, you can update to a new value by specifying the ON DUPLICATE KEY UPDATE clause in the INSERT statement.
Note
<INSERT ... VALUES statement>
<INSERT ... SET statement>
<INSERT ... SELECT statement>
INSERT ...
[ON DUPLICATE KEY UPDATE column_name = expr, ... ]
--creating a new table having the same schema as a_tbl1
CREATE TABLE a_tbl3 LIKE a_tbl1;
INSERT INTO a_tbl3 SELECT * FROM a_tbl1 WHERE id IS NOT NULL and name IS NOT NULL;
SELECT * FROM a_tbl3;
id name phone
=========================================================
1 'aaa' '000-0000'
2 'bbb' '000-0000'
3 'ccc' '333-3333'
6 'eee' '000-0000'
--insert duplicated value violating UNIQUE constraint
INSERT INTO a_tbl3 VALUES(2, 'bbb', '222-2222');
ERROR: Operation would have caused one or more unique constraint violations.
With ON DUPLICATE KEY UPDATE, "affected rows" value per row will be 1 if a new row is inserted, and 2 if an existing row is updated.
--insert duplicated value with specifying ON DUPLICATED KEY UPDATE clause
INSERT INTO a_tbl3 VALUES(2, 'ggg', '222-2222')
ON DUPLICATE KEY UPDATE name='ggg', phone = '222-2222';
SELECT * FROM a_tbl3 WHERE id=2;
id name phone
=========================================================
2 'ggg' '222-2222'
2 rows affected.