ดัชนีคีย์เอกลักษณ์และคีย์หลัก
ทำความเข้าใจว่า PostgreSQL สร้างดัชนีรองรับคีย์หลักและข้อจำกัดด้านเอกลักษณ์โดยอัตโนมัติอย่างไร และเรียนรู้การใช้ดัชนีเอกลักษณ์เพื่อบังคับใช้ความถูกต้องครบถ้วนของข้อมูลอย่างมีประสิทธิภาพ
ดัชนีคีย์เอกลักษณ์และคีย์หลัก เป็นบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Constraints Need Indexes
To enforce uniqueness, PostgreSQL must check existing rows fast — so it auto-builds a unique index behind every unique constraint and primary key.
Primary Key Index
Declaring a primary key creates a unique B-tree index on those columns and marks them NOT NULL. The code shows it in action.
CREATE TABLE users (
id SERIAL PRIMARY KEY,
email TEXT
);The Hidden Index
That primary key silently builds an index called users_pkey. You never wrote CREATE INDEX, yet lookups by id are already fast.
Unique Constraints
A UNIQUE constraint also creates a supporting index and rejects duplicate values — the ALTER TABLE below adds one.
ALTER TABLE users ADD CONSTRAINT users_email_key UNIQUE (email);Standalone Unique Index
You can build a unique index directly, without a named constraint — it still enforces uniqueness, as the code shows.
CREATE UNIQUE INDEX idx_users_email ON users (email);Multi-Column Uniqueness
Index multiple columns together for multi-column uniqueness — like one membership per user per group. The code makes the pair unique.
CREATE UNIQUE INDEX idx_membership ON memberships (user_id, group_id);NULLs and Uniqueness
PostgreSQL treats NULLs as distinct by default, so a unique column allows many NULLs. Add NULLS NOT DISTINCT to forbid more than one.
CREATE UNIQUE INDEX idx_code ON items (code) NULLS NOT DISTINCT;Partial Unique Index
A partial unique index enforces uniqueness only on rows matching a WHERE clause — for example, one active session per user.
CREATE UNIQUE INDEX idx_active ON sessions (user_id) WHERE active;Dual Benefit
A unique index gives you two things at once: data integrity and fast lookups. The planner uses it like any other B-tree index.
Constraint vs Index
The constraint is the logical rule; the index enforces it. You can't drop the index without the constraint, and foreign keys can reference these columns.
Cost Awareness
Unique indexes add write overhead since every insert checks for duplicates. Index only the columns that genuinely need to be unique.
Quick Check
Primary keys, NULLs, partial indexes — let's see what stuck about unique indexes.
Recap
Recap: every primary key and unique constraint is backed by a unique B-tree index for integrity plus speed. You can also go standalone, multi-column, or partial — but each adds write cost.
คำถามที่พบบ่อย
บทเรียน “ดัชนีคีย์เอกลักษณ์และคีย์หลัก” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ดัชนีคีย์เอกลักษณ์และคีย์หลัก” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced PostgreSQL: Indexing, Partitioning, Replication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ดัชนีคีย์เอกลักษณ์และคีย์หลัก”
ทำความเข้าใจว่า PostgreSQL สร้างดัชนีรองรับคีย์หลักและข้อจำกัดด้านเอกลักษณ์โดยอัตโนมัติอย่างไร และเรียนรู้การใช้ดัชนีเอกลักษณ์เพื่อบังคับใช้ความถูกต้องครบถ้วนของข้อมูลอย่างมีประสิทธิภาพ คุณปฏิบัติ Advanced PostgreSQL: Indexing, Partitioning, Replication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced PostgreSQL: Indexing, Partitioning, Replication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ดัชนีคีย์เอกลักษณ์และคีย์หลัก” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication นี้ได้ไหม
ได้ บทเรียน Advanced PostgreSQL: Indexing, Partitioning, Replication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เหตุใดดัชนีจึงสำคัญ
- พื้นฐานดัชนี B-Tree
- การสร้างและลบดัชนี
- ดัชนีคีย์เอกลักษณ์และคีย์หลัก