--- id: sql-primary-key title: "SQL Primary Key" category: "Database" status: "draft" verification_status: "conceptual" canonical_id: "" aliases: ["SQL PRIMARY KEY Constraint", "PRIMARY 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", "primarykey", "constraints"] raw_sources: ["https://www.w3schools.com/sql/sql_primarykey.asp"] applied_in: [] github_commit: "" --- # [[SQL Primary Key]] ## ๐ŸŽฏ ํ•œ ์ค„ ํ†ต์ฐฐ (One-line insight) PRIMARY KEY uniquely identifies every row (as a UNIQUE + NOT NULL combination), can span multiple columns, and is the anchor that FOREIGN KEY constraints in other tables point to. [S1] ## ๐Ÿง  ํ•ต์‹ฌ ๊ฐœ๋… (Core concepts) - **PRIMARY KEY constraint** โ€” uniquely identifies each record; ensures unique values and cannot contain NULL (combination of UNIQUE + NOT NULL). [S1] - **One per table** โ€” a table can have only ONE PRIMARY KEY, which can be a single column or a combination of columns (composite key). [S1] - **FK anchor** โ€” the primary key is the target that FOREIGN KEY constraints in other tables reference, enforcing referential integrity. [S1] - **ALTER TABLE prerequisite** โ€” adding a PRIMARY KEY via ALTER TABLE requires the column(s) already be declared NOT NULL. [S1] ## ๐Ÿ“– ์„ธ๋ถ€ ๋‚ด์šฉ (Details) - Single-column PK on creation: `CREATE TABLE Persons (ID int PRIMARY KEY, LastName varchar(255) NOT NULL, FirstName varchar(255), Age int);`. [S1] - Composite (multi-column) PK: `PRIMARY KEY (ID, LastName)`, or named: `CONSTRAINT PK_Person PRIMARY KEY (ID, LastName)`. [S1] - Add PK to existing table: `ALTER TABLE Persons ADD PRIMARY KEY (ID);`. [S1] - Drop PK: `ALTER TABLE Persons DROP CONSTRAINT PK_Person;` (SQL Server/Oracle/Access) vs. `ALTER TABLE Persons DROP PRIMARY KEY;` (MySQL). [S1] ## โš–๏ธ ๋ชจ์ˆœ ๋ฐ ์—…๋ฐ์ดํŠธ (Contradictions & updates) - **๋“œ๋กญ ๋ฌธ๋ฒ• ์ฐจ์ด**: SQL Server/Oracle/Access๋Š” `DROP CONSTRAINT`, MySQL์€ `DROP PRIMARY KEY`๋ฅผ ์‚ฌ์šฉ. [S1] ## ๐Ÿ› ๏ธ ์ ์šฉ ์‚ฌ๋ก€ (Applied in summary) ํ˜„์žฌ ๋ฐœ๊ฒฌ๋œ ์‹ค์ œ ์ ์šฉ ์‚ฌ๋ก€๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค โ€” FOREIGN KEY ์ฑ•ํ„ฐ์—์„œ ์ด PRIMARY KEY๋ฅผ ์ฐธ์กฐํ•˜๋Š” ๊ด€๊ณ„๊ฐ€ ๋ฐ”๋กœ ์ด์–ด์„œ ๋‹ค๋ค„์ง„๋‹ค. [S1] ## ๐Ÿ’ป ์ฝ”๋“œ ํŒจํ„ด (Code patterns) Composite primary key (SQL): ```sql CREATE TABLE Persons ( ID int, LastName varchar(255), FirstName varchar(255), Age int, CONSTRAINT PK_Person PRIMARY KEY (ID, LastName) ); ``` ## โœ… ๊ฒ€์ฆ ์ƒํƒœ ๋ฐ ์‹ ๋ขฐ๋„ - **์ƒํƒœ:** draft - **๊ฒ€์ฆ ๋‹จ๊ณ„:** conceptual - **์ถœ์ฒ˜ ์‹ ๋ขฐ๋„:** B (W3Schools โ€” widely used educational reference, not a primary standards body) - **์‹ ๋ขฐ ์ ์ˆ˜:** 0.90 - **์ค‘๋ณต ๊ฒ€์‚ฌ ๊ฒฐ๊ณผ:** ์‹ ๊ทœ ์ƒ์„ฑ (New discovery) ## ๐Ÿ”— ์ง€์‹ ๊ทธ๋ž˜ํ”„ (Knowledge Graph) - **์ƒ์œ„/๋ฃจํŠธ:** [[SQL Tutorial]] - **๊ด€๋ จ ๊ฐœ๋…:** [[SQL Foreign Key]], [[SQL Not Null]], [[SQL Unique]], [[SQL Auto Increment]] - **์ฐธ์กฐ ๋งฅ๋ฝ:** ํ…Œ์ด๋ธ” ์„ค๊ณ„์˜ ํ•ต์‹ฌ โ€” FOREIGN KEY๊ฐ€ ์ฐธ์กฐํ•˜๋Š” ๋Œ€์ƒ์ด์ž, ๋Œ€๊ฐœ AUTO INCREMENT์™€ ํ•จ๊ป˜ ์“ฐ์ธ๋‹ค. ## ๐Ÿ“š ์ถœ์ฒ˜ (Sources) - [S1] W3Schools โ€” SQL PRIMARY KEY Constraint โ€” https://www.w3schools.com/sql/sql_primarykey.asp ## ๐Ÿ“ ๋ณ€๊ฒฝ ์ด๋ ฅ (Change history) - 2026-07-04: Initial draft synthesized from the W3Schools "SQL PRIMARY KEY Constraint" page (Astra wiki-curation, P-Reinforce v3.1 format).