postgresql 修改列類型操作
習慣了Oracle中:
ALTER TABLE 表名 ALTER COLUMN 列名 新的數據類型[(長度)] NULL或NOT NULL
這種修改方式的時候,在pg中:
highgo=# create table p1 (id int,pswd varchar(30),time timestamp);
CREATE TABLE
highgo=# insert into p1 select generate_series(1,500000),md5('random()::text'),clock_timestamp();
錯誤: 對于可變字符類型來說,值太長了(30)
會發(fā)現(xiàn)無法添加成功呢?
highgo=# alter table p1 alter column pswd text NULL;
錯誤: 語法錯誤 在 "text" 或附近的
LINE 1: alter table p1 alter column pswd text NULL;
我們來看一下pg中的語法:
highgo=# \h auto
where action is one of:
ADD [ COLUMN ] [ IF NOT EXISTS ] column_name data_type [ COLLATE collation ] [ column_constraint [ ... ] ]
DROP [ COLUMN ] [ IF EXISTS ] column_name [ RESTRICT | CASCADE ]
ALTER [ COLUMN ] column_name [ SET DATA ] TYPE data_type [ COLLATE collation ] [ USING expression ]
ALTER [ COLUMN ] column_name SET DEFAULT expression
highgo=# alter table p1 alter COLUMN pswd type text ;
ALTER TABLE
highgo=# \d p1
Table "public.p1"
Column | Type | Collation | Nullable | Default
--------+-----------------------------+-----------+----------+---------
id | integer | | |
pswd | text | | |
time | timestamp without time zone | | |
成功!
補充:postgresql 修改字段類型為數組類型(text 改為 text[] )
語法:
alter table tablename alter columnname type oldcolumntype USING columnname:: newcolumntype
eg:
alter table dirty_track alter labels type text USING labels::text[];
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。如有錯誤或未考慮完全的地方,望不吝賜教。
相關文章
postgresql 中的幾個 timeout參數 用法說明
這篇文章主要介紹了postgresql中的幾個timeout參數用法說明,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01
Ruoyi從mysql切換到postgresql的幾個踩坑實戰(zhàn)
最近由于工作的原因,需要將Ruoyi從mysql切換到postgresql,所以這篇文章主要給大家介紹了關于Ruoyi從mysql切換到postgresql的幾個踩坑實戰(zhàn),需要的朋友可以參考下2023-02-02
使用PostgreSQL為表或視圖創(chuàng)建備注的操作
這篇文章主要介紹了使用PostgreSQL為表或視圖創(chuàng)建備注的操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

