{"id":94,"date":"2026-03-06T08:11:58","date_gmt":"2026-03-06T08:11:58","guid":{"rendered":"https:\/\/gigz.pk\/sql\/?post_type=lesson&#038;p=94"},"modified":"2026-03-16T18:52:31","modified_gmt":"2026-03-16T18:52:31","slug":"constraints-pk-fk-unique-check","status":"publish","type":"lesson","link":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/","title":{"rendered":"\u00a0Constraints (PK, FK, UNIQUE, CHECK)"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">In relational databases, <strong>constraints<\/strong> are rules applied to table columns to enforce data integrity and consistency. They prevent invalid data entry and ensure relationships between tables are maintained. The most common constraints are <strong>Primary Key, Foreign Key, UNIQUE, and CHECK<\/strong>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">1. Primary Key (PK)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>Primary Key<\/strong> uniquely identifies each record in a table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Points<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Each table can have only one primary key.<\/li>\n\n\n\n<li>The primary key column(s) cannot contain NULL values.<\/li>\n\n\n\n<li>Commonly used on ID columns to ensure uniqueness.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><br>If we have a table called <code>Students<\/code> with columns <code>StudentID<\/code>, <code>Name<\/code>, and <code>Email<\/code>, we can set <code>StudentID<\/code> as the primary key. This ensures no two students share the same ID.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">2. Foreign Key (FK)<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">A <strong>Foreign Key<\/strong> establishes a relationship between two tables by linking a column in one table to the primary key of another table.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Points<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Ensures referential integrity between tables.<\/li>\n\n\n\n<li>Values in the foreign key column must exist in the referenced primary key column.<\/li>\n\n\n\n<li>Helps maintain consistent relationships, such as orders linked to customers.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><br>A table <code>Orders<\/code> has a column <code>CustomerID<\/code> that references <code>CustomerID<\/code> in the <code>Customers<\/code> table. This ensures each order is associated with a valid customer.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">3. UNIQUE Constraint<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>UNIQUE<\/strong> constraint ensures all values in a column are distinct across records.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Points<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Multiple UNIQUE constraints can exist in a table.<\/li>\n\n\n\n<li>Unlike the primary key, a UNIQUE column can allow NULL values (unless otherwise specified).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><br>In a <code>Users<\/code> table, the <code>Email<\/code> column can be set as UNIQUE to prevent multiple users from registering with the same email address.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">4. CHECK Constraint<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">The <strong>CHECK<\/strong> constraint ensures that all values in a column satisfy a specific condition.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Key Points<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can be applied to one or more columns.<\/li>\n\n\n\n<li>Useful for enforcing domain rules or business logic.<\/li>\n\n\n\n<li>Prevents insertion of invalid data.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong><br>In an <code>Employees<\/code> table, a CHECK constraint can ensure that the <code>Age<\/code> column only contains values greater than 18.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Constraints help maintain <strong>data accuracy, integrity, and consistency<\/strong> in relational databases.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Primary Key:<\/strong> Ensures each record is unique and not NULL.<\/li>\n\n\n\n<li><strong>Foreign Key:<\/strong> Maintains relationships between tables.<\/li>\n\n\n\n<li><strong>UNIQUE:<\/strong> Prevents duplicate values.<\/li>\n\n\n\n<li><strong>CHECK:<\/strong> Validates data against a condition.<\/li>\n<\/ul>\n\n\n<div class=\"yoast-breadcrumbs\"><span><span><a href=\"https:\/\/gigz.pk\/sql\/\">Home<\/a><\/span> \u00bb <span class=\"breadcrumb_last\" aria-current=\"page\">Intermediate SQL for Data Professionals (SQL-201) > Table Management > Constraints (PK, FK, UNIQUE, CHECK)<\/span><\/span><\/div>\n\n\n<div class=\"schema-faq wp-block-yoast-faq-block\"><div class=\"schema-faq-section\" id=\"faq-question-1773576054561\"><strong class=\"schema-faq-question\"><\/strong> <p class=\"schema-faq-answer\"><\/p> <\/div> <\/div>\n","protected":false},"menu_order":41,"template":"","class_list":["post-94","lesson","type-lesson","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.6 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>\u00a0Constraints (PK, FK, UNIQUE, CHECK) - SQL Learning Hub<\/title>\n<meta name=\"description\" content=\"&quot;Learn SQL constraints like Primary Key, Foreign Key, UNIQUE, and CHECK to ensure data integrity and consistency in databases.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"\u00a0Constraints (PK, FK, UNIQUE, CHECK) - SQL Learning Hub\" \/>\n<meta property=\"og:description\" content=\"&quot;Learn SQL constraints like Primary Key, Foreign Key, UNIQUE, and CHECK to ensure data integrity and consistency in databases.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Learning Hub\" \/>\n<meta property=\"article:modified_time\" content=\"2026-03-16T18:52:31+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":[\"WebPage\",\"FAQPage\"],\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/constraints-pk-fk-unique-check\\\/\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/constraints-pk-fk-unique-check\\\/\",\"name\":\"\u00a0Constraints (PK, FK, UNIQUE, CHECK) - SQL Learning Hub\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\"},\"datePublished\":\"2026-03-06T08:11:58+00:00\",\"dateModified\":\"2026-03-16T18:52:31+00:00\",\"description\":\"\\\"Learn SQL constraints like Primary Key, Foreign Key, UNIQUE, and CHECK to ensure data integrity and consistency in databases.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/constraints-pk-fk-unique-check\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/constraints-pk-fk-unique-check\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/lesson\\\/constraints-pk-fk-unique-check\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Intermediate SQL for Data Professionals (SQL-201) > Table Management > Constraints (PK, FK, UNIQUE, CHECK)\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/#website\",\"url\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/\",\"name\":\"SQL Learning Hub\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/gigz.pk\\\/sql\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"\u00a0Constraints (PK, FK, UNIQUE, CHECK) - SQL Learning Hub","description":"\"Learn SQL constraints like Primary Key, Foreign Key, UNIQUE, and CHECK to ensure data integrity and consistency in databases.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/","og_locale":"en_US","og_type":"article","og_title":"\u00a0Constraints (PK, FK, UNIQUE, CHECK) - SQL Learning Hub","og_description":"\"Learn SQL constraints like Primary Key, Foreign Key, UNIQUE, and CHECK to ensure data integrity and consistency in databases.","og_url":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/","og_site_name":"SQL Learning Hub","article_modified_time":"2026-03-16T18:52:31+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":["WebPage","FAQPage"],"@id":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/","url":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/","name":"\u00a0Constraints (PK, FK, UNIQUE, CHECK) - SQL Learning Hub","isPartOf":{"@id":"https:\/\/gigz.pk\/sql\/#website"},"datePublished":"2026-03-06T08:11:58+00:00","dateModified":"2026-03-16T18:52:31+00:00","description":"\"Learn SQL constraints like Primary Key, Foreign Key, UNIQUE, and CHECK to ensure data integrity and consistency in databases.","breadcrumb":{"@id":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/gigz.pk\/sql\/lesson\/constraints-pk-fk-unique-check\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/gigz.pk\/sql\/"},{"@type":"ListItem","position":2,"name":"Intermediate SQL for Data Professionals (SQL-201) > Table Management > Constraints (PK, FK, UNIQUE, CHECK)"}]},{"@type":"WebSite","@id":"https:\/\/gigz.pk\/sql\/#website","url":"https:\/\/gigz.pk\/sql\/","name":"SQL Learning Hub","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/gigz.pk\/sql\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson\/94","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/lesson"}],"about":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/types\/lesson"}],"wp:attachment":[{"href":"https:\/\/gigz.pk\/sql\/wp-json\/wp\/v2\/media?parent=94"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}