INSERT RETURNING in MariaDB
Last Updated :
12 Mar, 2024
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.
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.
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.
CREATE TABLE users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL UNIQUE,
email VARCHAR(255) NOT NULL UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
);
Output:
Creation of users tableExplanation: The query creates a table named users with columns id, username, email and created_at having respective constraints.
Example 1: Returning All Columns
The below query inserts three rows and also returns all the inserted rows with respect to all the columns of the table.
INSERT INTO users (username, email)
VALUES ('john.doe', '[email protected]'),
('jane.smith', '[email protected]'),
('alice.lee', '[email protected]')
RETURNING *;
Output:
Returning All ColumnsExplanation: The query inserts three rows with username and email values but returns all these rows with all the columns of the table including id and created_at.
Example 2: Returning Specific Columns
The below query inserts three rows and also returns all the inserted rows with respect to specified columns of the table.
INSERT INTO users (username, email)
VALUES ('david.kim', '[email protected]'),
('maria.garcia', '[email protected]'),
('michael.chen', '[email protected]')
RETURNING id, username;
Output:
Returning Specific ColumnsExplanation: The query inserts three rows with username and email values but returns all these rows with only the id and username columns.
Example 3: Returning with Select Expression
The below query inserts three rows and also returns all the inserted rows with columns that satisfies the SELECT expression.
INSERT INTO users (username, email)
VALUES ('sarah.nguyen', '[email protected]'),
('peter.johnson', '[email protected]'),
('emma.white', '[email protected]')
RETURNING username AS user_name, CONCAT('User ID:', id) AS user_id_message;
Output:
Returning with Select ExpressionExplanation: The query inserts three rows with username and email values but returns all these rows with values of username columns named as user_name and values of id column named as user_id_message concatenated with User ID: string.
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.
Similar Reads
Intersect Operator in MariaDB
MariaDB, a popular open-source relational database management system (RDBMS), offers a plethora of powerful features for data manipulation and querying. Among these features is the Intersect operator, a valuable tool for performing set operations on query results. In this article, We will learn bout
4 min read
Showing indexes in MariaDB
In the area of database optimization, indexes play an important role in enhancing performance and speeding up query execution. MariaDB which is a powerful open-source relational database system that offers several indexing options to fast the data retrieval and boost efficiency. In this article, we
3 min read
MariaDB INSERT Statement
MariaDB is a famous open-source relational database management system. It is renowned for its performance, scalability, and robust functions. One essential thing about operating with databases is the ability to insert information, and the INSERT statement plays a crucial role in this technique. In t
3 min read
Union Operator in MariaDB
MariaDB is an Open-Source Database system and MariaDB offers similar security features to MySQL, including access control, user authentication, and encryption. UNION operator is a fundamental part of MariaDB, a well-known database systeÂm. The UNION operator mergeÂs results from different SELECT que
5 min read
MariaDB Unique Index
MariaDB is a fast, scalable, open-source community-supported relational database management system thatâs also an enhanced version of MySQL. Content management systems (CMS) are a key application of MariaDB. A CMS is a publication system through which web creators can push and manage large quantitie
5 min read
Unique Constraint in MariaDB
MariaDB uses relational databases similar to SQL. SQL is the precedence of MariaDB similar to the SQL mariaDB also uses some of the querying language to process through the databases. The main advantage of the MariaDB is that it runs on various operating systems and supports many programming languag
7 min read
MariaDB MIN Functions
MariaDB is a relational database language that is similar to SQL. We know that in a relational database language, the data is stored in the form of relations which are nothing but the tables. Similar to SQL which has aggregate functions such as MIN(), MAX(), AVG(), and LEAST() functions. These aggre
5 min read
Insert Text in MS Word
MS-Word is a word processing application used worldwide by many users. It is also very popular because of the vast features and tools provided by it to its user. It is a very user-friendly and interactive application developed by Microsoft Corporation Ltd. It is used to create various documents like
3 min read
MariaDB Create Triggers
Triggers are a very useful and powerful feature of MariaDB. It is a database object associated with a table that activates if an INSERT, UPDATE or DELETE operations are performed. The name itself reflects their action as they run immediately without any human intervention when the respective operati
6 min read
SET Variable in MariaDB
In MariaDB, the SET statement is a main tool in variable handling. Users can assign values to variables, operate with them, and control database operations in various respects. This article includes a look at the SET variable usage in MariaDB and its syntax together with some examples. SET Variable
4 min read