ON DELETE SET NULL Targets Specific Columns
Use PostgreSQL's column-list ON DELETE SET NULL so a composite foreign key clears only the reviewer column and keeps its order.
Code
-- PostgreSQL 15+:ON DELETE SET NULL 可以只清空列清单中的列
-- The column list targets specific columns, not every foreign key column
CREATE TABLE approvals (
order_id bigint NOT NULL,
reviewer_id bigint NOT NULL,
approved_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (order_id, reviewer_id)
);
CREATE TABLE shipments (
id bigint PRIMARY KEY,
order_id bigint NOT NULL,
reviewer_id bigint,
carrier text NOT NULL,
CONSTRAINT shipments_approval_fk
FOREIGN KEY (order_id, reviewer_id)
REFERENCES approvals (order_id, reviewer_id)
ON DELETE SET NULL (reviewer_id)
);
INSERT INTO approvals (order_id, reviewer_id) VALUES (100, 42);
INSERT INTO shipments (id, order_id, reviewer_id, carrier) VALUES (1, 100, 42, 'fast');
-- 删除审批记录:只清空 reviewer_id,order_id 保留
DELETE FROM approvals WHERE order_id = 100 AND reviewer_id = 42;
SELECT id, order_id, reviewer_id, carrier FROM shipments;
-- 对照:不写列清单会尝试清空全部外键列,order_id 为 NOT NULL 时会失败
ALTER TABLE shipments DROP CONSTRAINT shipments_approval_fk;
ALTER TABLE shipments
ADD CONSTRAINT shipments_approval_fk
FOREIGN KEY (order_id, reviewer_id)
REFERENCES approvals (order_id, reviewer_id)
ON DELETE SET NULL;How the snippet works
Several statements share this file — SELECT / INSERT / DELETE / CREATE / ALTER, with approvals and shipments among the objects involved.
The pieces that do the work: a WHERE clause narrowing which rows one statement affects; NOT NULL qualifiers on the required columns; a PRIMARY KEY that enforces row identity; DEFAULT values filling omitted columns; a foreign-key REFERENCES guard tying rows to the parent table.
Applying it
Run it in the approvals database context.
Statements that matter
CREATE TABLE shipments (
id bigint PRIMARY KEY,
order_id bigint NOT NULL,
reviewer_id bigint,
carrier text NOT NULL,
CONSTRAINT shipments_approval_fk
FOREIGN KEY (order_id, reviewer_id)
REFERENCES approvals (order_id, reviewer_id)
ON DELETE SET NULL (reviewer_id)
);
CREATE TABLE approvals (
order_id bigint NOT NULL,
reviewer_id bigint NOT NULL,
approved_at timestamptz NOT NULL DEFAULT now(),
PRIMARY KEY (order_id, reviewer_id)
);
INSERT INTO shipments (id, order_id, reviewer_id, carrier) VALUES (1, 100, 42, 'fast');
Interactive Tools for This Topic
PostgreSQL EXPLAIN AnalyzerAnalyze PostgreSQL EXPLAIN ANALYZE text or FORMAT JSON plans locally to flag Seq Scans, slow nodes, row misestimates, disk sorts, and index hints.SQL DDL to TypeScript ConverterConvert SQL CREATE TABLE DDL statements into strict TypeScript interfaces with nullable, array, enum, JSONB, and Date or ISO string column mappings.DSN Connection String ParserParse PostgreSQL, MySQL, Redis, MongoDB, and AMQP DSN URLs locally with masked credentials, multi-host support, default ports, and SSL/pool inspection.Multi-Mode Escape & Unescape CodecEscape and unescape strings safely across JSON, JavaScript, SQL single-quoted literals, POSIX shell single quotes, and RFC 4180 CSV cells in your browser.
FAQ
- What are the relations in play here?
- `approvals`, `shipments`.
Report a problem with this exampleOpens a pre-filled issue on GitHub.