Database.Beginner.Is primary key only about uniquness ?

When you define a Primary Key on a table:

  1. Uniqueness: ✅
    • No two rows can have the same value (or same combination, for composite keys).
  2. NOT NULL: ✅
    • No NULLs allowed in any column that is part of the Primary Key.
    • Every row must have a real, non-missing value for the Primary Key.
  3. Automatic Index: ✅
    • A unique index is automatically created behind the scenes.
    • This makes finding, updating, and deleting rows much faster.
  4. Data Integrity: ✅
    • Ensures your data has a reliable way to uniquely identify each row.
  5. Foreign Key Relationships: ✅
    • Other tables can create foreign keys pointing to this Primary Key.
    • This is how you build relationships between tables (like Users → Orders).

📝 In Short

FeaturePrimary Key Ensures
UniquenessNo duplicate key values.
Not NullNo NULL allowed in the key.
Fast LookupCreates a unique index for fast access.
Data IntegrityEach row is reliably identified.
RelationshipsOther tables can refer to it (foreign keys).

Example

CREATE TABLE Users (
    user_id INT PRIMARY KEY,
    name VARCHAR(100)
);

What happens now:

  • user_id must be unique.
  • user_id cannot be NULL.
  • A unique index on user_id is created automatically.
  • You can easily do:
This entry was posted in Без рубрики. Bookmark the permalink.