--- id: sql-foreign-key title: "SQL Foreign Key" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["SQL FOREIGN KEY Constraint", "FOREIGN KEY constraint"] duplicate_of: "" source_trust_level: "B" confidence_score: 0.9 created_at: 2026-07-04 updated_at: 2026-07-04 review_reason: "" merge_history: [] tags: ["sql", "database", "w3schools", "foreignkey", "constraints"] raw_sources: ["https://www.w3schools.com/sql/sql_foreignkey.asp"] applied_in: [] github_commit: "" --- # [[SQL Foreign Key]] ## 🎯 ν•œ 쀄 톡찰 (One-line insight) FOREIGN KEY links a child table's column to a parent table's PRIMARY KEY, blocking both invalid inserts into the child and deletes from the parent that would orphan child rows. [S1] ## 🧠 핡심 κ°œλ… (Core concepts) - **FOREIGN KEY constraint** β€” establishes a link between two tables and prevents actions that would destroy that link. [S1] - **Child vs. parent table** β€” the table holding the foreign key column is the child table; the table holding the referenced primary key is the parent (referenced) table. [S1] - **Two-way protection** β€” blocks invalid inserts into the child (value must exist in the parent), and blocks deletes from the parent that would orphan matching child rows. [S1] - **Syntax (on create)** β€” `CONSTRAINT fk_Person FOREIGN KEY (PersonID) REFERENCES Persons(PersonID)` inside `CREATE TABLE`. [S1] - **Syntax (on alter)** β€” `ALTER TABLE Orders ADD CONSTRAINT fk_Person FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);`. [S1] ## πŸ“– μ„ΈλΆ€ λ‚΄μš© (Details) - Define on table creation: [S1] ```sql CREATE TABLE Orders ( OrderID int PRIMARY KEY, OrderNumber int NOT NULL, PersonID int, CONSTRAINT fk_Person FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) ); ``` - Add to an existing table: `ALTER TABLE Orders ADD CONSTRAINT fk_Person FOREIGN KEY (PersonID) REFERENCES Persons(PersonID);`. [S1] - Drop: `ALTER TABLE Orders DROP CONSTRAINT fk_Person;` (SQL Server/Oracle/Access) vs. `ALTER TABLE Orders DROP FOREIGN KEY fk_Person;` (MySQL). [S1] ## βš–οΈ λͺ¨μˆœ 및 μ—…λ°μ΄νŠΈ (Contradictions & updates) - **λ“œλ‘­ 문법 차이**: SQL Server/Oracle/AccessλŠ” `DROP CONSTRAINT`, MySQL은 `DROP FOREIGN KEY`λ₯Ό μ‚¬μš©. [S1] ## πŸ› οΈ 적용 사둀 (Applied in summary) ν˜„μž¬ 발견된 μ‹€μ œ 적용 사둀가 μ—†μŠ΅λ‹ˆλ‹€ β€” DROP TABLE μ±•ν„°μ—μ„œ 이미 "FK둜 μ°Έμ‘°λ˜λŠ” ν…Œμ΄λΈ”μ€ λ°”λ‘œ λ“œλ‘­ν•  수 μ—†λ‹€"λŠ” μ œμ•½μ΄ μ„ ν–‰ μ–ΈκΈ‰λ˜μ—ˆλ‹€. [S1] ## πŸ’» μ½”λ“œ νŒ¨ν„΄ (Code patterns) FOREIGN KEY on table creation (SQL): ```sql CREATE TABLE Orders ( OrderID int PRIMARY KEY, OrderNumber int NOT NULL, PersonID int, CONSTRAINT fk_Person FOREIGN KEY (PersonID) REFERENCES Persons(PersonID) ); ``` ## βœ… 검증 μƒνƒœ 및 신뒰도 - **μƒνƒœ:** draft - **검증 단계:** conceptual - **좜처 신뒰도:** B (W3Schools β€” widely used educational reference, not a primary standards body) - **μ‹ λ’° 점수:** 0.90 - **쀑볡 검사 κ²°κ³Ό:** μ‹ κ·œ 생성 (New discovery) ## πŸ”— 지식 κ·Έλž˜ν”„ (Knowledge Graph) - **μƒμœ„/루트:** [[SQL Tutorial]] - **κ΄€λ ¨ κ°œλ…:** [[SQL Primary Key]], [[SQL Drop Table]], [[SQL Inner Join]] - **μ°Έμ‘° λ§₯락:** ν…Œμ΄λΈ” κ°„ μ°Έμ‘° 무결성을 κ°•μ œν•  λ•Œ μ‚¬μš© β€” JOINμ—μ„œ ν™œμš©λ˜λŠ” 관계가 λ°”λ‘œ 이 μ œμ•½μœΌλ‘œ μ •μ˜λœλ‹€. ## πŸ“š 좜처 (Sources) - [S1] W3Schools β€” SQL FOREIGN KEY Constraint β€” https://www.w3schools.com/sql/sql_foreignkey.asp ## πŸ“ λ³€κ²½ 이λ ₯ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL FOREIGN KEY Constraint" page (Astra wiki-curation, P-Reinforce v3.1 format).