In this tutorial, you will learn everything about PostgreSQL INSERT Statement to insert a new row into the PostgreSQL database table. In the previous tutorial, we have seen lots of PostgreSQL tutorials.
To insert a row into the database table, you have to use the INSERT INTO statement. When we create a new table, then we do have not any data inside that table. So first thing you have to insert some rows inside the table.PostgreSQL provides an INSERT statement to insert new rows inside the table.
Headings of Contents
PostgreSQL INSERT Statement Introduction
After creating a new table we need to insert records inside the table, PostgreSQL provides us an INSERT INTO command to insert new rows or records into the table.
Syntax
The below syntax represents the PostgreSQL INSERT
statement.
INSERT INTO table_name (column1, column2, column3, ....columnN) VALUES (value1, value2, value3,.....valueN);
- Firstly we specify the name of the table after the INSERT INTO statement in which you want to insert a new row.
- Next, we specify all the columns.
- After that, we provide the value corresponding to the names of the columns.
PostgreSQL INSERT Statement Example
Let’s create a new table Students for insert new rows into the table.
CREATE TABLE students (st_id integer, first_name varchar(50), last_name varchar(30), age integer, PRIMARY KEY (st_id));
In the above CREATE TABLE query we have created table students with st_id, first_name, last_name, and age fields and specify st_id as PRIMARY KEY which means st_id can not be repeated.
We have already discussed the PostgreSQL CREATE TABLE statement in our previous tutorial.
Insert one row into the table
To insert only a single into the table, use the following query.
Example
INSERT INTO students (st_id, first_name, last_name, age) VALUES(1, 'Vishvajit', 'Rao', 22);
Insert multiple rows into the table
To insert multiple rows into the table, use the following query.
Example
INSERT INTO students (st_id, first_name, last_name, age)
VALUES
(2, 'Priya', 'Kumari', 22),
(3, 'Priyansh', 'Kumar', 22),
(4, 'John', 'Doe', 24),
(5, 'Mariya', 'Saini', 22);
Conclusion
In this article, we have seen all about PostgreSQL INSERT statement to insert new rows or records into the table. This is one of the bases of the most useful command as you know that without data, the table is nothing so this command allows us to insert data inside the table.
I hope this article will have helped you, if you like this article, please share and keep visiting for further PostgreSQL interesting tutorials.
Ref:- Click Here
Thanks for your valuable time …. 👏👏