79377136

Date: 2025-01-22 09:28:05
Score: 0.5
Natty:
Report link

The difference between a primary key and a unique constraint in a database is as follows:

  1. Primary Key Purpose: Uniquely identifies each row in a table. Uniqueness: Ensures that no two rows have the same value for the primary key column(s). Null Values: Cannot contain null values. Count per Table: A table can have only one primary key. Default Index: Automatically creates a unique clustered index (in most databases).
  2. Unique Constraint Purpose: Ensures that all values in a column (or combination of columns) are unique. Uniqueness: Similar to a primary key, it ensures uniqueness but allows flexibility. Null Values: Can contain one or more null values (depends on the database). Count per Table: A table can have multiple unique constraints. Default Index: Automatically creates a unique non-clustered index.

CREATE TABLE Example ( id INT PRIMARY KEY, -- Primary key email VARCHAR(255) UNIQUE -- Unique constraint );

id is the primary key: It cannot be null, and each value must be unique. email has a unique constraint: Each value must be unique, but it can have null values.

Reasons:
  • Long answer (-1):
  • No code block (0.5):
  • Low reputation (1):
Posted by: Khushbu Joshi