从 Postgres 的外键约束中删除“MATCH FULL”的理想方法是什么?
What is the ideal way to remove `MATCH FULL` from a Foreign Key constraint in Postgres?
我刚开始阅读 Seven Databases in Seven Weeks, Second Edition,目前正在完成 PostgreSQL
课程。
在创建新的 table 时,我错误地将 MATCH FULL
包含在我的外键约束中。
这是我用来创建 table 的命令:
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)
我刚开始阅读 Seven Databases in Seven Weeks, Second Edition,目前正在完成 PostgreSQL
课程。
在创建新的 table 时,我错误地将 MATCH FULL
包含在我的外键约束中。
这是我用来创建 table 的命令:
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)