pg學習 資料更新

2021-07-29 22:00:02 字數 3495 閱讀 4363

資料更新

1、插入

語法結構

highgo=# \h insert

command: insert

description: create new rows in a table

syntax:

[ with [ recursive ] with_query [, ...] ]

insert into table_name [ ( column_name [, ...] ) ]

[, ...] ) [, ...] | query }

[ returning * | output_expression [ [ as ] output_name ] [, ...] ]

單條記錄插入

highgo=# select * from test1;

id | name

----+------

(0 rows)

highgo=# insert into test1 values(001,'adam');

insert 0 1

部分字段插入

highgo=# insert into test1(id) values(002);

insert 0 1

highgo=# select * from test1;

id | name

----+------

1 | adam

2 |

(2 rows)

通過其他表插入記錄

highgo=# insert into test1(id,name)

highgo-# select id,name from test2;

insert 0 1

highgo=# select * from test1;

id | name

----+--------

1 | adam

2 |

2 | lilith

(3 rows)

通過指令碼插入

[highgo@node1 ~]$ vi insert_test1.sql

insert into test1 values(003,'eva');

執行插入

highgo=# \i insert_test1.sql

insert 0 1

highgo=# select * from test1;

id | name

----+--------

1 | adam

2 |

2 | lilith

3 | eva

(4 rows)

2、更新操作

語法結構

highgo=# \h update

command: update

description: update rows of a table

syntax:

[ with [ recursive ] with_query [, ...] ]

update [ only ] table_name [ * ] [ [ as ] alias ]

set |

( column_name [, ...] ) = ( [, ...] ) } [, ...]

[ from from_list ]

[ where condition | where current of cursor_name ]

[ returning * | output_expression [ [ as ] output_name ] [, ...] ]

highgo=# update test1 set name='adam' where id=2;

update 2

highgo=# select * from test1;

id | name

----+------

1 | adam

3 | eva

2 | adam

2 | adam

(4 rows)

利用子查詢更新多列

highgo=# update test2

highgo-# set name=(select name from test1 where id=1),

highgo-# id=(select id from test1 where name='adam')

highgo-# where id=2;

update 2

highgo=# select * from test2;

id | name

----+------

1 | adam

1 | adam

(2 rows)

通過其他表的條件更新資料

highgo=# update test1

highgo-# set name=(select name from test2 where id =2)

highgo-# where name='adam';

update 2

highgo=# select * from test1;

id | name

----+--------

3 | eva

1 | eva

2 | lilith

2 | lilith

(4 rows)

3、刪除操作

語法結構

highgo=# \h delete

command: delete

description: delete rows of a table

syntax:

[ with [ recursive ] with_query [, ...] ]

delete from [ only ] table_name [ * ] [ [ as ] alias ]

[ using using_list ]

[ where condition | where current of cursor_name ]

[ returning * | output_expression [ [ as ] output_name ] [, ...] ]

highgo=# delete from test1 where id=2;

delete 2

highgo=# select * from test1;

id | name

----+------

3 | eva

1 | eva

(2 rows)

通過其他表的條件刪除資料

highgo=# delete from test1 where name in (select name from test2);

delete 2

highgo=# select * from test1;

id | name

----+------

(0 rows)

PG資料庫學習隨筆 1

啟動過程中遇到這個報錯 root db01 createdb mydb createdb could not connect to database template1 fatal role root does not exist我的是centos7系統.使用的yum裝的pg,從網上找了好久,沒有找...

PG資料目錄介紹

pg資料目錄介紹 pg version pg版本,如10 base 每個 database 會在 base 目錄下有乙個子目錄,儲存資料庫檔案 global 存放的檔案用於儲存全域性的系統表資訊和全域性控制資訊 pg commit ts 包含已提交事務的時間 pg dynshmem 包含動態共享記憶...

pg學習 基本表定義 資料型別

資料型別 1 字串型別 char 型別 描述單個位元組的字段。char length 型別 存放定長的字元到字串中,不足 length 的字串,用空格進行 補充。varchar length 型別 存放變長的字串,但有長度限制 text 型別 不限制字串的數目,通常用於描述長度變化較大或長度不可預知...