外部キー制約
CautionForeign keys
ALTER TABLE <t> ADD FOREIGN KEY (<col>) REFERENCES <p>(<id>);
Enforces referential integrity; the ON DELETE policy decides deletion behavior — choose it deliberately.
Examples
ALTER TABLE orders ADD CONSTRAINT fk_user FOREIGN KEY (user_id) REFERENCES users(id);Guarantee order→user integrity
Syntax
ALTER TABLE t ADD CONSTRAINT fk FOREIGN KEY (col) REFERENCES parent(id) [ON DELETE CASCADE];
Before you run it
Existing orphans block creation.
Related commands
- CREATE TABLECreate tables
- SELECT ... JOINJoin tables
The same reference, with no signal at all
Every command on this site, held on your device. The app adds 632 more: 1,510 commands and 120 fix flows, searchable with no connection. Free to install, no ads (some content is a one-time purchase).
Get it free on the App Store