postgresql修改列型別
阿新 • • 發佈:2019-02-01
習慣了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)
會發現無法新增成功呢?
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 | | |
成功!
BY 海無涯