Constraints & Keys in sql in hindi in hindi
Constraints & Keys in SQL
SEO Optimized Table of Contents for Constraints & Keys in SQL in Hindi
Constraints & Keys in SQL in Hindi
आज के समय में SQL हर college exam, competitive exam और practical database work का core हिस्सा है। जब भी हम database design करते हैं, तो सिर्फ table बनाना ही काफी नहीं होता, बल्कि data को सही, valid और reliable रखना भी जरूरी होता है। यहीं पर Constraints & Keys in SQL का role शुरू होता है।
इस part 1 में हम SQL Constraints और Keys को बिल्कुल basic level से समझेंगे, simple Hindi language में, ताकि exam में concept clear रहे और practical में confusion न हो।
SQL Constraints in Hindi
SQL Constraints ऐसे rules होते हैं जो table के column पर apply किए जाते हैं। इनका main purpose ये होता है कि database में गलत या duplicate data insert न हो सके।
Constraints data की quality बनाए रखते हैं और database को reliable बनाते हैं। अगर constraint violate होता है, तो SQL automatically error दे देता है।
NOT NULL Constraint in Hindi
NOT NULL Constraint का मतलब होता है कि किसी column में NULL value allow नहीं होगी। यानि उस column में value देना compulsory होता है।
Exam point of view से याद रखो — NOT NULL data completeness को ensure करता है। जैसे student table में student_name कभी empty नहीं होना चाहिए।
CREATE TABLE students (
student_id INT,
student_name VARCHAR(50) NOT NULL
);
ऊपर दिए example में student_name column कभी NULL नहीं हो सकता। अगर हम value नहीं देंगे तो insert query fail हो जाएगी।
UNIQUE Constraint in Hindi
UNIQUE Constraint ensure करता है कि column में duplicate values न हों। हर record की value अलग-अलग होनी चाहिए।
Email, mobile number जैसे fields के लिए UNIQUE Constraint बहुत useful होता है। Exam में इसे data uniqueness maintain करने के लिए use किया जाता है।
CREATE TABLE users (
user_id INT,
email VARCHAR(100) UNIQUE
);
इस example में एक ही email दो बार insert नहीं हो सकता। लेकिन UNIQUE column में NULL value allow हो सकती है।
PRIMARY KEY in Hindi
PRIMARY KEY सबसे important constraint होता है। यह NOT NULL + UNIQUE दोनों का combination होता है।
Primary Key table के हर record को uniquely identify करती है। Exam में अक्सर पूछा जाता है — table में एक ही Primary Key हो सकती है।
CREATE TABLE employees (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50)
);
यहाँ emp_id Primary Key है, इसलिए इसमें duplicate या NULL value नहीं आ सकती। Database internally Primary Key पर index भी बनाता है।
FOREIGN KEY in Hindi
FOREIGN KEY दो tables के बीच relationship create करती है। यह referential integrity maintain करने के लिए use होती है।
Foreign Key हमेशा किसी दूसरी table की Primary Key को refer करती है। इससे orphan records की problem नहीं आती।
CREATE TABLE orders (
order_id INT PRIMARY KEY,
customer_id INT,
FOREIGN KEY (customer_id) REFERENCES customers(customer_id)
);
इस example में customer_id तभी insert होगा जब customers table में मौजूद होगा। Exam में इसे parent-child relationship के रूप में explain किया जाता है।
CHECK Constraint in Hindi
CHECK Constraint value पर condition apply करता है। यानि value तभी insert होगी जब condition true होगी।
Age, salary, marks जैसे fields के लिए CHECK Constraint useful होता है। यह logical validation provide करता है।
CREATE TABLE students (
student_id INT,
age INT CHECK (age >= 18)
);
इस table में age 18 से कम insert नहीं हो सकती। अगर condition false होगी तो SQL error throw करेगा।
DEFAULT Constraint in Hindi
DEFAULT Constraint column के लिए default value set करता है। जब user value नहीं देता, तब default value automatically insert हो जाती है।
Status, country, created_date जैसे columns में इसका use common है। Exam में इसे automatic value assignment कहा जाता है।
CREATE TABLE accounts (
account_id INT,
status VARCHAR(20) DEFAULT 'Active'
);
अगर status नहीं दिया गया तो automatically 'Active' insert हो जाएगा। इससे extra validation की जरूरत नहीं पड़ती।
SQL Keys in Hindi
Keys SQL का conceptual part होती हैं। इनका use database design और normalization में ज्यादा होता है।
Keys data को uniquely identify करने और relationship define करने में help करती हैं। College exams में keys से related theory questions common होते हैं।
CANDIDATE KEY in Hindi
Candidate Key वो key होती है जो table के record को uniquely identify कर सकती है। Table में एक से ज्यादा candidate keys हो सकती हैं।
Example के तौर पर roll_number और email दोनों candidate keys हो सकती हैं। इनमें से किसी एक को Primary Key बनाया जाता है।
SUPER KEY in Hindi
Super Key attribute या attributes का वो set होता है जो record को uniquely identify करे। Candidate Key भी Super Key का ही part होती है।
Difference याद रखो — हर Candidate Key Super Key है, लेकिन हर Super Key Candidate Key नहीं होती।
COMPOSITE KEY in Hindi
Composite Key दो या दो से ज्यादा columns को मिलाकर बनती है। जब single column unique identification नहीं दे पाता, तब composite key use होती है।
Example: order_id + product_id मिलकर composite key बना सकते हैं। Exam में इसे combined primary key भी कहा जाता है।
ALTERNATE KEY in Hindi
Alternate Key वो candidate key होती है जिसे Primary Key नहीं चुना गया। लेकिन फिर भी वो uniquely identify करने की capability रखती है।
Example: अगर roll_number Primary Key है, तो email Alternate Key हो सकती है। Exam में इसे leftover candidate key भी कहा जाता है।
Constraints vs Keys in SQL in Hindi
अब तक हमने अलग-अलग Constraints और Keys को समझा। अब exam और practical दोनों के लिए यह समझना जरूरी है कि Constraints और Keys में actual difference क्या होता है।
Constraints mainly data validation के rules होते हैं, जबकि Keys data identification और relationship पर focus करती हैं। दोनों मिलकर database को strong और error-free बनाते हैं।
| Basis | Constraints | Keys |
|---|---|---|
| Main Purpose | Data validation और rules apply करना | Records को uniquely identify करना |
| Example | NOT NULL, UNIQUE, CHECK | Primary Key, Foreign Key |
| Exam Focus | Data correctness | Data relationship |
Exam में अक्सर comparison question आता है, इसलिए table format में याद रखना बहुत easy रहता है। अब हम Keys को थोड़ा deep level पर समझते हैं।
PRIMARY KEY Detailed Explanation in Hindi
Primary Key database design का backbone होती है। यह table के हर row को uniquely identify करती है।
Primary Key हमेशा NOT NULL और UNIQUE होती है। एक table में सिर्फ एक Primary Key allowed होती है, लेकिन वो composite हो सकती है।
CREATE TABLE marks (
student_id INT,
subject_id INT,
marks INT,
PRIMARY KEY (student_id, subject_id)
);
यहाँ student_id और subject_id मिलकर composite Primary Key बना रहे हैं। इसका मतलब same student same subject के लिए दो records नहीं हो सकते।
Exam में पूछ सकते हैं — Can Primary Key be NULL? Answer: No, Primary Key कभी NULL नहीं हो सकती।
FOREIGN KEY Detailed Explanation in Hindi
Foreign Key tables के बीच logical connection create करती है। यह ensure करती है कि child table का data parent table से match करे।
Foreign Key referential integrity maintain करती है। इसका मतलब invalid reference database में store नहीं हो सकता।
CREATE TABLE department (
dept_id INT PRIMARY KEY,
dept_name VARCHAR(50)
);
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
dept_id INT,
FOREIGN KEY (dept_id) REFERENCES department(dept_id)
);
अगर department table में dept_id मौजूद नहीं है, तो employee table में वही dept_id insert नहीं हो पाएगा।
Exam tip: Foreign Key NULL हो सकती है, लेकिन Primary Key NULL नहीं हो सकती।
Candidate Key vs Alternate Key in Hindi
Candidate Key वो key होती है जो uniquely identify कर सके। Table में एक से ज्यादा candidate keys हो सकती हैं।
Alternate Key वही candidate key होती है जिसे Primary Key नहीं चुना गया। Concept simple है, लेकिन exam में confusion होता है।
| Key Type | Meaning |
|---|---|
| Candidate Key | Possible keys जो Primary Key बन सकती हैं |
| Alternate Key | Candidate key जो Primary Key नहीं बनी |
Example: अगर student table में roll_number और email दोनों unique हैं, तो दोनों Candidate Keys हैं।
अगर roll_number को Primary Key बना दिया गया, तो email automatically Alternate Key बन जाएगी।
SUPER KEY Explained in Hindi
Super Key attributes का ऐसा set होता है जो record को uniquely identify करता है। इसमें extra attributes भी हो सकते हैं।
Candidate Key minimal super key होती है। यानी super key में unnecessary column नहीं होता।
Example: (student_id) → Super Key (student_id, student_name) → Super Key लेकिन Candidate Key सिर्फ (student_id) होगी।
Exam line याद रखो: Every Candidate Key is a Super Key, but every Super Key is not a Candidate Key.
COMPOSITE KEY Explained in Hindi
Composite Key तब use होती है जब single column unique identification provide नहीं करता। दो या दो से ज्यादा columns मिलकर uniqueness create करते हैं।
Real life example: Order table में order_id अलग-अलग customers के लिए same हो सकता है, लेकिन order_id + customer_id together unique होते हैं।
Composite Key mostly junction tables में use होती है। Exam में इसे many-to-many relationship से जोड़ा जाता है।
Role of Constraints in Data Integrity in Hindi
Constraints database को disciplined बनाते हैं। ये rules apply करके गलत data entry को रोकते हैं।
NOT NULL completeness ensure करता है, UNIQUE duplication रोकता है, CHECK logical condition enforce करता है।
DEFAULT constraint automatic value assign करके developer का काम आसान बनाता है। Foreign Key data consistency बनाए रखती है।
Exam Oriented Important Points
- Primary Key = NOT NULL + UNIQUE
- Foreign Key referential integrity maintain करती है
- Candidate Key possible Primary Key होती है
- Alternate Key unused Candidate Key होती है
- Composite Key multiple columns से बनती है
- CHECK constraint condition based validation करता है
College exams में short notes और difference questions इसी section से आते हैं। अगर concepts clear हैं, तो theory और practical दोनों easy हो जाते हैं।
इस तरह Constraints & Keys in SQL database को safe, structured और reliable बनाते हैं। इन concepts की strong understanding हर database student के लिए जरूरी है।
FAQs
SQL Constraints database में लगाए जाने वाले rules होते हैं, जो table के data को valid और correct बनाए रखते हैं। Constraints यह ensure करते हैं कि गलत, duplicate या incomplete data database में store न हो। NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK और DEFAULT ये सभी SQL Constraints in hindi समझे जाते हैं।
Primary Key table के हर record को uniquely identify करने के लिए use होती है। Primary Key कभी NULL नहीं हो सकती और इसमें duplicate values allowed नहीं होतीं। SQL में एक table में सिर्फ एक Primary Key होती है, लेकिन वह composite भी हो सकती है।
Foreign Key का use दो tables के बीच relationship बनाने के लिए किया जाता है। यह ensure करती है कि child table का data parent table से match करे। Foreign Key referential integrity maintain करती है और invalid data entry को रोकती है।
Candidate Key वो key होती है जो record को uniquely identify कर सकती है और Primary Key बन सकती है। Alternate Key वही Candidate Key होती है जिसे Primary Key नहीं चुना गया होता। Exam में इसे unused candidate key भी कहा जाता है।
Composite Key दो या दो से ज्यादा columns को मिलाकर बनाई जाती है। जब single column uniqueness provide नहीं करता, तब Composite Key का use किया जाता है। SQL में many-to-many relationship को handle करने में Composite Key बहुत useful होती है।
SQL Constraints data validation के rules होते हैं, जो गलत data को रोकते हैं। Keys records को uniquely identify करने और tables के बीच relationship बनाने में use होती हैं। Constraints data correctness पर focus करते हैं, जबकि Keys data identification पर focus करती हैं।