【发布时间】:2012-05-08 13:07:56
【问题描述】:
在 PostgreSQL 8 中是否可以将ON DELETE CASCADES 添加到下表中的两个外键而不删除后者?
# \d scores
Table "public.scores"
Column | Type | Modifiers
---------+-----------------------+-----------
id | character varying(32) |
gid | integer |
money | integer | not null
quit | boolean |
last_ip | inet |
Foreign-key constraints:
"scores_gid_fkey" FOREIGN KEY (gid) REFERENCES games(gid)
"scores_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
两个引用的表格都在下面 - 这里:
# \d games
Table "public.games"
Column | Type | Modifiers
----------+-----------------------------+----------------------------------------------------------
gid | integer | not null default nextval('games_gid_seq'::regclass)
rounds | integer | not null
finished | timestamp without time zone | default now()
Indexes:
"games_pkey" PRIMARY KEY, btree (gid)
Referenced by:
TABLE "scores" CONSTRAINT "scores_gid_fkey" FOREIGN KEY (gid) REFERENCES games(gid)
这里:
# \d users
Table "public.users"
Column | Type | Modifiers
------------+-----------------------------+---------------
id | character varying(32) | not null
first_name | character varying(64) |
last_name | character varying(64) |
female | boolean |
avatar | character varying(128) |
city | character varying(64) |
login | timestamp without time zone | default now()
last_ip | inet |
logout | timestamp without time zone |
vip | timestamp without time zone |
mail | character varying(254) |
Indexes:
"users_pkey" PRIMARY KEY, btree (id)
Referenced by:
TABLE "cards" CONSTRAINT "cards_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "catch" CONSTRAINT "catch_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "chat" CONSTRAINT "chat_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "game" CONSTRAINT "game_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "hand" CONSTRAINT "hand_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "luck" CONSTRAINT "luck_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "match" CONSTRAINT "match_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "misere" CONSTRAINT "misere_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "money" CONSTRAINT "money_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "pass" CONSTRAINT "pass_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "payment" CONSTRAINT "payment_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "rep" CONSTRAINT "rep_author_fkey" FOREIGN KEY (author) REFERENCES users(id)
TABLE "rep" CONSTRAINT "rep_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "scores" CONSTRAINT "scores_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
TABLE "status" CONSTRAINT "status_id_fkey" FOREIGN KEY (id) REFERENCES users(id)
我还想知道在前一个表中添加 2 个索引是否有意义?
更新:谢谢,而且我在邮件列表中得到了建议,我可以在 1 条语句中管理它,因此无需明确启动交易:
ALTER TABLE public.scores
DROP CONSTRAINT scores_gid_fkey,
ADD CONSTRAINT scores_gid_fkey
FOREIGN KEY (gid)
REFERENCES games(gid)
ON DELETE CASCADE;
【问题讨论】:
-
有点 OT,但我注意到您没有在引用列上创建索引(例如,
pref_scores.gid)。如果您在这些表中获得许多行,那么在没有这些的情况下删除引用的表将需要很长时间。一些数据库会自动在引用列上创建索引; PostgreSQL 把这留给你,因为在某些情况下它是不值得的。 -
谢谢!我实际上注意到删除需要很长时间,但不知道这是原因
-
什么情况下,外键索引不值得?
-
我将您的发现纳入我的回答中。 (那条语句也是一个事务。)
-
@AlexanderFarber:什么时候可以省略 FK 引用列上的索引?当有另一个索引不是完全匹配但效果很好时(例如,您可能有一个用于频繁相似性搜索的三元组索引,这也适用于 FK 删除)。当删除不频繁并且可以安排在非工作时间时。当表经常更新引用值时。当引用表非常小但经常更新时。异常经常发生,以至于 PostgreSQL 社区更愿意对其进行控制而不是使其自动化。
标签: postgresql constraints cascade cascading-deletes postgresql-8.4