The INSERT statement is used to insert new records into a table.
Syntax
INSERT INTO table_name [(column1, column2, ...)]
[ OVERRIDING { SYSTEM | USER } VALUE ]
{ VALUES (value1, value2, ...) [, (value1, value2, ...), ...]
| query
| DEFAULT VALUES }
[ RETURNING { * | column1 [, column2, ...] } ];
Parameters:
table_name: Name of the table where you want to insert records.column1, column2, ...: The target columns. WithVALUESor a query and no target column list, supply values in table column order. UseDEFAULT VALUESwithout a target column list.value1, value2, ...: Values that correspond to the columns that you are inserting data into. It is possible to provide sets of values to be inserted within one query.query: A SELECT query that provides the data for the insertion.
Default and NULL values
When you execute an INSERT statement, it’s not necessary to provide a value for every column in the table. You can omit one or more columns from the column list. If you do so, Polypheny will use the default value defined for each omitted column.
Here’s an example:
CREATE TABLE employees (
id BIGSERIAL PRIMARY KEY,
first_name VARCHAR(100) NOT NULL,
last_name VARCHAR(100) NOT NULL,
salary INT DEFAULT 50000
);
In this table, the salary column has a default value of 50000. So, if you execute the following statement:
INSERT INTO employees (first_name, last_name)
VALUES ('John', 'Doe'), ('Jane', 'Smith');
Polypheny generates an id for each employee and uses the default salary of 50000.
If no default value is defined for the omitted columns, and those columns allow NULL values, Polypheny will insert NULL. If the column does not allow NULLs and no default value has been set, an error will occur.
On a table with an identity column, DEFAULT in a VALUES row requests the default for one column. On a table without an identity column, omit the corresponding column from the target column list to use its default.
INSERT INTO table_name DEFAULT VALUES inserts a single row using the default or generated value for every column, or NULL for nullable columns without a default.
Identity values
An identity column generates one value for each row that omits it or uses DEFAULT. An explicit NULL fails the identity column’s NOT NULL requirement unless OVERRIDING USER VALUE replaces it with a generated value.
| Clause | Effect |
|---|---|
| Omitted | GENERATED BY DEFAULT accepts explicit values. GENERATED ALWAYS requires omission of the column or DEFAULT. |
OVERRIDING SYSTEM VALUE |
Allows an explicit value for a GENERATED ALWAYS column, for example when importing existing IDs. |
OVERRIDING USER VALUE |
Ignores supplied identity values and generates fresh ones in either generation mode. |
Explicitly inserted values leave the sequence’s next value unchanged. Place the override clause after the target column list and before the row source.
CREATE TABLE jobs (
id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
label VARCHAR(40) NOT NULL DEFAULT 'queued'
);
INSERT INTO jobs DEFAULT VALUES RETURNING id, label;
INSERT INTO jobs (id, label)
OVERRIDING SYSTEM VALUE
VALUES (100, 'imported');
INSERT INTO jobs (id, label)
OVERRIDING USER VALUE
VALUES (100, 'imported')
RETURNING id, label;
The first insert generates ID 1 with label queued. The last insert ignores the supplied ID 100 and generates ID 2.
Returning inserted rows
Add RETURNING to get a result set with one row per inserted row, including generated values and defaults. Use * for all target columns or list unqualified target column names in the desired result order. RETURNING also works on tables whose keys are supplied explicitly.
INSERT INTO employees (first_name, last_name)
VALUES ('Alex', 'Rivera')
RETURNING id, salary;
Inserting Data from Another Table (INSERT…SELECT)
You can insert data from one table into another using an INSERT...SELECT statement. This is useful when you want to copy data from one table to another, or when you want to summarize data from one table and insert it into another.
The INSERT...SELECT statement combines the INSERT and SELECT statements. The SELECT statement retrieves data from one or more tables, and then the INSERT statement inserts that data into the specified table.
The syntax is as follows:
INSERT INTO target_table [(column1, column2, ...)]
SELECT column1, column2, ...
FROM source_table
WHERE condition;
For example, to insert employees with a salary higher than 50000 from the employees table into the high_salary_employees table, you would use:
INSERT INTO high_salary_employees (id, first_name, last_name)
SELECT id, first_name, last_name
FROM employees
WHERE salary > 50000;
This would copy all employees from the employees table with a salary higher than 50000 to the high_salary_employees table.
Example
The following SQL statement inserts a new record into the employees table:
INSERT INTO employees (first_name, last_name, salary)
VALUES ('John', 'Doe', 50000);
This statement inserts a new employee with the first name ‘John’, last name ‘Doe’, and a salary of 50000.
The following SQL statement inserts multiple new records into the employees table:
INSERT INTO employees (first_name, last_name, salary)
VALUES
('John', 'Doe', 50000),
('Jane', 'Smith', 60000),
('Bob', 'Johnson', 70000);
This statement inserts new employees with the provided names and salaries.
If you don’t provide the column names, you should provide values for all columns, in the order they were defined in the table. Here’s an example:
INSERT INTO employees
VALUES
(101, 'John', 'Doe', 50000),
(102, 'Jane', 'Smith', 60000),
(103, 'Bob', 'Johnson', 70000);
In this example, the first value of each set is the id of the employee.
You can also insert data into a table based on a query. Here’s an example:
INSERT INTO high_paid_employees (first_name, last_name, salary)
SELECT first_name, last_name, salary
FROM employees
WHERE salary > 70000;
In this example, a new record is inserted into the high_paid_employees table for each employee in the employees table who has a salary greater than 70000.