Saturday 18 May 2019

Postgres: Insert data into table


In my previous post, I explained how to create a table. In this post, I am going to show how to insert data into table employee.

myOrg-# \d employee
                     Table "public.employee"
 Column |         Type          | Collation | Nullable | Default 
--------+-----------------------+-----------+----------+---------
 id     | integer               |           |          | 
 name   | character varying(20) |           |          | 

Using INSERT INTO statement, you can insert records into a table.

INSERT INTO employee VALUES (1, 'Hari Krishna');
INSERT INTO employee VALUES (2, 'Gopi');

You can also specify the fields order, while inserting.


INSERT INTO employee (name, id) VALUES ('Sudhir', 3);

myOrg=# INSERT INTO employee VALUES (1, 'Hari Krishna');
INSERT 0 1
myOrg=# 
myOrg=# INSERT INTO employee VALUES (2, 'Gopi');
INSERT 0 1
myOrg=# 
myOrg=# INSERT INTO employee (name, id) VALUES ('Sudhir', 3);
INSERT 0 1
myOrg=# 
myOrg=# SELECT * FROM employee;
 id |     name     
----+--------------
  1 | Hari Krishna
  2 | Gopi
  3 | Sudhir
(3 rows)



Previous                                                 Next                                                 Home

No comments:

Post a Comment