Table of contents
1.
Introduction
2.
SQL UNIQUE Constraint on CREATE TABLE
2.1.
SQL
3.
DROP a UNIQUE Constraint
4.
Frequently Asked Questions
4.1.
Can a table have more than one UNIQUE constraint?
4.2.
What happens if I try to add a duplicate value to a column with a UNIQUE constraint?
4.3.
Can I use UNIQUE constraint on multiple columns?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Unique Key in SQL

Author Sinki Kumari
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Unique keys in SQL are like secret codes that make sure every piece of information in a database is one-of-a-kind. Imagine you're in a room full of people where everyone must wear a unique badge. Just like those badges, unique keys help tell each piece of data apart from the others, ensuring no two pieces are exactly the same. 

Unique Key in SQL

In this article, we're going to explore how these special keys work & why they're so important in organizing & maintaining data efficiently. We'll walk through how to set them up when creating new tables, how to add them to existing ones, & even how to remove them if we need to. Let's get started & unlock the power of unique keys in SQL!

SQL UNIQUE Constraint on CREATE TABLE

When we create a new table in a database, it's like setting up a new room where each item needs its own special spot. The UNIQUE constraint in SQL is like telling the database, "Hey, make sure every item in this spot is different from the others!" This keeps our data tidy & easy to find.

Let's say we're making a list of all the students in a class, & we want to make sure each student's ID number is different so there's no mix-up. Here's how we can do it:

  • SQL

SQL

CREATE TABLE Students (

   StudentID int NOT NULL,

   FirstName varchar(255),

   LastName varchar(255),

   UNIQUE (StudentID)

);
output

In this example, StudentID is our unique key. This means no two students can have the same ID number. It's like giving each student a unique badge number. This way, when we look for a student in our list, we're sure to find just the one we're looking for, with no duplicates causing confusion.

DROP a UNIQUE Constraint

Sometimes in life, we change our minds. It's like deciding you don't want stickers on your notebook anymore. In the world of databases, we might decide a unique rule isn't needed for certain information. SQL makes it easy to remove, or "drop," this rule with a few simple steps.

Let's say we have a table for a club's members, and we initially thought each member's email should be unique. But later, we realize family members share an email, and we want to allow that. Here's how we can remove the unique constraint:

ALTER TABLE ClubMembers
DROP CONSTRAINT UniqueEmailConstraint;


In this command, UniqueEmailConstraint is the name we gave to our unique rule when we created it. By dropping it, we're telling the database, "It's okay, emails can be the same for some members." This makes our club more welcoming to families sharing an email address.

Frequently Asked Questions

Can a table have more than one UNIQUE constraint?

Yes, a table can have multiple unique constraints. It's like saying both your email & phone number must be different from everyone else's.

What happens if I try to add a duplicate value to a column with a UNIQUE constraint?

The database won't let you add it. It's like trying to enter a club with a fake ID that matches someone else's; you just won't get in.

Can I use UNIQUE constraint on multiple columns?

Absolutely! It's like saying a combination of your first & last name together must be unique in the whole school.

Conclusion

Unique keys in SQL are super important for keeping our data organized & making sure everything is as it should be, just like having a unique ID card in school. We learned how to set these keys up when we're making new tables & how to add them to tables that are already there. We also saw how to take these rules away if we decide we don't need them. Just like in real life, being flexible & able to adjust is key in managing data. Remember, unique keys help make sure every piece of information stands out on its own, keeping our data clean & easy to work with.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Live masterclass