PGoCareerGoCareer prep tools
Home
LoginSign up
  • Java
  • Python
  • AI
  • React
  • Angular
  • PHP
  • Node.js
  • SQL
  • DSA
  • HTML
  • CSS
  • JS
  • Spring
  • ML
  • MongoDB

SQL · Theory

Foreign Key

← All stacks

Theory

73/439

Foreign Key

Amit only reaches for Foreign Key when lookup by roll has to stay correct in a big table.

Foreign Key order — meaning → PRIMARY KEY(roll) + INDEX → one limit. Dumping ten features helps nobody.

Without Foreign Key, a big table gets messy and lookup by roll is hard to trust.

Use Foreign Key when lookup by roll must stay clear. If a simpler SQL step works, use that instead.

Foreign Key miss — indexing a low-cardinality flag only. Fix it before you talk about advanced SQL.

Place Foreign Key next to nearby SQL work — PRIMARY KEY(roll) + INDEX is the link.

One breath for Foreign Key, then lookup by roll, then indexing a low-cardinality flag only. Sit down.

Diagram
  students.id  ← PK (unique)
       ▲
       │ FK
  marks.student_id
Exam tip

Board: lookup by roll. Dry-run Foreign Key. Name the trap: indexing a low-cardinality flag only.

Foreign Key — sample query

CREATE TABLE authors (
  id INTEGER PRIMARY KEY,
  email TEXT UNIQUE NOT NULL
);
CREATE TABLE books (
  id INTEGER PRIMARY KEY,
  author_id INTEGER NOT NULL,
  title TEXT NOT NULL,
  FOREIGN KEY (author_id) REFERENCES authors(id)
);
INSERT INTO authors VALUES (1, 'a@x.com');
INSERT INTO books VALUES (1, 1, 'SQL Basics');
SELECT * FROM books;

Foreign Key — keys and constraints protect data integrity; indexes speed lookups.

Short notes

  • DefForeign Key — Amit uses it for lookup by roll in a big table.
  • RuleForeign Key → PRIMARY KEY(roll) + INDEX.
  • RememberForeign Key + an index (a big table).
  • UseForeign Key in a big table (lookup by roll).
  • TrapForeign Key — indexing a low-cardinality flag only.
  • ExForeign Key → lookup by roll.

Questions

1

Amit asks: why does Foreign Key exist? Use lookup by roll.

2

When would Amit actually reach for Foreign Key?

3

What trap does Amit hit with Foreign Key?

4

Dry-run PRIMARY KEY(roll) + INDEX and say the result.

Previous← Primary KeyNextComposite Key →
P

GoCareerGo

Utilities · Preparation Hub · Resume · CV · Tools — one workspace.

Workspace

DashboardProfilePreparation HubResume builderCV builderCareer planning

PDF Tools

Merge PDFSplit PDFCompress PDFImage to PDFAll toolsJobs

Image & QR

Compress ImageResize ImageQR ScannerQR GeneratorBlogIT interview prep

Company

FAQFeedbackContactPrivacyTermsSitemap

© 2026 GoCareerGo. Keep moving forward.