{"id":566,"date":"2016-04-15T07:26:58","date_gmt":"2016-04-15T07:26:58","guid":{"rendered":"https:\/\/sqltutorial.org\/?page_id=566"},"modified":"2025-02-08T01:15:31","modified_gmt":"2025-02-08T08:15:31","slug":"sql-check-constraint","status":"publish","type":"page","link":"https:\/\/www.sqltutorial.org\/sql-check-constraint\/","title":{"rendered":"SQL CHECK Constraint"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you will learn how to use the SQL <code>CHECK<\/code> constraint to ensure data in columns meet specific conditions.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-sql-check-constraint'>Introduction to SQL CHECK constraint <a href=\"#introduction-to-sql-check-constraint\" class=\"anchor\" id=\"introduction-to-sql-check-constraint\" title=\"Anchor for Introduction to SQL CHECK constraint\">#<\/a><\/h2>\n\n\n\n<p>In SQL, a check constraint ensures data in one or more columns within a table meet a specific condition. The <code>CHECK<\/code> constraints help you maintain data accuracy by limiting the value columns can store.<\/p>\n\n\n\n<p>Here&#8217;s the syntax for defining a <code>CHECK<\/code> constraint for a column:<\/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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name (\n    column1 data_type <span class=\"hljs-keyword\">CHECK<\/span>(boolean_expression)\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\">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, you specify the <code>CHECK<\/code> constraint in the column definition when creating the table. When you <a href=\"https:\/\/www.sqltutorial.org\/sql-insert\/\">insert<\/a> or <a href=\"https:\/\/www.sqltutorial.org\/sql-update\/\">update<\/a> values, they must pass the <code>boolean_expression<\/code>.<\/p>\n\n\n\n<p>When you define a <code>CHECK<\/code> constraint for a column, the <code>CHECK<\/code> constraint can check the values for that column only. In other words, the <code>boolean_expression<\/code> can only access the values of the column where you define the <code>CHECK<\/code> constraint.<\/p>\n\n\n\n<p>To define a <code>CHECK<\/code> constraint for multiple columns, you define it as a table constraint:<\/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\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> table_name (\n    column1 datatype <span class=\"hljs-keyword\">constraint<\/span>,\n    column2 datatype <span class=\"hljs-keyword\">constraint<\/span>,\n    <span class=\"hljs-keyword\">CHECK<\/span> (boolean_expression)\n);<\/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, the <code>boolean_expression<\/code> can access the values of all the columns of the table.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='defining-a-check-constraint-as-a-column-constraint'>Defining a CHECK constraint as a column constraint <a href=\"#defining-a-check-constraint-as-a-column-constraint\" class=\"anchor\" id=\"defining-a-check-constraint-as-a-column-constraint\" title=\"Anchor for Defining a CHECK constraint as a column constraint\">#<\/a><\/h2>\n\n\n\n<p>First, <a href=\"https:\/\/www.sqltutorial.org\/sql-create-table\/\">create a table<\/a> called <code>offers<\/code> to store job offers:<\/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\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> <span class=\"hljs-keyword\">IF<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-keyword\">EXISTS<\/span> offers (\n  offer_id <span class=\"hljs-built_in\">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> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  offer_date <span class=\"hljs-built_in\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  job_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  candidate <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">255<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  salary <span class=\"hljs-built_in\">DECIMAL<\/span>(<span class=\"hljs-number\">19<\/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> (salary &gt; <span class=\"hljs-number\">0<\/span>),\n  <span class=\"hljs-keyword\">FOREIGN<\/span> <span class=\"hljs-keyword\">KEY<\/span> (job_id) <span class=\"hljs-keyword\">REFERENCES<\/span> jobs (job_id) <span class=\"hljs-keyword\">ON<\/span> <span class=\"hljs-keyword\">DELETE<\/span> <span class=\"hljs-keyword\">CASCADE<\/span>\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><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIElGIE5PVCBFWElTVFMgb2ZmZXJzICggb2ZmZXJfaWQgSU5UIEdFTkVSQVRFRCBBTFdBWVMgQVMgSURFTlRJVFkgUFJJTUFSWSBLRVksIG9mZmVyX2RhdGUgREFURSBOT1QgTlVMTCwgam9iX2lkIElOVCBOT1QgTlVMTCwgY2FuZGlkYXRlIFZBUkNIQVIoMjU1KSBOT1QgTlVMTCwgc2FsYXJ5IERFQ0lNQUwoMTksIDIpIE5PVCBOVUxMIENIRUNLIChzYWxhcnkgPiAwKSwgRk9SRUlHTiBLRVkgKGpvYl9pZCkgUkVGRVJFTkNFUyBqb2JzIChqb2JfaWQpIE9OIERFTEVURSBDQVNDQURFICk7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this table, we define a <code>CHECK<\/code> constraint for the salary to ensure that it is always greater than zero:<\/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\">salary DECIMAL(19, 2) NOT NULL <span class=\"hljs-keyword\">CHECK<\/span> (salary &gt; <span class=\"hljs-number\">0<\/span>)<\/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>Second, attempt to insert a row into the <code>salary<\/code> column of the <code>offers<\/code> table with the value of zero:<\/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\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  offers (offer_date, job_id, candidate, salary)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'1994-05-07'<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'William Giet'<\/span>, <span class=\"hljs-number\">0<\/span>);<\/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><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=SU5TRVJUIElOVE8gb2ZmZXJzIChvZmZlcl9kYXRlLCBqb2JfaWQsIGNhbmRpZGF0ZSwgc2FsYXJ5KSBWQUxVRVMgKCcxOTk0LTA1LTA3JywgMSwgJ1dpbGxpYW0gR2lldCcsIDgzMDApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The database system will issue an error like this:<\/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\">ERROR:  new row for relation \"offers\" violates <span class=\"hljs-keyword\">check<\/span> <span class=\"hljs-keyword\">constraint<\/span> <span class=\"hljs-string\">\"offers_salary_check\"<\/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>The output indicates that the new row does not meet the constraint <code>offers_salary_check<\/code>.<\/p>\n\n\n\n<p class=\"note\">Note that the <code>offers_salary_check<\/code> is a generated constraint name by the database system. It may vary depending on the database system.<\/p>\n\n\n\n<p>Third, insert a row with a valid salary into the <code>offers<\/code> table:<\/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\"><span class=\"hljs-keyword\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  offers (offer_date, job_id, candidate, salary)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-string\">'1994-05-07'<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'William Giet'<\/span>, <span class=\"hljs-number\">8300<\/span>);<\/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><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=SU5TRVJUIElOVE8gb2ZmZXJzIChvZmZlcl9kYXRlLCBqb2JfaWQsIGNhbmRpZGF0ZSwgc2FsYXJ5KSBWQUxVRVMgKCcxOTk0LTA1LTA3JywgMSwgJ1dpbGxpYW0gR2lldCcsIDgzMDApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Finally, <a href=\"https:\/\/www.sqltutorial.org\/sql-select\/\">retrieve data<\/a> from the <code>offers<\/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\">SELECT<\/span>\n  offer_date,\n  job_id,\n  candidate,\n  salary\n<span class=\"hljs-keyword\">FROM<\/span>\n  offers;<\/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><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=U0VMRUNUIG9mZmVyX2RhdGUsIGpvYl9pZCwgY2FuZGlkYXRlLCBzYWxhcnkgRlJPTSBvZmZlcnM7\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>Output:<\/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\"> offer_date | job_id |  candidate   | salary\n<span class=\"hljs-comment\">------------+--------+--------------+---------<\/span>\n 1994-05-07 |      1 | William Giet | 8300.00<\/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<h2 class=\"wp-block-heading\" id='defining-a-check-constraint-as-a-table-constraint'>Defining a CHECK constraint as a table constraint <a href=\"#defining-a-check-constraint-as-a-table-constraint\" class=\"anchor\" id=\"defining-a-check-constraint-as-a-table-constraint\" title=\"Anchor for Defining a CHECK constraint as a table constraint\">#<\/a><\/h2>\n\n\n\n<p>First, create a table called <code>job_postings<\/code> to store the opening job postings:<\/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\"><span class=\"hljs-keyword\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> job_postings (\n  <span class=\"hljs-keyword\">id<\/span> <span class=\"hljs-built_in\">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> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  job_title <span class=\"hljs-built_in\">VARCHAR<\/span>(<span class=\"hljs-number\">100<\/span>) <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  description <span class=\"hljs-built_in\">TEXT<\/span>,\n  min_salary <span class=\"hljs-built_in\">DEC<\/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>,\n  max_salary <span class=\"hljs-built_in\">DEC<\/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>,\n  posting_date <span class=\"hljs-built_in\">DATE<\/span>,\n  closing_date <span class=\"hljs-built_in\">DATE<\/span>,\n  <span class=\"hljs-keyword\">CHECK<\/span> (\n    min_salary &gt; <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">AND<\/span> min_salary &lt;= max_salary\n  ),\n  <span class=\"hljs-keyword\">CHECK<\/span> (closing_date &gt;= posting_date)\n);<\/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>In this example, we define two <code>CHECK<\/code> constraints as table constraints, which include multiple multiple columns.<\/p>\n\n\n\n<p>The following <code>CHECK<\/code> constraint ensures that the minimum salary is&nbsp;greater than zero and less than or equal to the maximum salary:<\/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\">CHECK<\/span> (\n  min_salary &gt; <span class=\"hljs-number\">0<\/span>\n  <span class=\"hljs-keyword\">AND<\/span> min_salary &lt;= max_salary\n)<\/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<p>The following <code>CHECK<\/code> constraint ensures that the posting date is earlier than the closing date:<\/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\">CHECK<\/span> (closing_date &gt;= posting_date)<\/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<h2 class=\"wp-block-heading\" id='assigning-check-constraint-names'>Assigning CHECK constraint names <a href=\"#assigning-check-constraint-names\" class=\"anchor\" id=\"assigning-check-constraint-names\" title=\"Anchor for Assigning CHECK constraint names\">#<\/a><\/h2>\n\n\n\n<p>SQL allows you to assign an explicit name to a <code>CHECK<\/code> constraint via the <code>CONSTRAINT<\/code> clause:<\/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\">CONSTRAINT constraint_name \n<span class=\"hljs-keyword\">CHECK<\/span>(boolean_expression)<\/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<p>The constraint name will help you clarify the error message returned by the database system when the constraint violation occurs. <\/p>\n\n\n\n<p>For example:<\/p>\n\n\n\n<p>First, create a table called <code>hires<\/code> with a <code>CHECK<\/code> constraint:<\/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\">CREATE<\/span> <span class=\"hljs-keyword\">TABLE<\/span> hires (\n  hire_id <span class=\"hljs-built_in\">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> PRIMARY <span class=\"hljs-keyword\">KEY<\/span>,\n  offer_id <span class=\"hljs-built_in\">INT<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  hire_date <span class=\"hljs-built_in\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  start_date <span class=\"hljs-built_in\">DATE<\/span> <span class=\"hljs-keyword\">NOT<\/span> <span class=\"hljs-literal\">NULL<\/span>,\n  <span class=\"hljs-keyword\">CONSTRAINT<\/span> check_start_date <span class=\"hljs-keyword\">CHECK<\/span> (start_date &gt;= hire_date)\n);<\/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><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=Q1JFQVRFIFRBQkxFIGhpcmVzICggaGlyZV9pZCBJTlQgR0VORVJBVEVEIEFMV0FZUyBBUyBJREVOVElUWSBQUklNQVJZIEtFWSwgb2ZmZXJfaWQgSU5UIE5PVCBOVUxMLCBoaXJlX2RhdGUgREFURSBOT1QgTlVMTCwgc3RhcnRfZGF0ZSBEQVRFIE5PVCBOVUxMLCBDT05TVFJBSU5UIGNoZWNrX3N0YXJ0X2RhdGUgQ0hFQ0sgKHN0YXJ0X2RhdGUgPj0gaGlyZV9kYXRlKSApOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>In this example, we define a <code>CHECK<\/code> constraint called <code>check_start_date<\/code> to ensure that the start date must be on or after the hire date.<\/p>\n\n\n\n<p>Second, insert a new row into the hires table with the start date before the hire date:<\/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\">INSERT<\/span> <span class=\"hljs-keyword\">INTO<\/span>\n  hires (offer_id, hire_date, start_date)\n<span class=\"hljs-keyword\">VALUES<\/span>\n  (<span class=\"hljs-number\">1<\/span>, <span class=\"hljs-string\">'1994-06-07'<\/span>, <span class=\"hljs-string\">'1994-06-01'<\/span>);<\/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><a href=\"https:\/\/www.sqltutorial.org\/playground\/?q=SU5TRVJUIElOVE8gaGlyZXMgKG9mZmVyX2lkLCBoaXJlX2RhdGUsIHN0YXJ0X2RhdGUpIFZBTFVFUyAoMSwgJzE5OTQtMDYtMDcnLCAnMTk5NC0wNi0wMScpOw%3D%3D\" target=\"_blank\" rel=\"noreferrer noopener\">Try it<\/a><\/p>\n\n\n\n<p>The database system will issue the following error:<\/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\">ERROR:  new row for relation \"hires\" violates <span class=\"hljs-keyword\">check<\/span> <span class=\"hljs-keyword\">constraint<\/span> <span class=\"hljs-string\">\"check_start_date\"<\/span><\/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>The error message includes the <code>CHECK<\/code> constraint name <code>check_start_date<\/code> so that you can use to troubleshoot.<\/p>\n\n\n\n<p class=\"note\">Note that your database system may issue a slightly different error message.<\/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>Use a <code>CHECK<\/code> constraint to validate data based on a Boolean expression.<\/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<p><iframe loading=\"lazy\" class=\"iframe\" src=\"\/quiz\/?quiz=check\" name=\"quiz\" width=\"600\" height=\"700\"><\/iframe><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='databases'>Databases <a href=\"#databases\" class=\"anchor\" id=\"databases\" title=\"Anchor for Databases\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/www.pgtutorial.com\/postgresql-tutorial\/postgresql-check-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">PostgreSQL CHECK constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mysqltutorial.org\/mysql-basics\/mysql-check-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">MySQL CHECK constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.mariadbtutorial.com\/mariadb-basics\/mariadb-check-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">MariaDB CHECK constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.oracletutorial.com\/oracle-basics\/oracle-check-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">Oracle CHECK constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.db2tutorial.com\/db2-basics\/db2-check-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">Db2 CHECK constraint<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/www.sqlitetutorial.net\/sqlite-check-constraint\/\" target=\"_blank\" rel=\"noreferrer noopener\">SQLite CHECK constraint<\/a><\/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=\"566\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/\"\n\t\t\t\tdata-post-title=\"SQL 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=\"566\"\n\t\t\t\tdata-post-url=\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/\"\n\t\t\t\tdata-post-title=\"SQL 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 the SQL CHECK constraint to ensure data in columns meet specific conditions.<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":45,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-566","page","type-page","status-publish","hentry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.2 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>SQL CHECK Constraint<\/title>\n<meta name=\"description\" content=\"You will learn how to use the SQL CHECK constraint to validate data in a column or a set of columns based on 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.sqltutorial.org\/sql-check-constraint\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"SQL CHECK Constraint\" \/>\n<meta property=\"og:description\" content=\"You will learn how to use the SQL CHECK constraint to validate data in a column or a set of columns based on a Boolean expression.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL Tutorial\" \/>\n<meta property=\"article:modified_time\" content=\"2025-02-08T08:15: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=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/\",\"url\":\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/\",\"name\":\"SQL CHECK Constraint\",\"isPartOf\":{\"@id\":\"https:\/\/www.sqltutorial.org\/#website\"},\"datePublished\":\"2016-04-15T07:26:58+00:00\",\"dateModified\":\"2025-02-08T08:15:31+00:00\",\"description\":\"You will learn how to use the SQL CHECK constraint to validate data in a column or a set of columns based on a Boolean expression.\",\"breadcrumb\":{\"@id\":\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.sqltutorial.org\/sql-check-constraint\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.sqltutorial.org\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"SQL CHECK Constraint\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.sqltutorial.org\/#website\",\"url\":\"https:\/\/www.sqltutorial.org\/\",\"name\":\"SQL Tutorial\",\"description\":\"An Interactive SQL Tutorial\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.sqltutorial.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":"SQL CHECK Constraint","description":"You will learn how to use the SQL CHECK constraint to validate data in a column or a set of columns based on 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.sqltutorial.org\/sql-check-constraint\/","og_locale":"en_US","og_type":"article","og_title":"SQL CHECK Constraint","og_description":"You will learn how to use the SQL CHECK constraint to validate data in a column or a set of columns based on a Boolean expression.","og_url":"https:\/\/www.sqltutorial.org\/sql-check-constraint\/","og_site_name":"SQL Tutorial","article_modified_time":"2025-02-08T08:15:31+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.sqltutorial.org\/sql-check-constraint\/","url":"https:\/\/www.sqltutorial.org\/sql-check-constraint\/","name":"SQL CHECK Constraint","isPartOf":{"@id":"https:\/\/www.sqltutorial.org\/#website"},"datePublished":"2016-04-15T07:26:58+00:00","dateModified":"2025-02-08T08:15:31+00:00","description":"You will learn how to use the SQL CHECK constraint to validate data in a column or a set of columns based on a Boolean expression.","breadcrumb":{"@id":"https:\/\/www.sqltutorial.org\/sql-check-constraint\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.sqltutorial.org\/sql-check-constraint\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.sqltutorial.org\/sql-check-constraint\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.sqltutorial.org\/"},{"@type":"ListItem","position":2,"name":"SQL CHECK Constraint"}]},{"@type":"WebSite","@id":"https:\/\/www.sqltutorial.org\/#website","url":"https:\/\/www.sqltutorial.org\/","name":"SQL Tutorial","description":"An Interactive SQL Tutorial","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.sqltutorial.org\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/566","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/comments?post=566"}],"version-history":[{"count":0,"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/pages\/566\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.sqltutorial.org\/wp-json\/wp\/v2\/media?parent=566"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}