gpt4 book ai didi

sql - 从 Postgres 的外键约束中删除 `MATCH FULL` 的理想方法是什么?

转载 作者:行者123 更新时间:2023-11-29 11:49:04 24 4
gpt4 key购买 nike

我刚开始阅读 Seven Databases in Seven Weeks, Second Edition ,我目前正在完成 PostgreSQL 类(class)。

在创建新表时,我错误地将 MATCH FULL 包含在我的外键约束中。

这是我用来创建表的命令:

CREATE TABLE venues (
venue_id SERIAL PRIMARY KEY,
name varchar(255),
street_address text,
type char(7) CHECK (type in ('public', 'private') ) DEFAULT 'public',
postal_code varchar(9),
country_code char(2),
FOREIGN KEY (country_code, postal_code)
REFERENCES cities (country_code, postal_code) MATCH FULL
);

我正在查看 ALTER TABLE文档,但我想不出删除 MACTH FULL 的正确语法。

最佳答案

恐怕你不能ALTER它(only ALTER CONSTRAINT constraint_name [DEFERRABLE | NOT DEFERRABLE] [ INITIALLY DEFERRED | INITIALLY IMMEDIATE ]
)。您需要添加一个 simple match 并删除旧的,例如(简化结构):

db=# create table p(i int primary key);
CREATE TABLE
db=# create table f(i int references p(i) match full);
CREATE TABLE
db=# \d f
Table "public.f"
Column | Type | Modifiers
--------+---------+-----------
i | integer |
Foreign-key constraints:
"f_i_fkey" FOREIGN KEY (i) REFERENCES p(i) MATCH FULL

您看到了名称和匹配策略,所以现在:

db=# alter table f add constraint ms FOREIGN KEY (i) REFERENCES p(i) MATCH simple;
ALTER TABLE
db=# alter table f drop constraint f_i_fkey;
ALTER TABLE
db=# \d f
Table "public.f"
Column | Type | Modifiers
--------+---------+-----------
i | integer |
Foreign-key constraints:
"ms" FOREIGN KEY (i) REFERENCES p(i)

关于sql - 从 Postgres 的外键约束中删除 `MATCH FULL` 的理想方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51480443/

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