PostgreSQL資料庫修改sql表的方法彙總

2021-09-30 12:14:58 字數 3237 閱讀 1079

一,修改表

postgresql 提供了一族命令用於修改現有表。

可以實現:

增加字段,

刪除字段,

增加約束,

刪除約束,

修改預設值,

重新命名字段,

重新命名表。

這些操作可以用:alter table命令執行的。

1,增加字段

要增加乙個字段,使用這條命令:

alter table products add column description text;

新增的字段對於表中已經存在的行而言最初將先填充空值。

你也可以同時在該字段上定義約束,使用通常的語法:

alter table products add column description text check (description <> '');

乙個新字段不能用非空約束,因為最初的時候該欄位必須包含空值。 但是你可以稍後增加乙個非空約束。同樣,你也不能在乙個新字段 上定義預設值。根據 sql 標準的說明,這樣需要對現存行的新 字段填充預設值,而這個特性還沒有實現。但是你可以稍後調整 字段預設。

2,刪除字段

除乙個字段:

alter table products drop column description;

3,增加約束

要增加乙個約束,使用表約束語法。比如:

alter table products add check (name <> '');

alter table products add constraint some_name unique (product_no);

alter table products add foreign key (product_group_id) references product_groups;

要增加乙個不能寫成表約束的非空約束,使用下面語法:

alter table products alter column product_no set not null;

這個約束將立即進行檢查,所以表在加入約束之前必須符合約束條件。

4,刪除約束

要刪除乙個約束,你需要知道它的名字。如果你給了它乙個名字, 那麼事情就好辦了。

否則系統會分配乙個生成的名字,這樣你就需要 把它找出來了。psql 的命令 \d tablename 在這兒可以幫忙﹔

其它介面可能也提供了檢查表的細節的方法。

命令:

alter table products drop constraint some_name;

除了非空約束外,所有約束型別都這麼用。要刪除非空型別,用

alter table products alter column product_no drop not null;

(要記得非空約束沒有名字。)

5,改變預設值

要給乙個字段設定預設值,使用乙個象下面這樣的命令:

alter table products alter column price set default 7.77;

要刪除預設值,用

alter table products alter column price drop default;

這樣相當於把預設設定為空,至少在 postgresql裡是這樣的。

如果刪除乙個還沒有定義的預設值不算錯誤,因為預設隱含就是空值。

6,給字段改名字

重新命名乙個字段:

alter table products rename column product_no to product_number;

7,給表改名字

to rename a table:

alter table products rename to items;

8,  postgresql 9.0以後更改

字段長度

會重寫表,如果表比較大,那麼表會加鎖,需要很長時間,這裡介紹一種方法通過

修改pg_attribute.atttypmod

字段修改長度

,不需要重寫表

假如表中atttypemod為6,變長型別頭資訊占去4個位元組,這裡6-4=2就是我們欄位的

長度。檢視修改如下:

select atttypmod from pg_attribute where attrelid='hall_info'::regclass and attname='holiday';

update pg_attribute set atttypmod=19 where attrelid='hall_info'::regclass and attname='holiday';   (hall_info:表名,holiday:欄位名,該字段長度改為:15)

注:建立乙個作為外來鍵的字段company_id

首先:     alter table dtb_order add column company_id int default 0;

再:         alter table dtb_order add foreign key(company_id) references dtb_transinfo;

實際專案中經常會遇到要修改表結構的需求, 現在總結下postgres中修改表結構

比如, 現在有個表,

create table  test

(id  bigint not null,

name character varying,

constraint pk_test primary key (id)      -------這個個約束(constraint), 主鍵約束

) with (

oids=false

);alter table pets

owner to postgres;

1. 增加列, 修改主鍵

之前表有2個字段, id和name, id是主鍵, 現在有需求增加乙個index, 並且需求是主鍵改為id和index, sql語句如下

alter table test add column test_index smallint not null default 0;          -----------增加乙個列 test_index

alter table test drop constraint pk_test;        ----------------刪除之前的主鍵約束

alter table test add constraint pk_test primary_key(id, test_index);           ------------重新增加乙個主鍵

postgresql模板資料庫

template0和template1為postgresql資料庫的模板資料庫,新建的資料庫預設使用template1作為模板。template0和template1的區別在於template0無法修改,因此你可以修改template1資料庫以定製新建立的資料庫。template資料庫無法被刪除 d...

postgresql資料庫安裝

安裝並初始化 1 解壓資料庫並放到指定目錄 在opt目錄下 tar xvzf postgresql 10.1 1 linux x64 binaries.tar.gz 解壓出來之後目錄為pgsql 2 mv pgsql usr local pgsql 3 建立pgsql使用者並設定密碼 useradd...

資料庫 postgresql 安裝

當前專案是使用django框架搭建介面層的業務,資料庫端使用了postgresql,這裡只是簡單記錄下自己的安裝流程,因為開發機器使用的mac,所以流程只是針對mac。這裡我使用的homebrew,這個工具就不多說了,沒有用過的可以到這裡 執行下面命令即可 brew install postgres...