One - One Code All

Blog Content

postgresql添加各种约束语法非空约束唯一约束

PostgreSQL   2014-10-21 21:00:13

1. 添加主键

alter table goods add primary key(sid);


2. 添加外键

alter table orders add foreign key(goods_id) references goods(sid)  on update cascade on delete cascade;

on update cascade: 被引用行更新时,引用行自动更新; 

on update restrict: 被引用的行禁止更新;

on delete cascade: 被引用行删除时,引用行也一起删除;

on dellete restrict: 被引用的行禁止删除;


3. 删除外键

alter table orders drop constraint orders_goods_id_fkey;


4. 添加唯一约束

alter table goods add constraint unique_goods_sid unique(sid);


5. 删除默认值

alter table goods  alter column sid drop default;


6. 修改字段的数据类型

alter table goods alter column sid type character varying;


7. 重命名字段

alter table goods rename column sid to ssid;


8. 添加非空约束

alter table goods alter column sid set not null;


9. 添加默认值

alter table goods  alter column sid set default 0; 



上一篇:postgresql开启归档日志
下一篇:postgresql数据库基本操作,数据库表的显示

The minute you think of giving up, think of the reason why you held on so long.