← Back to MySQL Course | Chapter 4: Constraints | Lesson 7 of 8

CHECK Constraint

CHECK एक rule वाले bouncer जैसा है, जैसे 'आपकी उम्र कम से कम 10 साल होनी चाहिए'। rule fail करने वाली कोई भी value वापस कर दी जाती है।
Syntax
sql
column_name datatype CHECK (condition)

CHECK को समझना

CHECK आपको एक boolean condition define करके किसी column में allowed values restrict करने देता है — उदाहरण के लिए, एक price column को हमेशा zero से बड़ा होने की माँग करना — जिसे MySQL हर insert या update पर evaluate करता है।

उदाहरण: Understanding CHECK

sql
CREATE TABLE products (price DECIMAL(10,2) CHECK (price > 0));

CHECK Constraints Violate करना

अगर एक insert या update एक CHECK condition violate करे, MySQL पूरे statement को reject कर देता है और एक error return करता है, आपके business rules तोड़ने वाली एक value को चुपचाप store करने के बजाय।

उदाहरण: Violating CHECK Constraints

sql
CREATE TABLE products (price DECIMAL(10,2) CHECK (price > 0));
INSERT INTO products VALUES (-5); -- rejected: violates CHECK

कई CHECK Constraints

आप same table से कई independent CHECK constraints attach कर सकते हैं, और हर एक को explicitly नाम देना violation होने पर resulting error messages को diagnose करना कहीं ज़्यादा आसान बनाता है।

उदाहरण: Multiple CHECK Constraints

sql
CREATE TABLE products (
  price DECIMAL(10,2),
  stock INT,
  CONSTRAINT chk_price CHECK (price > 0),
  CONSTRAINT chk_stock CHECK (stock >= 0)
);

मौजूदा Tables में CHECK Add करना

पहले से data वाली एक table में एक CHECK constraint add करना MySQL को हर मौजूदा row को नए rule के against validate करवाता है, और अगर कोई current row compliant नहीं है तो ALTER TABLE fail हो जाएगा।

उदाहरण: Adding CHECK to Existing Tables

sql
ALTER TABLE products ADD CONSTRAINT chk_price CHECK (price > 0);

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

CHECK Constraints हटाना

एक CHECK constraint हटाने के लिए constraint के specific name के साथ DROP CHECK clause चाहिए, क्योंकि एक table में कई checks active हो सकते हैं और MySQL को जानना ज़रूरी है कि कौन सा हटाना है।

उदाहरण: Dropping CHECK Constraints

sql
ALTER TABLE products DROP CHECK chk_price;

⚠️ This is MySQL-specific syntax. It cannot run in the browser editor. Practice this on your local MySQL installation.

Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. 8.0.16 से पहले MySQL versions पर CHECK इस्तेमाल करना, जहाँ constraint accept हो जाता है लेकिन कभी enforce नहीं होता।
  2. किसी दूसरी table में पहले से इसे तोड़ने वाली rows वाली एक table में CHECK add करना, जो fail हो जाता है।
  3. एक CHECK लिखना जो दूसरी table को refer करता है, जिसकी अनुमति नहीं है।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.