來源:轉(zhuǎn)載 發(fā)布時(shí)間:2019-03-27 17:55:32 閱讀量:1599
pg_ctl -D /xx/pgdata start
pg_ctl --version
1 | psql -U username -d dbname -h hostip -p port |
\l
1 | \c dbname |
\d
1 | \d tablename |
1 | \d+ tablename |
1 | q |
例1(主鍵)
create table TESTCASE(
id INTEGER,
task_class INTEGER,
age TEXT,
PRIMARY KEY(id, task_class)
);
例2(自增SERIAL)
create table CREATETASK_CHKID_N(
id SERIAL PRIMARY KEY,
chk_id TEXT,
n INTEGER
);
其中SERIAL代表自增,默認(rèn)從1開始增加,每次自增1。
1 | drop table REL_CROSS_NODE; |
delete from [表名]
or
TRUNCATE TABLE [表名]
區(qū)別:Truncate table 表名 (注:不帶where語句) 速度快,而且效率高。
因?yàn)?/span>DELETE 語句每次刪除一行,并在事務(wù)日志中為所刪除的每行記錄一項(xiàng)。TRUNCATE TABLE 通過釋放存儲(chǔ)表數(shù)據(jù)所用的數(shù)據(jù)頁來刪除數(shù)據(jù),并且只在事務(wù)日志中記錄頁的釋放
1 | alter table [表名] add column [字段名] [類型]; |
alter table [表名] rename column [舊字段名] to [新字段名];
例:把表table_ex字段col_1限制非空去掉:ALTER TABLE table_eg ALTER col_1 drop not NULL
12.1 更改字段屬性,含空格
如果把字段colname把屬性Text轉(zhuǎn)化為int,原來text里面存在空啥的,可以
ALTER TABLE tablename ALTER COLUMN colname TYPE int USING (trim(colname)::integer);
12.2 更改字段由int4-->int8
alter table test_data alter column task_id type bigint using task_id::bigint
1 | alter table [表名] drop column [字段名]; |
1 | insert into [表名] (字段1,字段2) values (值1,值2); |
例如:
1 | insert into assist_info (id, maat_id, block_type) values ('F006', 'F7775', 1) |
注:
· 如果表中字段有大寫的字段,則需要對(duì)應(yīng)的加上雙引號(hào)。例:insert into test (no, "Name") values ('123', 'jihite');
· 值用單引號(hào)引起來(''),不能用雙引號(hào)("")
1 | delete from [表名] where [該行特征]; |
1 | update [表名] set [目標(biāo)字段名]=[目標(biāo)值] where [該行特征] |
1 | drop table [表名]; |
\q
1 2 3 4 5 | (select node_id from node where node_id=1 or node_id=2) except (select node_id from node where node_id=1); node_id --------- 2 (1 row) |
CREATE TABLE test_a_copy AS SELECT * FROM test_a;
psql -h localhost -d databaseName -U username -f filename
格式:
\o file_path
這樣就會(huì)把查詢結(jié)果存儲(chǔ)到輸出文件中。例
postgres=> \o /home/jihite/data/iu_data;
postgres=> select test_id from cdb_all_iu_data limit 10;
postgres=> select test_id from cdb_all_iu_data limit 5;
結(jié)果
test_id
--------------
2143
2153
2144
2156
2145
2154
2146
2157
2147
2155
(10 rows)
test_id
--------------
2143
2153
2144
2156
2145
(5 rows)
導(dǎo)出到線下文件
pg_dump --host hostname --port port --username username -t tablename -d dbname >/home/jihite/table.sql
把線下文件導(dǎo)入到數(shù)據(jù)庫
psql -h 10.125.7.68 -p 5432 -d postgres -U postgres -W postgres -f 2.sql
postgres=> \x
Expanded display is on.
postgres=> select * from cdb_chk_items where chk_id = 'R000000335';
-[ RECORD 1 ]+------------------------------------------------------------------------------------------------
chk_id | R000000335
chk_desc | 道路屬性與道路屬性相關(guān)檢查
chk_info | {"FIELDS": {"TRAFFIC_SIGN": ["TYPE", "GEOM"], "ROAD_LINK": ["ROAD_CLASS", "FORM_WAY", "GEOM"]}}
err_desc | {"ERR2": "roadclass取值錯(cuò)誤", "ERR1": "formway取值錯(cuò)誤"}
chk_level | 1
is_opened | 1
module_name | TRAFFIC_SIGN
invalid_flag | 1
rel_mode | MAIN_LAYER:TRAFFIC_SIGN
: TRAFFIC_SIGN|A,M|DIRECT
: ROAD_LINK|A,M,D|ATTR_REL
insert into A select * from B where id in ('a', 'b', 'c');
單字段索引
CREATE INDEX index_name ON table_name (field1);
多字段索引
CREATE INDEX index_name ON table_name (field1,field2);
查看所有表的索引使用情況
select
relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch
from
pg_stat_user_indexes
order by
idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;
查看某個(gè)表索引的使用情況
select
relname, indexrelname, idx_scan, idx_tup_read, idx_tup_fetch
from
pg_stat_user_indexes
where
relname = table_name
order by
idx_scan asc, idx_tup_read asc, idx_tup_fetch asc;
在線
客服
服務(wù)時(shí)間:周一至周日 08:30-18:00
選擇下列產(chǎn)品馬上在線溝通:
客服
熱線
7*24小時(shí)客服服務(wù)熱線
關(guān)注
微信
關(guān)注官方微信