{"id":272,"date":"2024-11-23T07:08:40","date_gmt":"2024-11-23T00:08:40","guid":{"rendered":"https:\/\/www.pgtutorial.com\/?page_id=272"},"modified":"2025-01-22T09:35:37","modified_gmt":"2025-01-22T02:35:37","slug":"postgresql-check-constraint","status":"publish","type":"page","link":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/","title":{"rendered":"PostgreSQL CHECK Constraint"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn how to use PostgreSQL <code>CHECK<\/code> constraint to ensure values in table columns meet a condition.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-postgresql-check-constraint'>Introduction to PostgreSQL CHECK constraint <a href=\"#introduction-to-postgresql-check-constraint\" class=\"anchor\" id=\"introduction-to-postgresql-check-constraint\" title=\"Anchor for Introduction to PostgreSQL CHECK constraint\">#<\/a><\/h2>\n\n\n\n<p>PostgreSQL <code>CHECK<\/code> constraints maintain data integrity by ensuring that the value in a column must satisfy a Boolean expression.<\/p>\n\n\n\n<p>When a column has a <code><code>CHECK<\/code><\/code> constraint, and you attempt to <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert<\/a> or <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-update\/\">update<\/a> a value that causes the Boolean expression to be false, PostgreSQL issues a constraint violation and rejects the changes.<\/p>\n\n\n\n<p><code>CHECK<\/code> constraints can be helpful to enforce data integrity rules at the database level. They prevent invalid data from being inserted or update in table columns.<\/p>\n\n\n\n<p>For example, you can use the <code>CHECK<\/code> constraint to prevent negative values from being inserted into the <code>price<\/code> column of the <code>products<\/code> table.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the <code><code>CHECK<\/code><\/code> constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> (\n\u00a0 \u00a0\u00a0column1 data_type <span class=\"hljs-keyword\">CONSTRAINT<\/span> <span class=\"hljs-built_in\">constraint_name<\/span> <span class=\"hljs-keyword\">CHECK<\/span>(boolean_expression),\n\u00a0 \u00a0\u00a0...\n\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, add a <code>CHECK<\/code> constraint to the table column.<\/li>\n\n\n\n<li>Second, provide a constraint name after the <code>CONSTRAINT<\/code> keyword. This constraint name will show up in the error if a constraint violation error occurs. If you don&#8217;t provide the constraint name, PostgreSQL will implicitly generate one for the constraint.<\/li>\n\n\n\n<li>Third, place a Boolean expression inside parentheses <code>()<\/code> after the <code>CHECK<\/code> keyword to check values being inserted or updated in the column. The Boolean expression can reference columns of the same table, not other tables.<\/li>\n<\/ul>\n\n\n\n<p>Here&#8217;s the more concise syntax that does not use the constraint name:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> (\n   \u00a0column1 data_type <span class=\"hljs-keyword\">CHECK<\/span>(boolean_expression),\n\u00a0 \u00a0\u00a0...\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This <code>CHECK<\/code> constraint is a column constraint because it attaches to a particular column (<code>column1<\/code>).<\/p>\n\n\n\n<p>It&#8217;s worth noting that you can apply multiple constraints to the same column. For example, you can use both <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-not-null\/\">NOT NULL<\/a> and <code>CHECK<\/code> constraints for the same column:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> (\n\u00a0 \u00a0\u00a0column1 data_type <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">CHECK<\/span>(boolean_expression),\n\u00a0 \u00a0\u00a0...\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Alternatively, you can define the <code>CHECK<\/code> constraint as a table constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> (\n\u00a0 \u00a0\u00a0column1 data_type <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n\u00a0 \u00a0\u00a0...,\n\u00a0 \u00a0<span class=\"hljs-keyword\">CHECK<\/span>(boolean_expression)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax, you define the <code>CHECK<\/code> constraint after the column list. This syntax is handy when referencing multiple table columns in the Boolean expression.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='basic-postgresql-check-constraint-example'>Basic PostgreSQL CHECK constraint example <a href=\"#basic-postgresql-check-constraint-example\" class=\"anchor\" id=\"basic-postgresql-check-constraint-example\" title=\"Anchor for Basic PostgreSQL CHECK constraint example\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-create-table\/\">create a table<\/a> called <code>products<\/code> with a <code>CHECK<\/code> constraint to ensure that the price is greater than or equal to 0:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products (\n  product_id <span class=\"hljs-type\">INT<\/span> <span class=\"hljs-keyword\">GENERATED<\/span> <span class=\"hljs-keyword\">ALWAYS<\/span> <span class=\"hljs-keyword\">AS<\/span> <span class=\"hljs-keyword\">IDENTITY<\/span> <span class=\"hljs-keyword\">PRIMARY KEY<\/span>,\n  <span class=\"hljs-type\">name<\/span> <span class=\"hljs-type\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span>,\n  price <span class=\"hljs-type\">DEC<\/span>(<span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> positive_price <span class=\"hljs-keyword\">CHECK<\/span> (price &gt;= <span class=\"hljs-number\">0<\/span>),\n  discounted_price <span class=\"hljs-type\">DEC<\/span>(<span class=\"hljs-number\">11<\/span>, <span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">NULL<\/span> <span class=\"hljs-keyword\">DEFAULT<\/span> <span class=\"hljs-number\">0<\/span>\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, attempt to <a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-insert\/\">insert a row<\/a> with a negative price into the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  products (<span class=\"hljs-type\">name<\/span>, price)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'iPhone Pro 15'<\/span>, <span class=\"hljs-number\">-1299.99<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=SU5TRVJUIElOVE8gcHJvZHVjdHMgKG5hbWUsIHByaWNlKSBWQUxVRVMgKCdpUGhvbmUgUHJvIDE1JywgLTEyOTkuOTkpOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>PostgreSQL issued the following error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">new row for relation \"products\" violates check constraint \"positive_price\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, insert a new row with a valid price:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  products (<span class=\"hljs-type\">name<\/span>, price)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'iPhone Pro 15'<\/span>, <span class=\"hljs-number\">1299.99<\/span>) <span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=SU5TRVJUIElOVE8gcHJvZHVjdHMgKG5hbWUsIHByaWNlKSBWQUxVRVMgKCdpUGhvbmUgUHJvIDE1JywgMTI5OS45OSkgUkVUVVJOSU5HICo7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> product_id |     name      |  price  | discounted_price\n------------+---------------+---------+------------------\n          2 | iPhone Pro 15 | 1299.99 |             0.00<\/code><\/span><\/pre>\n\n\n<p>After that, update the price to an invalid one:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">UPDATE<\/span> products\n<span class=\"hljs-keyword\">SET<\/span>\n  price = <span class=\"hljs-number\">-899.99<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_id = <span class=\"hljs-number\">2<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=VVBEQVRFIHByb2R1Y3RzIFNFVCBwcmljZSA9IC04OTkuOTkgV0hFUkUgcHJvZHVjdF9pZCA9IDI7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>PostgreSQL issued the same check constraint violation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">new row for relation \"products\" violates check constraint \"positive_price\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, update the price to a valid one:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">UPDATE<\/span> products\n<span class=\"hljs-keyword\">SET<\/span>\n  price = <span class=\"hljs-number\">999.99<\/span>\n<span class=\"hljs-keyword\">WHERE<\/span>\n  product_id = <span class=\"hljs-number\">2<\/span> \n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=VVBEQVRFIHByb2R1Y3RzIFNFVCBwcmljZSA9IDk5OS45OSBXSEVSRSBwcm9kdWN0X2lkID0gMiBSRVRVUk5JTkcgKjs\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> product_id |     name      | price  | discounted_price\n------------+---------------+--------+------------------\n          2 | iPhone Pro 15 | 999.99 |             0.00<\/code><\/span><\/pre>\n\n\n<p>The output indicates that the statement updated the product price successfully.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='adding-check-constraints-to-tables'>Adding CHECK constraints to tables <a href=\"#adding-check-constraints-to-tables\" class=\"anchor\" id=\"adding-check-constraints-to-tables\" title=\"Anchor for Adding CHECK constraints to tables\">#<\/a><\/h2>\n\n\n\n<p>You use <code>ALTER TABLE ... ADD CONSTRAINT<\/code> statement to add a <code>CHECK<\/code> constraint to an existing table.<\/p>\n\n\n\n<p>Here&#8217;s the syntax of the statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span> \n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> <span class=\"hljs-built_in\">constraint_name<\/span> \n<span class=\"hljs-keyword\">CHECK<\/span> (boolean_expression)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement adds a <code>CHECK<\/code> constraint to check if the discounted price is less than price:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> discounted_price_check\n<span class=\"hljs-keyword\">CHECK<\/span> (discounted_price &lt; price <span class=\"hljs-keyword\">AND<\/span> discounted_price &gt; <span class=\"hljs-number\">0<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=QUxURVIgVEFCTEUgcHJvZHVjdHMgQUREIENPTlNUUkFJTlQgZGlzY291bnRlZF9wcmljZV9jaGVjayBDSEVDSyAoZGlzY291bnRlZF9wcmljZSA8IHByaWNlIEFORCBkaXNjb3VudGVkX3ByaWNlID4gMCk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>PostgreSQL issues an error because some row in the table violates the new <code>CHECK<\/code> constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">check constraint \"discounted_price_check\" of relation \"products\" is violated by some row<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The reason is that row id 2 has a discounted price of 0, which violates the new <code>CHECK<\/code> constraint.<\/p>\n\n\n\n<p>To fix this, you can update the discounted price of the existing rows to make it valid for the new <code>CHECK<\/code> constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">UPDATE<\/span> products\n<span class=\"hljs-keyword\">SET<\/span> discounted_price = price * <span class=\"hljs-number\">0.9<\/span>\n<span class=\"hljs-keyword\">RETURNING<\/span> *;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=VVBEQVRFIHByb2R1Y3RzIFNFVCBkaXNjb3VudGVkX3ByaWNlID0gcHJpY2UgKiAwLjkgUkVUVVJOSU5HICo7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs\"> product_id |     name      | price  | discounted_price\n------------+---------------+--------+------------------\n          2 | iPhone Pro 15 | 999.99 |           899.99<\/code><\/span><\/pre>\n\n\n<p>And then add the <code>CHECK<\/code> constraint to the <code>products<\/code> table again:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> discounted_price_check\n<span class=\"hljs-keyword\">CHECK<\/span> (discounted_price &lt; price <span class=\"hljs-keyword\">AND<\/span> discounted_price &gt; <span class=\"hljs-number\">0<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=QUxURVIgVEFCTEUgcHJvZHVjdHMgQUREIENPTlNUUkFJTlQgZGlzY291bnRlZF9wcmljZV9jaGVjayBDSEVDSyAoZGlzY291bnRlZF9wcmljZSA8IHByaWNlIEFORCBkaXNjb3VudGVkX3ByaWNlID4gMCk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>If you attempt to insert a row with the discounted price higher or equal to the price, PostgreSQL will issue a check violation. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> products(<span class=\"hljs-type\">name<\/span>, price, discounted_price)\n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'iPhone Pro 15'<\/span>, <span class=\"hljs-number\">1299.99<\/span>, <span class=\"hljs-number\">1399.99<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=SU5TRVJUIElOVE8gcHJvZHVjdHMgKG5hbWUsIHByaWNlLCBkaXNjb3VudGVkX3ByaWNlKSBWQUxVRVMgKCdpUGhvbmUgUHJvIDE1JywgMTI5OS45OSwgMTM5OS45OSk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"plaintext\" data-shcb-language-slug=\"plaintext\"><span><code class=\"hljs language-plaintext\">ERROR: new row for relation \"products\" violates check constraint \"discounted_price_check\"<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">plaintext<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">plaintext<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='removing-check-constraints-from-tables'>Removing CHECK constraints from tables <a href=\"#removing-check-constraints-from-tables\" class=\"anchor\" id=\"removing-check-constraints-from-tables\" title=\"Anchor for Removing CHECK constraints from tables\">#<\/a><\/h2>\n\n\n\n<p>To remove a <code>CHECK<\/code> constraint from a table, you use the <code>ALTER TABLE ... DROP CONSTRAINT<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-built_in\">table_name<\/span>\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> <span class=\"hljs-built_in\">constraint_name<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>First, specify the table name from which you want to remove the <code>CHECK<\/code> constraint.<\/li>\n\n\n\n<li>Second, provide the constraint name in the <code>DROP CONSTRAINT<\/code> clause to remove it.<\/li>\n\n\n\n<li>Third, use the optional <code>IF EXISTS<\/code> option to prevent an error from attempting to remove a non-existing <code>CHECK<\/code> constraint.<\/li>\n<\/ul>\n\n\n\n<p>For example, the following statement drops the <code><code>discounted_price<\/code><\/code> <code>CHECK<\/code> constraint from the <code>products<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"PostgreSQL SQL dialect and PL\/pgSQL\" data-shcb-language-slug=\"pgsql\"><span><code class=\"hljs language-pgsql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> products\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> discounted_price_check;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PostgreSQL SQL dialect and PL\/pgSQL<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">pgsql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p><a href=\"https:\/\/www.pgtutorial.com\/playground\/?db=check&amp;q=QUxURVIgVEFCTEUgcHJvZHVjdHMgRFJPUCBDT05TVFJBSU5UIElGIEVYSVNUUyBkaXNjb3VudGVkX3ByaWNlX2NoZWNrOw\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A <code>CHECK<\/code> constraint adds a validation logic to one or more columns in a table to ensure data integrity.<\/li>\n\n\n\n<li>Use the <code>CONSTRAINT ... CHECK<\/code> to define a <code><code>CHECK<\/code><\/code> constraint for a table.<\/li>\n\n\n\n<li>Use the <code>ALTER TABLE ... ADD CONSTRAINT<\/code> statement to add a <code>CHECK<\/code> constraint to a table.<\/li>\n\n\n\n<li>Use the <code>ALTER TABLE ... DROP CONSTRAINT<\/code> statement to remove a <code>CHECK<\/code> constraint from a table.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\" id='quiz'>Quiz <a href=\"#quiz\" class=\"anchor\" id=\"quiz\" title=\"Anchor for Quiz\">#<\/a><\/h2>\n\n\n\n<iframe loading=\"lazy\"\n  name=\"quiz\"\n  src=\"\/quiz\/?quiz=check\"\n  height=\"700\"\n  width=\"600\"\n  class=\"iframe\"\n><\/iframe>\n\n\n\n<p><\/p>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"272\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL CHECK Constraint\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"272\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/\"\n\t\t\t\tdata-post-title=\"PostgreSQL CHECK Constraint\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\n\t\t\t<button class=\"btn btn-primary wth-btn-submit\">Send<\/button>\n\t\t\t<button class=\"btn wth-btn-cancel\">Cancel<\/button>\n\t\t\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, you&#8217;ll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":13,"menu_order":15,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-272","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>PostgreSQL CHECK Constraint<\/title>\n<meta name=\"description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"PostgreSQL CHECK Constraint\" \/>\n<meta property=\"og:description\" content=\"In this tutorial, you&#039;ll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/\" \/>\n<meta property=\"og:site_name\" content=\"PostgreSQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-22T02:35:37+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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-check-constraint\\\/\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-check-constraint\\\/\",\"name\":\"PostgreSQL CHECK Constraint\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\"},\"datePublished\":\"2024-11-23T00:08:40+00:00\",\"dateModified\":\"2025-01-22T02:35:37+00:00\",\"description\":\"In this tutorial, you'll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-check-constraint\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-check-constraint\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/postgresql-tutorial\\\/postgresql-check-constraint\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"PostgreSQL Tutorial\",\"item\":\"https:\\\/\\\/www.pgtutorial.com\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"PostgreSQL CHECK Constraint\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.pgtutorial.com\\\/#website\",\"url\":\"https:\\\/\\\/www.pgtutorial.com\\\/\",\"name\":\"PostgreSQL Tutorial\",\"description\":\"Learn PostgreSQL from Scratch\",\"alternateName\":\"PostgreSQL\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.pgtutorial.com\\\/?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":"PostgreSQL CHECK Constraint","description":"In this tutorial, you'll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.","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:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/","og_locale":"en_US","og_type":"article","og_title":"PostgreSQL CHECK Constraint","og_description":"In this tutorial, you'll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.","og_url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/","og_site_name":"PostgreSQL Tutorial","article_modified_time":"2025-01-22T02:35:37+00:00","twitter_card":"summary_large_image","twitter_misc":{"Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/","url":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/","name":"PostgreSQL CHECK Constraint","isPartOf":{"@id":"https:\/\/www.pgtutorial.com\/#website"},"datePublished":"2024-11-23T00:08:40+00:00","dateModified":"2025-01-22T02:35:37+00:00","description":"In this tutorial, you'll learn how to use PostgreSQL CHECK constraint to ensure values in table columns meet a condition.","breadcrumb":{"@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":2,"name":"PostgreSQL Tutorial","item":"https:\/\/www.pgtutorial.com\/"},{"@type":"ListItem","position":3,"name":"PostgreSQL CHECK Constraint"}]},{"@type":"WebSite","@id":"https:\/\/www.pgtutorial.com\/#website","url":"https:\/\/www.pgtutorial.com\/","name":"PostgreSQL Tutorial","description":"Learn PostgreSQL from Scratch","alternateName":"PostgreSQL","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.pgtutorial.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/272","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/comments?post=272"}],"version-history":[{"count":17,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/272\/revisions"}],"predecessor-version":[{"id":1779,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/272\/revisions\/1779"}],"up":[{"embeddable":true,"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/pages\/13"}],"wp:attachment":[{"href":"https:\/\/www.pgtutorial.com\/wp-json\/wp\/v2\/media?parent=272"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}