gpt4 book ai didi

postgresql - 如何更改 PostgreSQL 中的约束定义?

转载 作者:行者123 更新时间:2023-11-29 13:26:21 25 4
gpt4 key购买 nike

我在 PostgreSQL 中创建了一个表,如下所示:

CREATE TABLE Table1 (
Id varchar(100) PRIMARY KEY CHECK (Id ~ '^[a-z0-9]{3,15}$'),
...
);

这将自动创建一个名为 table1_id_check 的约束。

现在我想将检查约束更改为

(Id ~ '^[a-z0-9]{3,}$')

我如何在 PostgreSQL 中将此作为单个语句执行,而不删除约束并再次重新创建它?

最佳答案

在事务中使用多个语句适用于支持在事务中使用此 DDL 的所有 SQL dbms。

begin transaction;
alter table table1
drop constraint table1_id_check;

alter table table1
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');
commit;

PostgreSQL 允许您在 ALTER TABLE 语句中使用多个子句。

alter table table1
drop constraint table1_id_check,
add constraint table1_id_check CHECK (Id ~ '^[a-z0-9]{3,}$');

关于postgresql - 如何更改 PostgreSQL 中的约束定义?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33197311/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com