postgres DML操作并返回结果

insert操作并返回结果

imos=#  CREATE TABLE person (
imos(#       id   SERIAL,
imos(#       name TEXT
imos(#    );
CREATE TABLE
imos=#
imos=#
imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id;
 id
----
  1
(1 row)

INSERT 0 1
imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING id;
 id
----
  2
(1 row)

INSERT 0 1
imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING *;
 id |     name
----+---------------
  3 | Blaise Pascal
(1 row)

INSERT 0 1
imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal') RETURNING null;
 ?column?
----------

(1 row)

INSERT 0 1
imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal') ;
INSERT 0 1

delete 操作并返回结果

imos=# select * from person;
 id |     name
----+---------------
  1 | Blaise Pascal
  2 | Blaise Pascal
  3 | Blaise Pascal
  4 | Blaise Pascal
  5 | Blaise Pascal
(5 rows)

imos=# delete from person where id=1;
DELETE 1
imos=# select * from person;
 id |     name
----+---------------
  2 | Blaise Pascal
  3 | Blaise Pascal
  4 | Blaise Pascal
  5 | Blaise Pascal
(4 rows)

imos=# delete from person where id=2 returning *;
 id |     name
----+---------------
  2 | Blaise Pascal
(1 row)

DELETE 1
imos=# delete from person where id=3 returning null;
 ?column?
----------

(1 row)

DELETE 1
imos=# select * from person;
 id |     name
----+---------------
  4 | Blaise Pascal
  5 | Blaise Pascal
(2 rows)

imos=# delete from person where id=4 returning name;
     name
---------------
 Blaise Pascal
(1 row)

DELETE 1
imos=# delete from person where id=5 returning id;
 id
----
  5
(1 row)

DELETE 1
imos=# select * from person;
 id | name
----+------
(0 rows)

update操作并返回结果

imos=# select * from person;
 id | name
----+------
(0 rows)

imos=#
imos=#
imos=#
imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal') ;
INSERT 0 1
imos=# select * from person;
 id |     name
----+---------------
  6 | Blaise Pascal
(1 row)

imos=#    INSERT INTO person (name) VALUES ('Blaise Pascal32') ;
INSERT 0 1
imos=# select * from person;
 id |      name
----+-----------------
  6 | Blaise Pascal
  7 | Blaise Pascal32
(2 rows)

imos=# update person set name='test' where id=6 returning *;
 id | name
----+------
  6 | test
(1 row)

UPDATE 1
imos=# select * from person;
 id |      name
----+-----------------
  7 | Blaise Pascal32
  6 | test
(2 rows)

imos=# update person set name='test' where id=7 returning name;
 name
------
 test
(1 row)

UPDATE 1
imos=# select * from person;
 id | name
----+------
  6 | test
  7 | test
(2 rows)

posted @ 2019-07-20 13:05  岳麓丹枫  阅读(262)  评论(0编辑  收藏  举报