DEV Community

Abhay Singh Kathayat
Abhay Singh Kathayat

Posted on

Understanding Unique Keys in SQL: Ensuring Data Integrity

What is a Unique Key in SQL?

A Unique Key in SQL is a constraint that ensures all values in a column (or combination of columns) are distinct across rows within a table. This means no two rows can have the same value in the unique key column(s). It helps maintain data integrity by preventing duplicate entries.


Key Characteristics of a Unique Key

  1. Uniqueness:

    Each value in a unique key column must be distinct.

  2. Allows Null Values:

    Unlike primary keys, unique keys allow one or more NULL values, depending on the database system. However, these NULL values cannot violate the uniqueness rule (e.g., two rows cannot have the same non-null value in a unique key).

  3. Multiple Unique Keys:

    A table can have multiple unique keys, each ensuring the uniqueness of its respective columns.

  4. Index Creation:

    Unique keys automatically create a unique index in the database, which optimizes searches.


Syntax for Creating a Unique Key

While Creating a Table:

CREATE TABLE users (
    user_id INT PRIMARY KEY,
    email VARCHAR(255) UNIQUE,
    username VARCHAR(255) UNIQUE
);
Enter fullscreen mode Exit fullscreen mode
  • Here, email and username columns are unique keys, ensuring no two users have the same email or username.

Adding a Unique Key to an Existing Table:

ALTER TABLE users ADD CONSTRAINT unique_email UNIQUE (email);
Enter fullscreen mode Exit fullscreen mode

Examples

Inserting Data into a Table with Unique Keys:

INSERT INTO users (user_id, email, username) 
VALUES (1, 'user@example.com', 'user123');
Enter fullscreen mode Exit fullscreen mode
  • If you try to insert a duplicate value in the email or username columns:
INSERT INTO users (user_id, email, username) 
VALUES (2, 'user@example.com', 'user456');
Enter fullscreen mode Exit fullscreen mode
  • This query will fail because the email value already exists.

Difference Between Primary Key and Unique Key

Aspect Primary Key Unique Key
Uniqueness Ensures unique values. Ensures unique values.
Null Values Does not allow NULL values. Allows NULL values (varies by system).
Number in Table Only one primary key per table. Multiple unique keys per table.

Why Use a Unique Key?

  1. Prevent Duplicate Data:

    Ensures important fields, like emails or usernames, remain unique.

  2. Support Business Rules:

    Enforces data integrity by maintaining constraints specific to the application.

  3. Optimize Queries:

    The underlying unique index helps speed up data retrieval.


Practical Use Cases

  • User Authentication:

    Ensure unique emails or usernames for account creation.

  • Inventory Management:

    Prevent duplicate product IDs or barcodes.

  • Banking Systems:

    Enforce unique account numbers or card details.


Conclusion

A unique key is a vital SQL feature for maintaining data integrity and preventing duplicate entries in your database. It complements primary keys by allowing for multiple unique constraints in a table, making it a versatile tool in database design.

Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.

Top comments (0)