PageSourceSearch

https://www.sqlnoir.com/_next/static/chunks/7877.fe23a3f2dc6e3aab.js

js sqlnoir.com collected 2026-10-04 00:07:44 UTC 24,648 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[7877],{7877:function(e,s,a){a.r(s),a.d(s,{default:function(){return o}});var t=a(7437),n=a(7648),i=a(8054),r=a(4513);function o(){return(0,t.jsxs)("div",{className:"prose prose-lg max-w-none",children:[(0,t.jsxs)("div",{className:"not-prose my-8 p-6 bg-gray-50 rounded-xl border border-gray-200",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"Quick Navigation"}),(0,t.jsx)("div",{className:"flex flex-wrap gap-2",children:[{text:"Quick Answer",id:"quick-answer"},{text:"What is Primary Key?",id:"what-is-primary-key"},{text:"What is Foreign Key?",id:"what-is-foreign-key"},{text:"How They Work Together",id:"keys-work-together"},{text:"Referential Integrity",id:"referential-integrity"},{text:"Full Comparison",id:"full-comparison"},{text:"Common Mistakes",id:"common-mistakes"},{text:"Quiz",id:"quiz"},{text:"FAQ",id:"faq"}].map(e=>(0,t.jsx)("a",{href:"#".concat(e.id),className:"px-3 py-1 bg-amber-100 hover:bg-amber-200 text-amber-800 rounded-full text-sm transition-colors",children:e.text},e.id))})]}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"If you've ever stared at a database and wondered why some columns are marked as keys while others aren't, you're not alone. Primary keys and foreign keys are the backbone of relational databases, but most explanations make them sound more complicated than they need to be. Let's fix that."}),(0,t.jsxs)("div",{className:"not-prose my-8 rounded-xl border border-amber-200/60 bg-amber-50/40 p-5",children:[(0,t.jsxs)("div",{className:"flex items-center gap-2 mb-2",children:[(0,t.jsx)("span",{className:"text-lg",children:"\uD83D\uDCAC"}),(0,t.jsx)("span",{className:"text-sm font-medium text-amber-800",children:"Real question from r/SQL"})]}),(0,t.jsx)("blockquote",{className:"text-gray-700 text-sm leading-relaxed italic",children:"“PLEASE explain foreign keys to me like I am six years old”"})]}),(0,t.jsx)("h2",{id:"quick-answer",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"Quick Answer: Primary Key vs Foreign Key"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Here's the TL;DR:"}),(0,t.jsxs)("ul",{className:"list-disc pl-6 mb-6 space-y-2 text-gray-700",children:[(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Primary key:"})," Unique identifier for each row in a table (like a suspect ID in a criminal database)"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Foreign key:"})," A reference to a primary key in another table (like case_id in an evidence table linking back to cases)"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Key difference:"})," Primary keys ensure uniqueness; foreign keys ensure relationships"]})]}),(0,t.jsx)(i.Rb,{headers:["Feature","Primary Key","Foreign Key"],rows:[["Purpose","Uniquely identifies each row","Links to another table"],["Uniqueness","Must be unique","Can have duplicates"],["NULL allowed","No","Yes (usually)"],["Count per table","Only one","Multiple allowed"],["Automatically indexed","Yes","No (but recommended)"]],caption:"Quick reference comparison"}),(0,t.jsx)("h2",{id:"what-is-primary-key",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"What is a Primary Key?"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"A primary key is a column (or combination of columns) that uniquely identifies each row in a table. Think of it like a suspect ID in a criminal database: no two suspects share the same ID, and every suspect must have one."}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:(0,t.jsx)("strong",{children:"Four rules of primary keys:"})}),(0,t.jsxs)("ol",{className:"list-decimal pl-6 mb-6 space-y-2 text-gray-700",children:[(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Unique:"})," No duplicates allowed"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Not null:"})," Every row must have a value"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Immutable:"})," Should rarely change (changing IDs causes chaos)"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"Indexed:"})," Automatically indexed for fast lookups"]})]}),(0,t.jsx)(r._N,{clauses:[{keyword:"CREATE TABLE",code:"CREATE TABLE suspects (",annotation:"Define a new table"},{keyword:"PRIMARY KEY",code:"suspect_id INT PRIMARY KEY,",annotation:"This column uniquely identifies each suspect"},{keyword:"columns",code:"name VARCHAR(100),\n  age INT,\n  last_seen DATE\n);",annotation:"Other suspect information"}],caption:"CREATE TABLE with primary key"}),(0,t.jsxs)("p",{className:"text-gray-700 leading-relaxed mb-6",children:["You can also define a ",(0,t.jsx)("strong",{children:"composite primary key"})," using multiple columns. This is common in junction tables:"]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"Composite Primary Key Example:"}),(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"CREATE TABLE case_suspects (\n  case_id INT,\n  suspect_id INT,\n  role VARCHAR(50),\n  PRIMARY KEY (case_id, suspect_id)\n);"}),(0,t.jsx)("p",{className:"text-gray-600 text-sm mt-2",children:"The combination of case_id + suspect_id must be unique."})]}),(0,t.jsx)("h2",{id:"what-is-foreign-key",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"What is a Foreign Key?"}),(0,t.jsxs)("p",{className:"text-gray-700 leading-relaxed mb-6",children:["A foreign key is a column that references the primary key of another table. It creates a relationship between tables and enforces"," ",(0,t.jsx)("em",{children:"referential integrity"})," (meaning you can't link to something that doesn't exist)."]}),(0,t.jsxs)("div",{className:"not-prose my-8 rounded-xl border border-amber-200/60 bg-amber-50/40 p-5",children:[(0,t.jsxs)("div",{className:"flex items-center gap-2 mb-2",children:[(0,t.jsx)("span",{className:"text-lg",children:"\uD83D\uDCAC"}),(0,t.jsx)("span",{className:"text-sm font-medium text-amber-800",children:"Community analogy from r/explainlikeimfive"})]}),(0,t.jsx)("blockquote",{className:"text-gray-700 text-sm leading-relaxed italic",children:"“Think of it like social security numbers. Your SSN is your primary key. When a bank stores your account, they use your SSN as a foreign key to link back to you.”"})]}),(0,t.jsxs)("p",{className:"text-gray-700 leading-relaxed mb-6",children:["In a detective database, evidence belongs to a specific case. The"," ",(0,t.jsx)("code",{children:"case_id"})," in the evidence table is a foreign key that references the ",(0,t.jsx)("code",{children:"case_id"})," primary key in the cases table:"]}),(0,t.jsx)(r._N,{clauses:[{keyword:"CREATE TABLE",code:"CREATE TABLE evidence (",annotation:"Evidence table stores clues"},{keyword:"PRIMARY KEY",code:"evidence_id INT PRIMARY KEY,",annotation:"Each piece of evidence has unique ID"},{keyword:"FOREIGN KEY",code:"case_id INT,\n  description TEXT,\n  FOREIGN KEY (case_id) REFERENCES cases(case_id)",annotation:"Links this evidence to a specific case"}],caption:"CREATE TABLE with foreign key"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Unlike primary keys, foreign keys can have duplicates (many pieces of evidence can belong to the same case) and can be NULL (evidence might not be assigned to a case yet)."}),(0,t.jsxs)("p",{className:"text-gray-700 leading-relaxed mb-6",children:["Want to practice JOINing tables with primary and foreign keys?"," ",(0,t.jsx)(n.default,{href:"/cases",className:"text-amber-700 hover:text-amber-900 underline font-medium",children:"SQLNoir's detective cases"})," ","challenge you to query across multiple related tables to crack mysteries."]}),(0,t.jsx)("h2",{id:"keys-work-together",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"How Primary and Foreign Keys Work Together"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Keys create relationships between tables. In a detective database, you might have suspects, cases, evidence, and interviews. Here's how they connect:"}),(0,t.jsx)(i.i9,{tables:[{name:"suspects",columns:["suspect_id","name","age","last_seen"],primaryKey:"suspect_id"},{name:"cases",columns:["case_id","title","status","lead_detective"],primaryKey:"case_id"},{name:"evidence",columns:["evidence_id","case_id","description","found_date"],primaryKey:"evidence_id"},{name:"interviews",columns:["interview_id","case_id","suspect_id","transcript"],primaryKey:"interview_id"}],relations:[{from:"cases",to:"evidence",fromColumn:"case_id",toColumn:"case_id",type:"1:N",label:"One case has many pieces of evidence"},{from:"cases",to:"interviews",fromColumn:"case_id",toColumn:"case_id",type:"1:N",label:"One case has many interviews"},{from:"suspects",to:"interviews",fromColumn:"suspect_id",toColumn:"suspect_id",type:"1:N",label:"One suspect can have many interviews"}],caption:"Detective database schema showing primary and foreign key relationships"}),(0,t.jsx)(r.M_,{variant:"clue",title:"The Key Insight",children:"Primary keys are the “anchor” that other tables reference. Foreign keys are the “links” that create the chain. Without this relationship, you'd need to duplicate data everywhere."}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Here's the complete schema in SQL:"}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"Complete Detective Database:"}),(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"-- Suspects table (primary key: suspect_id)\nCREATE TABLE suspects (\n  suspect_id INT PRIMARY KEY,\n  name VARCHAR(100),\n  age INT,\n  last_seen DATE\n);\n\n-- Cases table (primary key: case_id)\nCREATE TABLE cases (\n  case_id INT PRIMARY KEY,\n  title VARCHAR(200),\n  status VARCHAR(50),\n  lead_detective VARCHAR(100)\n);\n\n-- Evidence table (foreign key references cases)\nCREATE TABLE evidence (\n  evidence_id INT PRIMARY KEY,\n  case_id INT,\n  description TEXT,\n  found_date DATE,\n  FOREIGN KEY (case_id) REFERENCES cases(case_id)\n);\n\n-- Interviews table (foreign keys reference both cases AND suspects)\nCREATE TABLE interviews (\n  interview_id INT PRIMARY KEY,\n  case_id INT,\n  suspect_id INT,\n  transcript TEXT,\n  FOREIGN KEY (case_id) REFERENCES cases(case_id),\n  FOREIGN KEY (suspect_id) REFERENCES suspects(suspect_id)\n);"})]}),(0,t.jsx)("h2",{id:"referential-integrity",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"Referential Integrity: What Happens When You Delete Data?"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Here's where it gets interesting. What happens when you delete a case that has linked evidence? The foreign key constraint controls this behavior:"}),(0,t.jsxs)("ul",{className:"list-disc pl-6 mb-6 space-y-2 text-gray-700",children:[(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"CASCADE:"})," Delete the parent, and all child rows are automatically deleted too"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"RESTRICT:"}
1)," Prevent deletion if child rows exist (default in most databases)"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"SET NULL:"})," Set the foreign key to NULL when the parent is deleted"]}),(0,t.jsxs)("li",{children:[(0,t.jsx)("strong",{children:"NO ACTION:"})," Similar to RESTRICT, but checked at end of transaction"]})]}),(0,t.jsx)(i.wq,{nodes:[{label:"DELETE FROM cases WHERE case_id = 1",icon:"\uD83D\uDDD1️",type:"start"},{label:"Check for linked evidence",icon:"\uD83D\uDD0D",type:"process"},{label:"CASCADE: Delete evidence too",icon:"\uD83D\uDCA5",type:"process"},{label:"RESTRICT: Block the delete",icon:"\uD83D\uDEAB",type:"process"},{label:"SET NULL: Orphan the evidence",icon:"❓",type:"end"}],caption:"What happens when you delete a parent row depends on your ON DELETE setting"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Here's how you define these behaviors:"}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"ON DELETE CASCADE:"}),(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"CREATE TABLE evidence (\n  evidence_id INT PRIMARY KEY,\n  case_id INT,\n  description TEXT,\n  FOREIGN KEY (case_id) REFERENCES cases(case_id)\n    ON DELETE CASCADE\n);\n\n-- Now when you delete a case:\nDELETE FROM cases WHERE case_id = 1;\n-- All evidence linked to case 1 is automatically deleted!"})]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"ON DELETE RESTRICT (safer):"}),(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"CREATE TABLE evidence (\n  evidence_id INT PRIMARY KEY,\n  case_id INT,\n  description TEXT,\n  FOREIGN KEY (case_id) REFERENCES cases(case_id)\n    ON DELETE RESTRICT\n);\n\n-- Now when you try to delete a case with evidence:\nDELETE FROM cases WHERE case_id = 1;\n-- ERROR: Cannot delete - dependent records exist!"})]}),(0,t.jsx)(r.Nw,{before:{code:"cases: [(1, 'Miami Murder')]\nevidence: [(1, 1, 'Fingerprint'), (2, 1, 'Weapon')]",label:"Before: DELETE FROM cases WHERE case_id = 1",issues:["Case 1 exists with 2 pieces of evidence linked to it"]},after:{code:"cases: (empty)\nevidence: (empty)",label:"After: With ON DELETE CASCADE",improvements:["Case 1 deleted","Both evidence rows automatically removed","No orphaned records"]},caption:"CASCADE deletes cascade to all related child rows"}),(0,t.jsx)(r.M_,{variant:"warning",title:"Danger Zone",children:"CASCADE is powerful but dangerous. One wrong DELETE can wipe out years of related data. Use RESTRICT in production unless you have a specific reason for CASCADE."}),(0,t.jsx)("h2",{id:"full-comparison",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"Primary Key vs Foreign Key: Full Comparison"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Here's the comprehensive side-by-side breakdown:"}),(0,t.jsx)(i.Rb,{headers:["Aspect","Primary Key","Foreign Key"],rows:[["Purpose","Uniquely identifies rows","Creates relationships between tables"],["Uniqueness","Must be unique (no duplicates)","Duplicates allowed"],["NULL values","Never allowed","Allowed (unless constrained)"],["Count per table","Exactly one","Zero or more"],["Automatically indexed","Yes (always)","No (must add manually)"],["References another table","No","Yes (references a primary key)"],["Can be composite","Yes (multiple columns)","Yes (multiple columns)"],["Modification","Difficult to change","Can be updated if new value exists"],["Delete behavior","Cannot delete if referenced","Configurable (CASCADE, RESTRICT, etc.)"],["Performance impact","Speeds up lookups","Slows writes (integrity checks)"]],caption:"10-point comparison of primary and foreign keys"}),(0,t.jsx)(r.L2,{caseNumber:3,caseTitle:"The Miami Marina Murder",challenge:"Put your primary and foreign key knowledge to work. Join suspects, interviews, and hotel check-ins to find the killer.",difficulty:"intermediate",href:"/cases"}),(0,t.jsx)("h2",{id:"common-mistakes",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"Common Mistakes (And How to Avoid Them)"}),(0,t.jsx)("h3",{className:"text-xl font-bold text-gray-900 mt-8 mb-4",children:"Mistake 1: Forgetting the Foreign Key Constraint"}),(0,t.jsxs)("p",{className:"text-gray-700 leading-relaxed mb-6",children:["Just because a column is named ",(0,t.jsx)("code",{children:"customer_id"})," doesn't make it a foreign key. You need to explicitly define the constraint:"]}),(0,t.jsx)(r.Nw,{before:{code:"CREATE TABLE orders (\n  order_id INT PRIMARY KEY,\n  customer_id INT  -- No constraint!\n);",label:"❌ Wrong: No foreign key constraint",issues:["Can insert orders with non-existent customer_id","Orphaned records possible","Data integrity compromised"]},after:{code:"CREATE TABLE orders (\n  order_id INT PRIMARY KEY,\n  customer_id INT,\n  FOREIGN KEY (customer_id) \n    REFERENCES customers(customer_id)\n);",label:"✅ Right: Foreign key constraint enforced",improvements:["Database enforces valid customer_id","Cannot insert invalid references","Data integrity guaranteed"]},caption:"Always define foreign key constraints explicitly"}),(0,t.jsx)("h3",{className:"text-xl font-bold text-gray-900 mt-8 mb-4",children:"Mistake 2: Using Business Data as Primary Key"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Using email addresses, phone numbers, or social security numbers as primary keys seems convenient but causes problems when that data changes:"}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"❌ Wrong:"}),(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"CREATE TABLE customers (\n  email VARCHAR(255) PRIMARY KEY,  -- Bad idea!\n  name VARCHAR(100)\n);\n\n-- What happens when a customer changes their email?\n-- You have to update EVERY table that references it!"})]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("h4",{className:"font-bold text-gray-900 mb-3",children:"✅ Right:"}),(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"CREATE TABLE customers (\n  customer_id INT PRIMARY KEY AUTO_INCREMENT,\n  email VARCHAR(255) 
1UNIQUE,  -- Unique constraint, not primary key\n  name VARCHAR(100)\n);\n\n-- Email can change; customer_id stays constant forever."})]}),(0,t.jsx)("h3",{className:"text-xl font-bold text-gray-900 mt-8 mb-4",children:"Mistake 3: Not Indexing Foreign Key Columns"}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"Primary keys are automatically indexed, but foreign keys are not. This means JOINs on foreign keys can be slow on large tables:"}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg mb-6",children:[(0,t.jsx)("pre",{className:"bg-gray-800 text-green-400 p-4 rounded text-sm overflow-x-auto",children:"-- After creating your table with foreign key:\nCREATE INDEX idx_evidence_case_id ON evidence(case_id);\nCREATE INDEX idx_interviews_case_id ON interviews(case_id);\nCREATE INDEX idx_interviews_suspect_id ON interviews(suspect_id);"}),(0,t.jsx)("p",{className:"text-gray-600 text-sm mt-2",children:"Index foreign key columns for faster JOINs and constraint checking."})]}),(0,t.jsx)(r.M_,{variant:"tip",title:"Pro Tip",children:"Most database tools show you which columns are indexed. Check your foreign keys. If they're not indexed and you JOIN on them frequently, add indexes."}),(0,t.jsx)("h2",{id:"quiz",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"Test Your Understanding"}),(0,t.jsx)(r.Ri,{title:"\uD83D\uDD11 Primary Key vs Foreign Key Quiz",questions:[{question:"A primary key column can contain NULL values.",options:["True","False"],correctIndex:1,explanation:"Primary keys can NEVER be NULL. They must uniquely identify each row, and NULL is not a valid unique identifier."},{question:"How many foreign keys can a single table have?",options:["Only one","Zero or more","Exactly two","One per column"],correctIndex:1,explanation:"A table can have zero, one, or many foreign keys. Each foreign key creates a relationship to another table."},{question:"What happens with ON DELETE CASCADE when you delete a parent row?",options:["Nothing - the delete is blocked","The parent row is deleted, child rows remain","Both parent and all linked child rows are deleted","The foreign key is set to NULL"],correctIndex:2,explanation:"CASCADE means the action 'cascades' to child rows. Deleting a parent deletes all children that reference it."},{question:"Why should you create an index on foreign key columns?",options:["It's required by SQL","To prevent NULL values","To speed up JOINs and constraint checks","To allow duplicate values"],correctIndex:2,explanation:"Unlike primary keys, foreign keys are NOT automatically indexed. Adding an index speeds up JOINs and the constraint checking that happens on every INSERT/UPDATE."}]}),(0,t.jsx)("h2",{id:"faq",className:"text-3xl font-detective text-amber-900 mt-12 mb-6",children:"FAQ"}),(0,t.jsxs)("div",{className:"space-y-6",children:[(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg",children:[(0,t.jsx)("h3",{className:"font-bold text-gray-900 mb-2",children:"Can a foreign key also be a primary key?"}),(0,t.jsxs)("p",{className:"text-gray-700",children:["Yes! In junction tables for many-to-many relationships, the composite primary key often consists of two foreign keys. Example: a"," ",(0,t.jsx)("code",{children:"case_suspects"})," table with"," ",(0,t.jsx)("code",{children:"(case_id, suspect_id)"})," as the composite primary key, where both columns are also foreign keys."]})]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg",children:[(0,t.jsx)("h3",{className:"font-bold text-gray-900 mb-2",children:"Can a table have multiple primary keys?"}),(0,t.jsx)("p",{className:"text-gray-700",children:"No. A table can have only ONE primary key. However, that primary key can be a COMPOSITE key made of multiple columns. You might hear “multiple primary keys” but this is incorrect terminology."})]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg",children:[(0,t.jsx)("h3",{className:"font-bold text-gray-900 mb-2",children:"What is a composite key?"}),(0,t.jsxs)("p",{className:"text-gray-700",children:["A composite key is a primary key made of two or more columns. All columns together must be unique. Common in junction tables:"," ",(0,t.jsx)("code",{children:"PRIMARY KEY (order_id, product_id)"}),"."]})]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg",children:[(0,t.jsx)("h3",{className:"font-bold text-gray-900 mb-2",children:"Do foreign keys hurt database performance?"}),(0,t.jsx)("p",{className:"text-gray-700",children:"Foreign keys add overhead on INSERT, UPDATE, and DELET
1E because the database must check referential integrity. However, this overhead is usually small and the data integrity benefits outweigh the cost. Index your foreign key columns to minimize the impact."})]}),(0,t.jsxs)("div",{className:"bg-gray-50 p-6 rounded-lg",children:[(0,t.jsx)("h3",{className:"font-bold text-gray-900 mb-2",children:"Is a foreign key required?"}),(0,t.jsx)("p",{className:"text-gray-700",children:"No. Foreign keys are optional but strongly recommended. Without them, the database allows orphaned records (evidence linked to non-existent cases). You CAN skip them, but your data quality will suffer."})]})]}),(0,t.jsxs)("div",{className:"not-prose my-8 rounded-xl border border-amber-200/60 bg-amber-50/40 p-5",children:[(0,t.jsxs)("div",{className:"flex items-center gap-2 mb-2",children:[(0,t.jsx)("span",{className:"text-lg",children:"\uD83D\uDCAC"}),(0,t.jsx)("span",{className:"text-sm font-medium text-amber-800",children:"Common frustration from r/learnprogramming"})]}),(0,t.jsx)("blockquote",{className:"text-gray-700 text-sm leading-relaxed italic",children:"“I keep seeing these terms but nobody explains WHY you'd use them, just WHAT they are”"})]}),(0,t.jsx)("p",{className:"text-gray-700 leading-relaxed mb-6",children:"The WHY is simple: primary keys let you find specific rows instantly, and foreign keys let you connect related data without duplicating it. Without keys, you'd either have massive duplication or no way to link your data together."}),(0,t.jsxs)("div",{className:"not-prose my-10 p-8 bg-gradient-to-br from-amber-50 to-amber-100/80 border border-amber-200 rounded-xl text-center",children:[(0,t.jsx)("p",{className:"text-amber-900 font-detective text-xl mb-2",children:"Ready to practice with primary and foreign keys?"}),(0,t.jsx)("p",{className:"text-amber-700 mb-5 max-w-lg mx-auto",children:"SQLNoir's detective cases let you query across related tables to solve mysteries. No signup required for three starter cases."}),(0,t.jsx)(n.default,{href:"/cases",className:"inline-flex items-center gap-2 px-6 py-3 bg-amber-800/90 hover:bg-amber-700/90 text-amber-100 rounded-lg font-detective text-lg transition-colors",children:"Start Your Investigation →"})]})]})}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.