What is Returned by INSERT?

The INSERT...RETURNING statement combines both INSERT and SELECT statements. It inserts one or more rows into a table and then returns a result containing the values of inserted rows depending upon the parameter.

After the RETURNING keyword, we can provide the column list or SELECT expression that specifies the columns to be returned. We can also use an asterisk (*) to get all the columns of the inserted rows.

Syntax:

INSERT INTO table_name (column1, column2, ...)
VALUES (value1, value2, ...)
RETURNING [ * | column_list | SELECT expression ];

Explanation:

  • column_list: You can directly provide the comma-separated list of columns whose values you want to retrieve from the inserted rows.
  • SELECT expression: You can also provide only a SELECT expression that specifies the values to be returned. The SELECT keyword is not required, specify only the expression.

INSERT RETURNING in MariaDB

MariaDB, an open–source relational database management system, continues to evolve with new features and enhancements. One such feature introduced in version 10.5 is the INSERT…RETURNING statement, which offers a convenient and efficient way to insert data into a table and retrieve the inserted rows in a single operation.

This feature simplifies queries and saves time by eliminating the need for separate insert and select statements.

In this article, we will learn about everything about the INSERT…RETURNING with the help of examples and so on.

Similar Reads

What is Returned by INSERT?

The INSERT...RETURNING statement combines both INSERT and SELECT statements. It inserts one or more rows into a table and then returns a result containing the values of inserted rows depending upon the parameter....

Examples of INSERT…RETURNING

To understand INSERT…RETURNING in MariaDB we need a table on which we will perform various operations and queries. Here we will consider a table called users which contains id, username, email, and created_at as Columns....

Conclusion

The INSERT…RETURNING statement in MariaDB provides a powerful and efficient way to insert data into a table and retrieve the inserted rows in a single operation. This feature simplifies queries, saves time and enhances the overall productivity of developers and database administrators. Whether you need to retrieve all columns, specific columns or custom expressions, INSERT…RETURNING offers flexibility and convenience, making it a valuable addition to your MariaDB....