In this tutorial, We will be exploring how to add new row in the existing table.
Let's get started...
The INSERT
statement is used to insert a new row in the table.
Syntax
INSERT INTO table_name(column1, column2, …)
VALUES (value1, value2, …);
In the above syntax, we can see that first we have specify the table name with the INSERT INTO
keywords and the table will have list of comma-separated columns in the brackets.
Second, we have to provide values against each column after VALUES
keyword, that must be in the same orders as columns are.
If you want to return the row that is just inserted use the following code in the end:
RETURNING *;
If you want to return just some information, just specify the one or more column names after 'RETURNING' keyword like this:
RETURNING id;
In the above code I am returning ID only.
EXAMPLE
Getting the last insert 'id':
Suppose we want to return the id of the last in the table Named links
that has url
and the name
, 'id' and 'description' as columns.
INSERT INTO links (url, name)
VALUES('http://www.postgresql.org','PostgreSQL')
RETURNING id;
In the above code we are inserting 'url' and 'name' values and returning the id
of the last insert.
OUTPUT
Conclusion
In this article we learnt about the 'INSERT' statement and it's example.
Top comments (0)