{"id":132,"date":"2014-04-22T12:45:00","date_gmt":"2014-04-22T11:45:00","guid":{"rendered":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/22\/difference-between-primary-key-and-unique-key-in-sql\/"},"modified":"2014-04-22T12:45:00","modified_gmt":"2014-04-22T11:45:00","slug":"difference-between-primary-key-and-unique-key-in-sql","status":"publish","type":"post","link":"https:\/\/debuggersspace.com\/index.php\/2014\/04\/22\/difference-between-primary-key-and-unique-key-in-sql\/","title":{"rendered":"Difference between Primary key and Unique Key in Sql??"},"content":{"rendered":"<div class='booster-block booster-read-block'>\n                <div class=\"twp-read-time\">\n                \t<i class=\"booster-icon twp-clock\"><\/i> <span>Read Time:<\/span>41 Second                <\/div>\n\n            <\/div><div dir=\"ltr\" style=\"text-align: left;\">\n<div style=\"background-color: #eeeeee; box-sizing: border-box; color: #272727; display: table-row; font-family: 'Segoe UI', Arial, sans-serif; font-size: 14px; line-height: 24px; margin: 0px; text-align: justify; text-shadow: rgb(255, 255, 255) 1px 1px 0px; vertical-align: top; width: 615px;\">\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top; width: 300px;\">\n<span style=\"font-family: Times, Times New Roman, serif; font-size: small;\">Primary Key<\/span><\/div>\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif; font-size: small;\">Unique Key<\/span><\/div>\n<\/div>\n<div style=\"background-color: #f9f9f9; box-sizing: border-box; color: #161616; display: table-row; line-height: 24px; margin: 0px; text-align: justify; vertical-align: top; width: 615px;\">\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">Primary Key can&#8217;t accept null values.<\/span><\/div>\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">Unique key can accept only one null value.<\/span><\/div>\n<\/div>\n<div style=\"background-color: #f9f9f9; box-sizing: border-box; color: #161616; display: table-row; line-height: 24px; margin: 0px; text-align: justify; vertical-align: top; width: 615px;\">\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">By default, Primary key is clustered index <\/span><br \/>\n<span style=\"font-family: Times, Times New Roman, serif;\">and data in the database table is physically <\/span><br \/>\n<span style=\"font-family: Times, Times New Roman, serif;\">organized in the sequence of clustered index.<\/span><\/div>\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">By default, Unique key is a unique non-clustered index. &nbsp;<\/span><\/div>\n<\/div>\n<div style=\"background-color: #f9f9f9; box-sizing: border-box; color: #161616; display: table-row; line-height: 24px; margin: 0px; text-align: justify; vertical-align: top; width: 615px;\">\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">We can have only one Primary key in a table.<\/span><\/div>\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">We can have more than one unique key in a table. &nbsp; &nbsp; &nbsp; &nbsp;<\/span><\/div>\n<\/div>\n<div style=\"background-color: #f9f9f9; box-sizing: border-box; color: #161616; display: table-row; line-height: 24px; margin: 0px; text-align: justify; vertical-align: top; width: 615px;\">\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">Primary key can be made foreign key into &nbsp; &nbsp; &nbsp;another table.<\/span><\/div>\n<div style=\"border: 1px solid rgb(196, 196, 196); box-sizing: border-box; display: table-cell; margin: 0px; padding: 5px; text-align: left; vertical-align: top;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">In SQL Server, Unique key can be made foreign key into another table.<\/span><\/div>\n<\/div>\n<p><span style=\"font-family: Times, Times New Roman, serif;\"><b style=\"background-color: white; color: #222222; line-height: 18.479999542236328px;\">Example:<\/b><span style=\"background-color: white; color: #222222; line-height: 18.479999542236328px;\"><\/span><\/span><\/p>\n<div style=\"background-color: white; color: #222222; line-height: 18.479999542236328px; margin: 0in 0in 0.0001pt;\">\n<span style=\"font-family: Times, Times New Roman, serif;\"><span style=\"color: blue;\">Create<\/span>&nbsp;<span style=\"color: blue;\">table<\/span>&nbsp;Student<\/span><\/div>\n<div style=\"background-color: white; color: #222222; line-height: 18.479999542236328px; margin: 0in 0in 0.0001pt;\">\n<span style=\"color: grey; font-family: Times, Times New Roman, serif;\">(<\/span><\/div>\n<div style=\"background-color: white; color: #222222; line-height: 18.479999542236328px; margin: 0in 0in 0.0001pt;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">StuId&nbsp;<span style=\"color: blue;\">int<\/span>&nbsp;<span style=\"color: blue;\">primary<\/span>&nbsp;<span style=\"color: blue;\">key<\/span><span style=\"color: grey;\">,<\/span>&nbsp;<span style=\"color: green;\">&#8212;- Define Primary Key<\/span><\/span><\/div>\n<div style=\"background-color: white; color: #222222; line-height: 18.479999542236328px; margin: 0in 0in 0.0001pt;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">StuName&nbsp;<span style=\"color: blue;\">varchar<\/span><span style=\"color: grey;\">(<\/span>50<span style=\"color: grey;\">)<\/span>&nbsp;<span style=\"color: grey;\">Not<\/span>&nbsp;<span style=\"color: grey;\">Null,<\/span><\/span><\/div>\n<div style=\"background-color: white; color: #222222; line-height: 18.479999542236328px; margin: 0in 0in 0.0001pt;\">\n<span style=\"font-family: Times, Times New Roman, serif;\">ContactNo&nbsp;<span style=\"color: blue;\">int<\/span>&nbsp;<span style=\"color: blue;\">Unique<\/span>&nbsp;<span style=\"color: green;\">&#8212; Define Unique Key<\/span><\/span><\/div>\n<div style=\"background-color: white; color: #222222; line-height: 14.12399959564209px; margin: 0in 0in 8pt;\">\n<span style=\"color: grey; font-family: Times, Times New Roman, serif;\">)<\/span><\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Primary Key Unique Key Primary Key can&#8217;t accept null values. Unique key can accept only one null value. By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index. By default, Unique key is a unique non-clustered index. &nbsp; We can have only one [&hellip;]<\/p>\n","protected":false},"author":43,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_uag_custom_page_level_css":"","footnotes":""},"categories":[27],"tags":[],"class_list":["post-132","post","type-post","status-publish","format-standard","hentry","category-sql"],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false},"uagb_author_info":{"display_name":"Himanshu Namdeo","author_link":"https:\/\/debuggersspace.com\/author\/admin\/"},"uagb_comment_info":0,"uagb_excerpt":"Primary Key Unique Key Primary Key can&#8217;t accept null values. Unique key can accept only one null value. By default, Primary key is clustered index and data in the database table is physically organized in the sequence of clustered index. By default, Unique key is a unique non-clustered index. &nbsp; We can have only one&hellip;","_links":{"self":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/132"}],"collection":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/users\/43"}],"replies":[{"embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":0,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"wp:attachment":[{"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/debuggersspace.com\/index.php\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}