天天看點

Postgresql實戰經驗之alter table 開小差了

Postgresql實戰經驗之alter table 開小差了

今天需要将一張有資料的表中一個字段varchar 類型轉換為timestamp類型,但是pg的alter table 語句卻開小差,出現了兩種問題,翻了pg10.5中文手冊、很多部落格文檔,做了對比實驗,哎,可謂費了九牛二虎之力才解決問題。

1.alter table 修改表定義(DDL操作)

官方文檔關于alter table 的用法

ALTER TABLE [ ONLY ] name [ * ]
    action [, ... ]
ALTER TABLE [ ONLY ] name [ * ]
    RENAME [ COLUMN ] column TO new_column
ALTER TABLE name
    RENAME TO new_name
ALTER TABLE name
    SET SCHEMA new_schema

這裡的 action 是下列之一:

    ADD [ COLUMN ] column type [ column_constraint [ ... ] ]
    DROP [ COLUMN ] column [ RESTRICT | CASCADE ]
    ALTER [ COLUMN ] column TYPE type [ USING expression ]
    ALTER [ COLUMN ] column SET DEFAULT expression
    ALTER [ COLUMN ] column DROP DEFAULT
    ALTER [ COLUMN ] column { SET | DROP } NOT NULL
    ALTER [ COLUMN ] column SET STATISTICS integer
    ALTER [ COLUMN ] column SET STORAGE { PLAIN | EXTERNAL | EXTENDED | MAIN }
    ADD table_constraint
    DROP CONSTRAINT constraint_name [ RESTRICT | CASCADE ]
    DISABLE TRIGGER [ trigger_name | ALL | USER ]
    ENABLE TRIGGER [ trigger_name | ALL | USER ]
    CLUSTER ON index_name
    SET WITHOUT CLUSTER
    SET WITHOUT OIDS
    SET ( storage_parameter = value [, ... ] )
    RESET ( storage_parameter [, ... ] )
    INHERIT parent_table
    NO INHERIT parent_table
    OWNER TO new_owner
    SET TABLESPACE new_tablespace
      

  

其中,修改表字段是用

alter table test alter colunm_name type colunm_type;
      

2.将表alter_table 中var_date字段類型varchar 轉換為timestamp

實驗步驟:

1)建表

2)插入資料

3)使用修改表字段語句進行字段類型轉換

Postgresql實戰經驗之alter table 開小差了

出現錯誤:

error:cloumn “var_date” cannot be autmatically to type timestamp without time zone Hint:You might need to specify "using var_date::timestamp without time zone"

查閱資料:

This form changes the type of a column of a table. Indexes and simple table constraints involving the column willbe automatically converted to use the new column type by reparsing the originally supplied expression. The optional COLLATE clause specifies a collation for the new column; if omitted, the collation is the default for the new column type. The optional USING clause specifies how to compute the new column value from the old; if omitted, the default conversion is the same as an assignment cast from old data type to new. A USING clause must be provided if there is no implicit or assignment cast from old to new type.

大緻意思是:轉換類型的時候有隐含類型轉換的時候,會自動轉換,如果沒有,那麼就必須使用using顯性指定一下轉換規則

那麼我們使用如alter table 就成功轉換:

alter table alter_table alter var_date type timestamp using var_date::timestamp without time zone
      

結果如下:

Postgresql實戰經驗之alter table 開小差了

3.失敗二alter table

上述顯式修改表字段,我以為這樣,我的正式表就可以修改成功,結果又出現另一種問題,如圖所示:

顯式修改字段類型出現錯誤如下:

error:invalid input syntax for type timestamp;

查了好多官方文檔,也沒有這類解釋,後來經過高人指點,可能表中這個資料是null類型,轉換不了,可以先将表中這個資料轉換為timestamp類型,然後再用alter 語句轉換資料類型。

實驗如下:

1)先查表中字段這個資料,果然是"NULL"

2)修改表中資料為”timestamp“類型

3)顯式修改表中該字段類型為”timestamp“類型

如圖所示:

Postgresql實戰經驗之alter table 開小差了

這個小實驗,也算小錯誤吧,感覺還是實戰出真知!

參考部落格:

1.https://www.cnblogs.com/winkey4986/p/6274729.html

2.https://www.yiibai.com/manual/postgresql/sql-altertable.html

期間,咨詢了一下德哥,可以用自定義一個轉化函數,把不支援的類型處理一下,exception 轉化為你希望的值。大家可以試試!

作者:王雪,西安電子科技大學研究所學生畢業,現工作于某銀行軟體研發中心,負責資料庫pg後端開發應用,19年11月在合肥pg分享會上認識德哥,開始了與pg之旅。

繼續閱讀