Skip to main content

Difference between Primary Key and Unique Key Vijay Sinha The Crazy Programmer

In this article we look into the difference between Primary Key and Unique Key Constraints. We will see the description of both along with how to define a schema and create attributes in a table with these constraints.

Primary Key

A Primary Key is a key attribute or a column which is used to uniquely identify a row (tuple) in a table (relation). It maintains the integrity constraints of a table. A table can have only one primary key attribute. The data of primary key must be unique. Hence, no duplicate values are valid. Also, no NULL (empty) values are allowed during insertion as it violates the integrity constraint. The database automatically creates a Clustered Index over primary key which orders data on the basis of primary key.

We define a Primary Key Attribute as follows:

Column_Name DataType PRIMARY KEY

Unique Key

A Unique Key is a constraint or key attribute which also uniquely identifies a row in a table. It’s purpose is somewhat same as primary key. It differs in ways such as a table can have one or more than one Unique keys. It allows the possibility of NULL value insertion limited to only one null value insertion. In other words, Unique Key Constraints allow a column to have only one NULL value. No duplicate values are permitted. By default, the Unique key are generated in a Non-Clustered Index.

We define a Unique Key Attribute as follows while creating a table:

Column_Name Datatype UNIQUE

Now let us look at an example of creating a table with these constraints:

CREATE TABLE CARS
(
Car_Id INT PRIMARY KEY,
Car_Brand VARCHAR(10),
Car_NumberPlate VARCHAR(10) UNIQUE
);

Here Car_Id is the Primary key and Car_NumberPlate is the Unique Key of table CARS.

Let us assume we have these records in table CARS:

If we add a record like this:

INSERT INTO CARS VALUES(3,'Hyundai','');

This will add the record successfully since the unique key Car_NumberPlate allows one null value and show contents of database:

If we add any duplicate value in Primary key field Car_Id, it will show us error

INSERT INTO CARS VALUES(3,'Honda','TN056789');

So, when we try entering duplicate values to our Primary key Car_Id it shows the Unique constraint of the field is violated.

Also Read: Difference between Primary Key and Foreign Key

Difference between Primary Key and Unique Key

Now let’s look at the key differences between them:

Primary Key Unique Key
1. It is the SQL constraint to uniquely identify rows of a table. 1. It is a unique identifier for rows when primary key is not present.
2. Primary Key does not allow NULL value insertion. 2. It allows one NULL value insertion.
3. Only one Primary Key can be present in a table. 3.There can be one or more than one Unique keys in a table.
4.  The main purpose is to enforce entity integrity. 4. The main purpose is to enforce uniqueness of entity data.
5. By default, the primary key column has Unique Clustered Index. 5. The unique key column has Non-Clustered Index.
6. It is useful in creating relationships between other tables. 6. It is useful for Data Validation.

That’s all for the article you can create a table like the example mentioned above, add some records and see it in working!

Feel free to ask your doubts in the comments section.

The post Difference between Primary Key and Unique Key appeared first on The Crazy Programmer.



from The Crazy Programmer https://ift.tt/2ZwG0KI

Comments

Popular posts from this blog

Difference between Web Designer and Web Developer Neeraj Mishra The Crazy Programmer

Have you ever wondered about the distinctions between web developers’ and web designers’ duties and obligations? You’re not alone! Many people have trouble distinguishing between these two. Although they collaborate to publish new websites on the internet, web developers and web designers play very different roles. To put these job possibilities into perspective, consider the construction of a house. To create a vision for the house, including the visual components, the space planning and layout, the materials, and the overall appearance and sense of the space, you need an architect. That said, to translate an idea into a building, you need construction professionals to take those architectural drawings and put them into practice. Image Source In a similar vein, web development and design work together to create websites. Let’s examine the major responsibilities and distinctions between web developers and web designers. Let’s get going, shall we? What Does a Web Designer Do?

A guide to data integration tools

CData Software is a leader in data access and connectivity solutions. It specializes in the development of data drivers and data access technologies for real-time access to online or on-premise applications, databases and web APIs. The company is focused on bringing data connectivity capabilities natively into tools organizations already use. It also features ETL/ELT solutions, enterprise connectors, and data visualization. Matillion ’s data transformation software empowers customers to extract data from a wide number of sources, load it into their chosen cloud data warehouse (CDW) and transform that data from its siloed source state, into analytics-ready insights – prepared for advanced analytics, machine learning, and artificial intelligence use cases. Only Matillion is purpose-built for Snowflake, Amazon Redshift, Google BigQuery, and Microsoft Azure, enabling businesses to achieve new levels of simplicity, speed, scale, and savings. Trusted by companies of all sizes to meet

2022: The year of hybrid work

Remote work was once considered a luxury to many, but in 2020, it became a necessity for a large portion of the workforce, as the scary and unknown COVID-19 virus sickened and even took the lives of so many people around the world.  Some workers were able to thrive in a remote setting, while others felt isolated and struggled to keep up a balance between their work and home lives. Last year saw the availability of life-saving vaccines, so companies were able to start having the conversation about what to do next. Should they keep everyone remote? Should they go back to working in the office full time? Or should they do something in between? Enter hybrid work, which offers a mix of the two. A Fall 2021 study conducted by Google revealed that over 75% of survey respondents expect hybrid work to become a standard practice within their organization within the next three years.  Thus, two years after the world abruptly shifted to widespread adoption of remote work, we are declaring 20