PostgreSQL alter语法

本文介绍了SQL中对数据库表的操作,包括如何增加、删除列,设置和移除约束,修改默认值及数据类型,以及重命名列和表。这些基本操作对于数据库管理和维护至关重要。

增加列:

alter table products add column description text;

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

删除列:

alter table products drop column description

alter table products drop column description cascade;

增加约束:

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 drop constraint some_name;

alter table products alter column product_no drop not null;

修改默认值:

alter table products alter column price set default 7.77;

alter table products alter column price drop default;

修改数据类型:

alter table products alter column price type numeric(10,2);

列重命名:

alter table products rename column product_no to product_number;

表重命名:

alter table products rename to items;

修改所有者:

alter table table_name owner to new_owner;

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

请叫我曾阿牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值