Home
Forums
New posts
Search forums
What's new
New posts
New resources
New profile posts
Latest activity
Resources
Latest reviews
Search resources
Members
Current visitors
New profile posts
Search profile posts
DMCA Policy
Log in
Register
What's new
Search
Search
Search titles only
By:
New posts
Search forums
Menu
Log in
Register
Install the app
Install
FEEL FREE TO SHARE TUTORIALS, YOUR SKILS & KNOWLEDGE ON CODING, SCRIPTS, THEMES, PLUGINS OR ANY RESOURCES YOU HAVE WITH THE COMMUNITY-
Click Here To Post Your Request,
JOIN COMPUTER REPAIR FORUM
Home
Forums
DATABASE CODING
SQL
Difference between Primary Key and Unique Key
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Code55" data-source="post: 25" data-attributes="member: 14"><p>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.</p><p></p><h2>Primary Key</h2><p></p><p>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 <strong>NULL</strong> (empty) values are allowed during insertion as it violates the integrity constraint. The database automatically creates a<strong> Clustered Index</strong> over primary key which orders data on the basis of primary key.</p><p></p><p>We define a Primary Key Attribute as follows:</p><p></p><p>Column_Name DataType PRIMARY KEY</p><p></p><p></p><h2>Unique Key</h2><p></p><p>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 <strong>Non-Clustered Index</strong>.</p><p></p><p>We define a Unique Key Attribute as follows while creating a table:</p><p></p><p>Column_Name Datatype UNIQUE</p><p></p><p>Now let us look at an example of creating a table with these constraints:</p><p></p><p>CREATE TABLE CARS</p><p>(</p><p>Car_Id INT PRIMARY KEY,</p><p>Car_Brand VARCHAR(10),</p><p>Car_NumberPlate VARCHAR(10) UNIQUE</p><p>);</p><p></p><p>Here <strong>Car_Id</strong> is the Primary key and <strong>Car_NumberPlate</strong> is the Unique Key of table CARS.</p><p></p><p>Let us assume we have these records in table CARS:</p><p></p><p><img src="https://www.thecrazyprogrammer.com/wp-content/uploads/2021/02/Screenshot-27.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>If we add a record like this:</p><p></p><p>INSERT INTO CARS VALUES(3,'Hyundai','');</p><p></p><p>This will add the record successfully since the unique key Car_NumberPlate allows one null value and show contents of database:</p><p></p><p><img src="https://www.thecrazyprogrammer.com/wp-content/uploads/2021/02/Screenshot-30.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>If we add any duplicate value in Primary key field Car_Id, it will show us error</p><p></p><p>INSERT INTO CARS VALUES(3,'Honda','TN056789');</p><p></p><p><img src="https://www.thecrazyprogrammer.com/wp-content/uploads/2021/02/Differencebetweenprimarykeyandunique.png" alt="" class="fr-fic fr-dii fr-draggable " style="" /></p><p></p><p>So, when we try entering duplicate values to our Primary key Car_Id it shows the Unique constraint of the field is violated.</p><p></p><p></p><h2>Difference between Primary Key and Unique Key</h2><p></p><p>Now let’s look at the key differences between them:</p><p></p><table style='width: 100%'><tr><td><p style="text-align: center"><strong>Primary Key</strong></p> </td><td><p style="text-align: center"><strong>Unique Key</strong></p> </td></tr><tr><td>1. It is the SQL constraint to uniquely identify rows of a table.</td><td>1. It is a unique identifier for rows when primary key is not present.</td></tr><tr><td>2. Primary Key does not allow NULL value insertion.</td><td>2. It allows one NULL value insertion.</td></tr><tr><td>3. Only one Primary Key can be present in a table.</td><td>3.There can be one or more than one Unique keys in a table.</td></tr><tr><td>4. The main purpose is to enforce entity integrity.</td><td>4. The main purpose is to enforce uniqueness of entity data.</td></tr><tr><td>5. By default, the primary key column has Unique Clustered Index.</td><td>5. The unique key column has Non-Clustered Index.</td></tr><tr><td>6. It is useful in creating relationships between other tables.</td><td>6. It is useful for Data Validation.</td></tr></table><p></p><p>That’s all for the article you can create a table like the example mentioned above, add some records and see it in working!</p><p></p><p>Feel free to ask your doubts in the comments section.</p></blockquote><p></p>
[QUOTE="Code55, post: 25, member: 14"] 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. [HEADING=1]Primary Key[/HEADING] 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 [B]NULL[/B] (empty) values are allowed during insertion as it violates the integrity constraint. The database automatically creates a[B] Clustered Index[/B] 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 [HEADING=1]Unique Key[/HEADING] 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 [B]Non-Clustered Index[/B]. 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 [B]Car_Id[/B] is the Primary key and [B]Car_NumberPlate[/B] is the Unique Key of table CARS. Let us assume we have these records in table CARS: [IMG]https://www.thecrazyprogrammer.com/wp-content/uploads/2021/02/Screenshot-27.png[/IMG] 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: [IMG]https://www.thecrazyprogrammer.com/wp-content/uploads/2021/02/Screenshot-30.png[/IMG] If we add any duplicate value in Primary key field Car_Id, it will show us error INSERT INTO CARS VALUES(3,'Honda','TN056789'); [IMG]https://www.thecrazyprogrammer.com/wp-content/uploads/2021/02/Differencebetweenprimarykeyandunique.png[/IMG] So, when we try entering duplicate values to our Primary key Car_Id it shows the Unique constraint of the field is violated. [HEADING=1]Difference between Primary Key and Unique Key[/HEADING] Now let’s look at the key differences between them: [TABLE] [TR] [TD][CENTER][B]Primary Key[/B][/CENTER][/TD] [TD][CENTER][B]Unique Key[/B][/CENTER][/TD] [/TR] [TR] [TD]1. It is the SQL constraint to uniquely identify rows of a table.[/TD] [TD]1. It is a unique identifier for rows when primary key is not present.[/TD] [/TR] [TR] [TD]2. Primary Key does not allow NULL value insertion.[/TD] [TD]2. It allows one NULL value insertion.[/TD] [/TR] [TR] [TD]3. Only one Primary Key can be present in a table.[/TD] [TD]3.There can be one or more than one Unique keys in a table.[/TD] [/TR] [TR] [TD]4. The main purpose is to enforce entity integrity.[/TD] [TD]4. The main purpose is to enforce uniqueness of entity data.[/TD] [/TR] [TR] [TD]5. By default, the primary key column has Unique Clustered Index.[/TD] [TD]5. The unique key column has Non-Clustered Index.[/TD] [/TR] [TR] [TD]6. It is useful in creating relationships between other tables.[/TD] [TD]6. It is useful for Data Validation.[/TD] [/TR] [/TABLE] 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. [/QUOTE]
Insert quotes…
Verification
Post reply
Richest Freecoded User
Most Freecoin
freecoded
4,876 Freecoin
J
Johnhendrick
645 Freecoin
S
Smith16
607 Freecoin
Davy200
590 Freecoin
nathan69
426 Freecoin
Laureine
415 Freecoin
A
anajeen
395 Freecoin
P
Peterparker87
331 Freecoin
C
codeguru
282 Freecoin
Tekera
267 Freecoin
Home
Forums
DATABASE CODING
SQL
Difference between Primary Key and Unique Key
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.
Accept
Learn more…
Top