{"id":166,"date":"2017-11-26T11:04:53","date_gmt":"2017-11-26T11:04:53","guid":{"rendered":"https:\/\/blinksql.com\/?p=166"},"modified":"2017-11-26T11:04:53","modified_gmt":"2017-11-26T11:04:53","slug":"constraints-in-postgresql","status":"publish","type":"post","link":"https:\/\/blinksql.com\/?p=166","title":{"rendered":"Constraints in Postgresql"},"content":{"rendered":"<p><strong>Constraints:<\/strong><br \/>\n-&gt;Constraints are the rules enforced on data columns on table.<br \/>\n-&gt; These are used to prevent invalid data from being entered into the database. This ensures the accuracy and reliability of the data in the database.<br \/>\n-&gt;Constraints could be column level or table level.<br \/>\n-&gt; Column level constraints are applied only to one column whereas table level constraints are applied to the whole table.<\/p>\n<p><strong>Primary Key:<br \/>\n<\/strong><br \/>\nA primary key is a column or a group of columns that is used to identify a row uniquely in a table.<br \/>\n<strong>Syntax:<\/strong><br \/>\nALTER TABLE TABLE_NAME ADD PRIMARY KEY (column_1, column_2);<br \/>\n<strong>Eg:<\/strong><br \/>\nAlter table Employee add primary key(emp_id);<\/p>\n<p>-&gt; If we want a column in a table as primary key and we want the field is auto-incremented by 1 then<br \/>\n<strong>Syntax:<br \/>\n<\/strong><br \/>\nALTER TABLE vendors ADD COLUMN ID SERIAL PRIMARY KEY;<\/p>\n<p><strong>Drop Primary Key:<br \/>\n<\/strong><br \/>\nALTER TABLE TABLE_NAME DROP CONSTRAINT primary_key_constraint_name;<\/p>\n<p>-&gt; To get the name of the primary key available in a table<br \/>\nselect constraint_name from information_schema.table_constraints where table_name = &#8216;attribute&#8217; and constraint_type = &#8216;PRIMARY KEY&#8217;;<br \/>\n-&gt; If we do not specify any name to the contraint by default the name will be tanle_name_pkey<\/p>\n<p><strong>Foreign Key:<br \/>\n<\/strong><br \/>\nA foreign key constraint specifies that the values in a column (or a group of columns) must match the values appearing in some row of another table.<br \/>\n<strong>Syntax:<\/strong><br \/>\nALTER TABLE manager ADD CONSTRAINT fkey_constraint FOREIGN KEY (user_id) REFERENCES users (uid) ON DELETE CASCADE;<br \/>\n-&gt; Here the user_id column of of manager table is referencing the uid column of users table.<br \/>\n-&gt; Before any modification in the uid column of the user table we must modify the user_id column of the manager table.<\/p>\n<p><strong><br \/>\nNOT NULL:<\/strong><br \/>\n<strong>Syntax:<\/strong><br \/>\nALTER TABLE table_name ALTER COLUMN column_name SET NOT NULL;<\/p>\n<p>Note: Before adding not null constraint to the alredy existing table make sure to update the existing data in the column to be not null.<br \/>\n<strong><br \/>\nDefault:<\/strong><br \/>\n-&gt; To set a value by default this constraint should be used.<br \/>\n<strong>Syntax:<br \/>\n<\/strong><br \/>\nAlter Table Table_name alter column_name set DEFAULT &#8216;TC&#8217;::character varying;<br \/>\nEg:<br \/>\nALTER TABLE ONLY service ALTER COLUMN service_type SET DEFAULT &#8216;DATA&#8217;;<\/p>\n<p><strong>Unique:<\/strong><br \/>\n-&gt; It is used to make sure that every row in the column must have an unique value.<br \/>\nSyntax:<br \/>\nALTER TABLE table_name<br \/>\nADD CONSTRAINT constraint_name UNIQUE (column1, column2, &#8230; column_n);<br \/>\nEg:<br \/>\nADD CONSTRAINT unique_key UNIQUE (name,type,description);<\/p>\n<p><strong>Drop Unique :<\/strong><br \/>\nALTER TABLE  attribute  drop CONSTRAINT unique_key;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Constraints: -&gt;Constraints are the rules enforced on data columns on table. -&gt; These are used to prevent invalid data from being entered into the database. This ensures the accuracy and&hellip;<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_eb_attr":""},"categories":[1],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Constraints in Postgresql - Blink SQL<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/blinksql.com\/?p=166\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Constraints in Postgresql - Blink SQL\" \/>\n<meta property=\"og:description\" content=\"Constraints: -&gt;Constraints are the rules enforced on data columns on table. -&gt; These are used to prevent invalid data from being entered into the database. This ensures the accuracy and&hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/blinksql.com\/?p=166\" \/>\n<meta property=\"og:site_name\" content=\"Blink SQL\" \/>\n<meta property=\"article:published_time\" content=\"2017-11-26T11:04:53+00:00\" \/>\n<meta name=\"author\" content=\"Hannie\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@harika_sanaka\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Hannie\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/blinksql.com\/?p=166#article\",\"isPartOf\":{\"@id\":\"https:\/\/blinksql.com\/?p=166\"},\"author\":{\"name\":\"Hannie\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4\"},\"headline\":\"Constraints in Postgresql\",\"datePublished\":\"2017-11-26T11:04:53+00:00\",\"dateModified\":\"2017-11-26T11:04:53+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/blinksql.com\/?p=166\"},\"wordCount\":434,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/blinksql.com\/#organization\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/blinksql.com\/?p=166#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/blinksql.com\/?p=166\",\"url\":\"https:\/\/blinksql.com\/?p=166\",\"name\":\"Constraints in Postgresql - Blink SQL\",\"isPartOf\":{\"@id\":\"https:\/\/blinksql.com\/#website\"},\"datePublished\":\"2017-11-26T11:04:53+00:00\",\"dateModified\":\"2017-11-26T11:04:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/blinksql.com\/?p=166#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/blinksql.com\/?p=166\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/blinksql.com\/?p=166#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/blinksql.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Constraints in Postgresql\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/blinksql.com\/#website\",\"url\":\"https:\/\/blinksql.com\/\",\"name\":\"Blink SQL\",\"description\":\" Thinking SQL\",\"publisher\":{\"@id\":\"https:\/\/blinksql.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/blinksql.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/blinksql.com\/#organization\",\"name\":\"Blink SQL\",\"url\":\"https:\/\/blinksql.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png\",\"contentUrl\":\"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png\",\"width\":280,\"height\":172,\"caption\":\"Blink SQL\"},\"image\":{\"@id\":\"https:\/\/blinksql.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4\",\"name\":\"Hannie\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/blinksql.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g\",\"caption\":\"Hannie\"},\"sameAs\":[\"http:\/\/blinksql.com\",\"https:\/\/twitter.com\/harika_sanaka\"],\"url\":\"https:\/\/blinksql.com\/?author=2\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Constraints in Postgresql - Blink SQL","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:\/\/blinksql.com\/?p=166","og_locale":"en_US","og_type":"article","og_title":"Constraints in Postgresql - Blink SQL","og_description":"Constraints: -&gt;Constraints are the rules enforced on data columns on table. -&gt; These are used to prevent invalid data from being entered into the database. This ensures the accuracy and&hellip;","og_url":"https:\/\/blinksql.com\/?p=166","og_site_name":"Blink SQL","article_published_time":"2017-11-26T11:04:53+00:00","author":"Hannie","twitter_card":"summary_large_image","twitter_creator":"@harika_sanaka","twitter_misc":{"Written by":"Hannie","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/blinksql.com\/?p=166#article","isPartOf":{"@id":"https:\/\/blinksql.com\/?p=166"},"author":{"name":"Hannie","@id":"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4"},"headline":"Constraints in Postgresql","datePublished":"2017-11-26T11:04:53+00:00","dateModified":"2017-11-26T11:04:53+00:00","mainEntityOfPage":{"@id":"https:\/\/blinksql.com\/?p=166"},"wordCount":434,"commentCount":0,"publisher":{"@id":"https:\/\/blinksql.com\/#organization"},"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/blinksql.com\/?p=166#respond"]}]},{"@type":"WebPage","@id":"https:\/\/blinksql.com\/?p=166","url":"https:\/\/blinksql.com\/?p=166","name":"Constraints in Postgresql - Blink SQL","isPartOf":{"@id":"https:\/\/blinksql.com\/#website"},"datePublished":"2017-11-26T11:04:53+00:00","dateModified":"2017-11-26T11:04:53+00:00","breadcrumb":{"@id":"https:\/\/blinksql.com\/?p=166#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/blinksql.com\/?p=166"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/blinksql.com\/?p=166#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/blinksql.com\/"},{"@type":"ListItem","position":2,"name":"Constraints in Postgresql"}]},{"@type":"WebSite","@id":"https:\/\/blinksql.com\/#website","url":"https:\/\/blinksql.com\/","name":"Blink SQL","description":" Thinking SQL","publisher":{"@id":"https:\/\/blinksql.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/blinksql.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/blinksql.com\/#organization","name":"Blink SQL","url":"https:\/\/blinksql.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blinksql.com\/#\/schema\/logo\/image\/","url":"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png","contentUrl":"https:\/\/blinksql.com\/wp-content\/uploads\/2022\/11\/cropped-BlinkSQL-removebg.png","width":280,"height":172,"caption":"Blink SQL"},"image":{"@id":"https:\/\/blinksql.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/blinksql.com\/#\/schema\/person\/f04b99304f59e14956e768e764d2afd4","name":"Hannie","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/blinksql.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c5876b3497d2c2eb98256b7c183220c1?s=96&d=mm&r=g","caption":"Hannie"},"sameAs":["http:\/\/blinksql.com","https:\/\/twitter.com\/harika_sanaka"],"url":"https:\/\/blinksql.com\/?author=2"}]}},"_links":{"self":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/posts\/166"}],"collection":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=166"}],"version-history":[{"count":0,"href":"https:\/\/blinksql.com\/index.php?rest_route=\/wp\/v2\/posts\/166\/revisions"}],"wp:attachment":[{"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=166"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=166"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blinksql.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=166"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}