{"id":5284,"date":"2015-05-23T00:55:35","date_gmt":"2015-05-23T07:55:35","guid":{"rendered":"http:\/\/www.mysqltutorial.org\/?page_id=5284"},"modified":"2023-12-29T21:02:50","modified_gmt":"2023-12-30T04:02:50","slug":"mysql-check-constraint","status":"publish","type":"page","link":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/","title":{"rendered":"MySQL CHECK Constraint"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use MySQL <code>CHECK<\/code> constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.<\/p>\n\n\n\n<p class=\"note\">MySQL 8.0.16 implemented the SQL check constraint. If you use MySQL with the earlier versions, you can <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint-emulation\/\">emulate a <code>CHECK<\/code> constraint<\/a> using a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-views\/\">view<\/a> <code>WITH CHECK OPTION<\/code> or a <a href=\"https:\/\/www.mysqltutorial.org\/mysql-triggers\/\">trigger<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the MySQL CHECK&nbsp;constraint<\/h2>\n\n\n\n<p>Before MySQL 8.0.16, the <code><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">CREATE TABLE<\/a><\/code> allows you to include a table <code>CHECK<\/code> constraint. However, MySQL ignores all the <code>CHECK<\/code> constraints:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CHECK<\/span>(expression)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As of MySQL 8.0.16, the <code class=\"literal\"><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">CREATE TABLE<\/a><\/code> supported essential features of table and column <code>CHECK<\/code> constraints for all <a href=\"https:\/\/www.mysqltutorial.org\/mysql-administration\/mysql-storage-engines\/\">storage engines<\/a>.<\/p>\n\n\n\n<p>Here is the  basic syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CONSTRAINT constraint_name \n<span class=\"hljs-keyword\">CHECK<\/span> (expression) \n&#91;<span class=\"hljs-keyword\">ENFORCED<\/span> | <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">ENFORCED<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this syntax:<\/p>\n\n\n\n<p>First, specify the name for the check constraint that you want to create after the <code>CONSTRAINT<\/code> keyword. If you omit the constraint name, MySQL automatically generates a name with the following convention:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">table_name_chk_n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this convention, <code>n<\/code> is an ordinal number such as 1,2, and 3. For example, the automatically generated names of <code>CHECK<\/code> constraints of the <code>parts<\/code> table will be <code>parts_chk_1<\/code>, <code>parts_chk_2<\/code>, and so on.<\/p>\n\n\n\n<p>Second, specify a Boolean <code>expression<\/code> which must be evaluated to <code>TRUE<\/code> or <code>UNKNOWN<\/code> for each row of the table inside the parentheses after the <code>CHECK<\/code> keyword. <\/p>\n\n\n\n<p>If the expression evaluates to <code>FALSE<\/code>, the values violate the constraint or a constraint violation occurs.<\/p>\n\n\n\n<p class=\"note\">Note that MySQL treats 1 as true and 0 as false.<\/p>\n\n\n\n<p>Third, optionally specify the enforcement clause to indicate whether the check constraint is enforced:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>ENFORCED<\/code> or omit the <code>ENFORCED<\/code> clause to create and enforce the constraint.<\/li>\n\n\n\n<li>Use <code>NOT ENFORCED<\/code> to create the constraint but not enforce it.<\/li>\n<\/ul>\n\n\n\n<p>As mentioned earlier, you can define a <code>CHECK<\/code> constraint as a table constraint or column constraint.<\/p>\n\n\n\n<p>A table <code>CHECK<\/code> constraint can reference multiple columns whereas the column <code>CHECK<\/code> constraint can refer to the only column where it is defined.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">MySQL CHECK constraint examples<\/h2>\n\n\n\n<p>Let&#8217;s take some examples of using the <code>CHECK<\/code> constraints.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1) Creating CHECK constraints as column constraints<\/h3>\n\n\n\n<p>The following <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-create-table\/\">CREATE TABLE<\/a> statement creates a new table called <code>parts<\/code>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> parts (\n    part_no <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">18<\/span>) PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    description <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">40<\/span>),\n    <span class=\"hljs-keyword\">cost<\/span> <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span> ) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (<span class=\"hljs-keyword\">cost<\/span> &gt;= <span class=\"hljs-number\">0<\/span>),\n    price <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (price &gt;= <span class=\"hljs-number\">0<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The parts table has two column <code>CHECK<\/code> constraints: one for the <code>cost<\/code> column and the other for the <code>price<\/code> column.<\/p>\n\n\n\n<p>Because we did not explicitly specify the names of the <code>CHECK<\/code> constraints, MySQL automatically generated names for them.<\/p>\n\n\n\n<p>To view the table definition with the <code>CHECK<\/code> constraint name, you use the <code>SHOW CREATE TABLE<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> parts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"411\" height=\"140\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png\" alt=\"mysql check constraint\" class=\"wp-image-8053\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png 411w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint-200x68.png 200w\" sizes=\"auto, (max-width: 411px) 100vw, 411px\" \/><\/figure>\n\n\n\n<p>The output indicates that MySQL generated the names (<code>parts_chk_1<\/code> and <code>parts_chk_2<\/code>) for the check constraints.<\/p>\n\n\n\n<p>After creating <code>CHECK<\/code> constraints, if you <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert<\/a> or <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-update\/\">update<\/a> a value that causes the Boolean expression to be false, MySQL rejects the change and issues an error.<\/p>\n\n\n\n<p>This statement inserts a new row into the <code>parts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> parts(part_no, description,<span class=\"hljs-keyword\">cost<\/span>,price) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'A-001'<\/span>,<span class=\"hljs-string\">'Cooler'<\/span>,<span class=\"hljs-number\">0<\/span>,<span class=\"hljs-number\">-100<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>MySQL issued an error:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Error Code: 3819. <span class=\"hljs-keyword\">Check<\/span> <span class=\"hljs-keyword\">constraint<\/span> <span class=\"hljs-string\">'parts_chk_2'<\/span> <span class=\"hljs-keyword\">is<\/span> violated.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because the value of the <code>price<\/code> column is negative which causes the expression <code>price &gt; 0<\/code> evaluates to <code>FALSE<\/code> that results in a constraint violation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2) Creating CHECK constraints as a table constraints<\/h3>\n\n\n\n<p>First, drop the <code>parts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> parts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, create a new <code>parts<\/code> table with one more table <code>CHECK<\/code> constraint:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> parts (\n    part_no <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">18<\/span>) PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n    description <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">40<\/span>),\n    <span class=\"hljs-keyword\">cost<\/span> <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span> ) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (<span class=\"hljs-keyword\">cost<\/span> &gt;= <span class=\"hljs-number\">0<\/span>),\n    price <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">10<\/span>,<span class=\"hljs-number\">2<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (price &gt;= <span class=\"hljs-number\">0<\/span>),\n    <span class=\"hljs-keyword\">CONSTRAINT<\/span> parts_chk_price_gt_cost \n        <span class=\"hljs-keyword\">CHECK<\/span>(price &gt;= <span class=\"hljs-keyword\">cost<\/span>)\n);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The following new clause defines a table <code>CHECK<\/code> constraint that ensures the price is always greater than or equal to the cost:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">CONSTRAINT parts_chk_price_gt_cost <span class=\"hljs-keyword\">CHECK<\/span>(price &gt;= <span class=\"hljs-keyword\">cost<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Because we explicitly specify the name of the <code>CHECK<\/code> constraint, MySQL creates the new constraint with the specified name.<\/p>\n\n\n\n<p>Here is the definition of the <code>parts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">SHOW<\/span> <span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> parts;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<figure class=\"wp-block-image\"><img loading=\"lazy\" decoding=\"async\" width=\"463\" height=\"151\" src=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint-table-constraint-example.png\" alt=\"\" class=\"wp-image-8054\" srcset=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint-table-constraint-example.png 463w, https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint-table-constraint-example-200x65.png 200w\" sizes=\"auto, (max-width: 463px) 100vw, 463px\" \/><\/figure>\n\n\n\n<p>The table <code>CHECK<\/code> constraint appears at the end of the table definition after the column list.<\/p>\n\n\n\n<p>This statement attempts to <a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-insert\/\">insert<\/a> a new part whose price is less than the cost:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> parts(part_no, description,<span class=\"hljs-keyword\">cost<\/span>,price) \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'A-001'<\/span>,<span class=\"hljs-string\">'Cooler'<\/span>,<span class=\"hljs-number\">200<\/span>,<span class=\"hljs-number\">100<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the error due to the constraint violation:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\">Error Code: 3819. <span class=\"hljs-keyword\">Check<\/span> <span class=\"hljs-keyword\">constraint<\/span> <span class=\"hljs-string\">'parts_chk_price_gt_cost'<\/span> <span class=\"hljs-keyword\">is<\/span> violated.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Adding a check constraint to a table<\/h2>\n\n\n\n<p>To add a check constraint to an existing table, you use the <code>ALTER TABLE  ... ADD CHECK<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (expression);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you want to explicitly specify the name of the <code>CHECK<\/code> constraint, you can use the <code>ALTER TABLE ... ADD CONSTRAINT ... CHECK<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CONSTRAINT<\/span> contraint_name\n<span class=\"hljs-keyword\">CHECK<\/span> (expression);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/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 the <code>parts<\/code> table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> parts\n<span class=\"hljs-keyword\">ADD<\/span> <span class=\"hljs-keyword\">CHECK<\/span> (part_no &lt;&gt; description);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>This <code>CHECK<\/code> constraint prevents you from having the <code>part_no<\/code> identical to the <code>description<\/code>.<\/p>\n\n\n\n<p>For example, the following <code>INSERT<\/code> statement will be rejected:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span> parts \n<span class=\"hljs-keyword\">VALUES<\/span>(<span class=\"hljs-string\">'A'<\/span>,<span class=\"hljs-string\">'A'<\/span>,<span class=\"hljs-number\">100<\/span>,<span class=\"hljs-number\">120<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Output:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript\">ERROR <span class=\"hljs-number\">3819<\/span> (HY000): Check constraint <span class=\"hljs-string\">'parts_chk_3'<\/span> is violated.<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Removing a check constraint from a table<\/h2>\n\n\n\n<p>To remove a <code>CHECK<\/code> constraint from a table, you use the <code>ALTER TABLE ... DROP CHECK<\/code> statement:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CHECK<\/span> constraint_name;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>For example, the following statement removes the <code>CHECK<\/code> constraint <code>parts_chk_3<\/code> from the parts table:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"SQL (Structured Query Language)\" data-shcb-language-slug=\"sql\"><span><code class=\"hljs language-sql\"><span class=\"hljs-keyword\">ALTER<\/span> <span class=\"hljs-keyword\">TABLE<\/span> parts\n<span class=\"hljs-keyword\">DROP<\/span> <span class=\"hljs-keyword\">CHECK<\/span> parts_chk_3;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">SQL (Structured Query Language)<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">sql<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\">Summary<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <code>CHECK<\/code> constraints to ensure values stored in a column satisfy a Boolean condition.<\/li>\n\n\n\n<li>Use the <code>CHECK(expression)<\/code> to define a <code>CHECK<\/code> constraint.<\/li>\n\n\n\n<li>Use the <code>ALTER TABLE ... ADD CHECK<\/code> to add a <code>CHECK<\/code> constraint to a table.<\/li>\n\n\n\n<li>Use the <code>ALTER TABLE ... DROP CHECK<\/code> to remove a <code>CHECK<\/code> constraint from a table.<\/li>\n<\/ul>\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=\"5284\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/\"\n\t\t\t\tdata-post-title=\"MySQL 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=\"5284\"\n\t\t\t\tdata-post-url=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/\"\n\t\t\t\tdata-post-title=\"MySQL 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 will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.<\/p>\n","protected":false},"author":2,"featured_media":0,"parent":174,"menu_order":51,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-5284","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>MySQL CHECK Constraints<\/title>\n<meta name=\"description\" content=\"You will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.\" \/>\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.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"MySQL CHECK Constraints\" \/>\n<meta property=\"og:description\" content=\"You will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/\" \/>\n<meta property=\"og:site_name\" content=\"MySQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2023-12-30T04:02:50+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/\",\"name\":\"MySQL CHECK Constraints\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/mysql-check-constraint.png\",\"datePublished\":\"2015-05-23T07:55:35+00:00\",\"dateModified\":\"2023-12-30T04:02:50+00:00\",\"description\":\"You will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/mysql-check-constraint.png\",\"contentUrl\":\"https:\\\/\\\/www.mysqltutorial.org\\\/wp-content\\\/uploads\\\/2019\\\/08\\\/mysql-check-constraint.png\",\"width\":411,\"height\":140,\"caption\":\"mysql check constraint\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/mysql-check-constraint\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"MySQL Basics\",\"item\":\"https:\\\/\\\/www.mysqltutorial.org\\\/mysql-basics\\\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"MySQL CHECK Constraint\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.mysqltutorial.org\\\/#website\",\"url\":\"https:\\\/\\\/www.mysqltutorial.org\\\/\",\"name\":\"MySQL Tutorial\",\"description\":\"A comprehensive MySQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.mysqltutorial.org\\\/?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":"MySQL CHECK Constraints","description":"You will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.","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.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/","og_locale":"en_US","og_type":"article","og_title":"MySQL CHECK Constraints","og_description":"You will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.","og_url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/","og_site_name":"MySQL Tutorial","article_modified_time":"2023-12-30T04:02:50+00:00","og_image":[{"url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png","type":"","width":"","height":""}],"twitter_misc":{"Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/","url":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/","name":"MySQL CHECK Constraints","isPartOf":{"@id":"https:\/\/www.mysqltutorial.org\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/#primaryimage"},"image":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/#primaryimage"},"thumbnailUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png","datePublished":"2015-05-23T07:55:35+00:00","dateModified":"2023-12-30T04:02:50+00:00","description":"You will learn how to use MySQL CHECK constraint to ensure that values stored in a column or group of columns satisfy a Boolean expression.","breadcrumb":{"@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/#primaryimage","url":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png","contentUrl":"https:\/\/www.mysqltutorial.org\/wp-content\/uploads\/2019\/08\/mysql-check-constraint.png","width":411,"height":140,"caption":"mysql check constraint"},{"@type":"BreadcrumbList","@id":"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.mysqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"MySQL Basics","item":"https:\/\/www.mysqltutorial.org\/mysql-basics\/"},{"@type":"ListItem","position":3,"name":"MySQL CHECK Constraint"}]},{"@type":"WebSite","@id":"https:\/\/www.mysqltutorial.org\/#website","url":"https:\/\/www.mysqltutorial.org\/","name":"MySQL Tutorial","description":"A comprehensive MySQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.mysqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5284","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/comments?post=5284"}],"version-history":[{"count":5,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5284\/revisions"}],"predecessor-version":[{"id":13864,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/5284\/revisions\/13864"}],"up":[{"embeddable":true,"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/pages\/174"}],"wp:attachment":[{"href":"https:\/\/www.mysqltutorial.org\/wp-json\/wp\/v2\/media?parent=5284"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}