怎么使用PostgreSQL12中的generatedcolumns-創(chuàng)新互聯(lián)

這篇文章主要介紹“怎么使用PostgreSQL 12中的generated columns”,在日常操作中,相信很多人在怎么使用PostgreSQL 12中的generated columns問題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”怎么使用PostgreSQL 12中的generated columns”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!

創(chuàng)新互聯(lián)長期為上千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為防城企業(yè)提供專業(yè)的網(wǎng)站設(shè)計(jì)制作、成都做網(wǎng)站,防城網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

在其他數(shù)據(jù)庫中,generated columns又被成為計(jì)算列(calculated columns)/虛擬列(virtual columns)等.

簡(jiǎn)介
在PG 11或以前的版本中,generated columns是不支持的:

testdb=# drop table if exists t_generated_col;
NOTICE:  table "t_generated_col" does not exist, skipping
DROP TABLE
testdb=# create table t_generated_col
testdb-# (n1 int,
testdb(#  n2 int,
testdb(#  c1 varchar(10),
testdb(#  c2 varchar(10),
testdb(#  counter int generated always as (n1 + n2) stored,
testdb(#  link varchar(20) generated always as (c1 || c2) stored);
ERROR:  syntax error at or near "("
LINE 6:  counter int generated always as (n1 + n2) stored,
                                         ^

counter列的值由n1和n2相加而得,而link列的值有c1和c2拼接而得.
在PG 12中,可以支持generated columns

testdb=# drop table if exists t_generated_col;
psql: NOTICE:  table "t_generated_col" does not exist, skipping
DROP TABLE
testdb=# create table t_generated_col
testdb-# (n1 int,
testdb(#  n2 int,
testdb(#  c1 varchar(10),
testdb(#  c2 varchar(10),
testdb(#  counter int generated always as (n1 + n2) stored,
testdb(#  link varchar(20) generated always as (c1 || c2) stored);
CREATE TABLE
testdb=# insert into  t_generated_col(n1,n2,c1,c2) values(1,1,'c1','c2');
INSERT 0 1
testdb=# 
testdb=# select * from t_generated_col;
 n1 | n2 | c1 | c2 | counter | link 
----+----+----+----+---------+------
  1 |  1 | c1 | c2 |       2 | c1c2
(1 row)

生成列的值由表達(dá)式的值計(jì)算而得,如為null則為null:

testdb=# insert into  t_generated_col(n1,n2,c1,c2) values(1,null,'c1',null);
INSERT 0 1
testdb=# select * from t_generated_col;
 n1 | n2 | c1 | c2 | counter | link 
----+----+----+----+---------+------
  1 |  1 | c1 | c2 |       2 | c1c2
  1 |    | c1 |    |         | 
(2 rows)

生成列不能被update:

testdb=# update t_generated_col set counter = 10;
psql: ERROR:  column "counter" can only be updated to DEFAULT
DETAIL:  Column "counter" is a generated column.

可以創(chuàng)建在其上創(chuàng)建index:

testdb=# create index idx_t_generated_col_counter on t_generated_col(counter);
CREATE INDEX

執(zhí)行查詢時(shí),跟普通列沒有什么區(qū)別:

  ^
testdb=# insert into  t_generated_col(n1,n2) select x,0 from generate_series(1,10000) x;
INSERT 0 10000
testdb=# 
testdb=# explain select * from t_generated_col where counter = 1000;
                                             QUERY PLAN                                             
----------------------------------------------------------------------------------------------------
 Index Scan using idx_t_generated_col_counter on t_generated_col  (cost=0.29..8.30 rows=1 width=23)
   Index Cond: (counter = 1000)
(2 rows)

到此,關(guān)于“怎么使用PostgreSQL 12中的generated columns”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注創(chuàng)新互聯(lián)-成都網(wǎng)站建設(shè)公司網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

新聞名稱:怎么使用PostgreSQL12中的generatedcolumns-創(chuàng)新互聯(lián)
網(wǎng)頁鏈接:http://bm7419.com/article44/dcojee.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供建站公司、響應(yīng)式網(wǎng)站、面包屑導(dǎo)航、營銷型網(wǎng)站建設(shè)、App設(shè)計(jì)網(wǎng)站設(shè)計(jì)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化