PostgreSQLでPRIMARY KEYの変更、削除方法

1.PRIMARY KEYの作成、削除
①PRIMARY KEYの作成
ALTER TABLE table ADD CONSTRAINT table_pkey PRIMARY KEY (field1, field2);

studentdb=> alter table stutbl add constraint stutbl_pkey primary key (stuid);
NOTICE: ALTER TABLE / ADD PRIMARY KEY will create implicit index “stutbl_pkey" for table “stutbl"
ALTER TABLE
②PRIMARY KEYの削除
ALTER TABLE table DROP CONSTRAINT table_pkey;

studentdb=> alter table stutbl drop constraint stutbl_pkey;
ALTER TABLE
studentdb=> \d rfideventtbl1
2.NOT NULL制約の追加と削除
追加
ALTER TABLE table ALTER COLUMN column SET NOT NULL;

alter table stutbl alter birth column set not null

削除
ALTER TABLE table ALTER COLUMN column DROP NOT NULL;
alter table stutbl alter birth column drop not null

DataBase

Posted by arkgame