{"id":3989,"date":"2025-06-07T09:04:29","date_gmt":"2025-06-07T09:04:29","guid":{"rendered":"https:\/\/w3buddy.com\/?post_type=cposts&#038;p=3989"},"modified":"2025-06-07T09:04:30","modified_gmt":"2025-06-07T09:04:30","slug":"sql-check","status":"publish","type":"cposts","link":"https:\/\/w3buddy.com\/blog\/notes\/sql\/sql-check\/","title":{"rendered":"SQL CHECK"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">The <strong>CHECK<\/strong> constraint ensures that the values in a column meet a specific condition before being inserted or updated. It\u2019s used to enforce domain-level integrity.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Basic Syntax<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>-- Add CHECK during table creation\nCREATE TABLE employees (\n  employee_id INT PRIMARY KEY,\n  age INT,\n  salary DECIMAL(10,2),\n  CHECK (age >= 18 AND salary >= 10000)\n);\n\n-- Add CHECK to an existing table\nALTER TABLE employees\nADD CONSTRAINT chk_salary CHECK (salary >= 10000);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Example<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>CREATE TABLE students (\n  student_id INT PRIMARY KEY,\n  grade INT CHECK (grade BETWEEN 1 AND 12)\n);<\/code><\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u2705 Only allows grade values from 1 to 12.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udd39 <strong>Important Notes<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Can apply to a single column or multiple columns.<\/li>\n\n\n\n<li>Violations will throw an error during <code>INSERT<\/code> or <code>UPDATE<\/code>.<\/li>\n\n\n\n<li>Named constraints improve clarity and maintenance.<\/li>\n\n\n\n<li>Some DBMS (like older MySQL versions) may ignore CHECK by default unless in strict mode.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83e\udde0 <strong>Quick Recap<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-table has-small-font-size\"><table><thead><tr><th>Key Point<\/th><th>Explanation<\/th><\/tr><\/thead><tbody><tr><td>Purpose<\/td><td>Enforce rules\/conditions on column values<\/td><\/tr><tr><td>Scope<\/td><td>Single or multiple columns<\/td><\/tr><tr><td>When Enforced<\/td><td>On INSERT and UPDATE operations<\/td><\/tr><tr><td>Syntax Tip<\/td><td>Use <code>CHECK (condition)<\/code> inside or after table<\/td><\/tr><tr><td>DBMS Caveat<\/td><td>MySQL may ignore unless in strict mode<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\ud83d\udca1 Use <strong>CHECK<\/strong> constraints to make sure your data stays within expected boundaries and is logically valid.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The CHECK constraint ensures that the values in a column meet a specific condition before being inserted or updated. It\u2019s used to enforce domain-level integrity. \ud83d\udd39 Basic Syntax \ud83d\udd39 Example \u2705 Only allows grade values from 1 to 12. \ud83d\udd39 Important Notes \ud83e\udde0 Quick Recap Key Point Explanation Purpose Enforce rules\/conditions on column values Scope [&hellip;]<\/p>\n","protected":false},"template":"","meta":{"googlesitekit_rrm_CAowu461DA:productID":""},"categories":[952,979],"class_list":["post-3989","cposts","type-cposts","status-publish","hentry","category-notes","category-sql"],"_links":{"self":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts\/3989","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/cposts"}],"about":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/types\/cposts"}],"wp:attachment":[{"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/media?parent=3989"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/w3buddy.com\/blog\/wp-json\/wp\/v2\/categories?post=3989"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}