外部キー制約
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 and flow from this site, held on your device and searchable with no connection. Free to install, no ads.
Get it free on the App Store