Free tutorials & notes in Hindi & English · Clean code examples · Mobile friendly learning
MySQL + SQL · Lesson 112

Normal Forms Quick Revision

Normalization क्यों करते हैं?

Normalization keys/dependencies से हर fact को सही relation में रखता है और update, insert, delete anomalies घटाता है। Decomposition lossless हो ताकि joins intended facts वापस बनाएँ; important dependencies preserve करना भी desirable है।

Memory: 1NF repeating structure, 2NF partial dependency, 3NF transitive dependency हटाता है; BCNF में हर determinant superkey होता है।

Normalization integrity improve करता है; indexes, transactions और security अलग concerns हैं।

Keys और Functional Dependencies

X → Y का अर्थ valid business rules में हर X exactly one Y determine करता है। Superkey row unique करता है; candidate key minimal superkey; primary key chosen candidate; prime attribute किसी candidate key का part; determinant dependency का left side है।

(order_id,product_id) → quantity,sale_price
product_id → product_name,current_price
order_id → order_date,customer_id
customer_id → customer_name,customer_city

ये rules बताते हैं facts कहाँ रखने हैं; केवल ID column नहीं।

First Normal Form (1NF)

1NF में each cell/domain value और no repeating groups—जैसे product1/product2 या comma-separated IDs नहीं।

CREATE TABLE order_items_1nf(
 order_id BIGINT NOT NULL,product_id BIGINT NOT NULL,
 quantity INT NOT NULL,sale_price DECIMAL(10,2) NOT NULL,
 PRIMARY KEY(order_id,product_id),CHECK(quantity > 0)
) ENGINE=InnoDB;

हर product occurrence अलग row है। Atomicity domain-dependent है; address display में one value, query/validation में split fields हो सकती हैं।

Second Normal Form (2NF)

2NF = 1NF और हर non-prime attribute पूरी candidate key पर dependent; composite key के केवल part पर नहीं।

order_line(order_id,product_id,product_name,current_price,quantity) में name/price केवल product_id पर हैं। Split:

products(product_id PK,product_name,current_price)
order_items(order_id PK/FK,product_id PK/FK,
            quantity,sale_price)

Sale price order-specific हो सकती है। केवल single-attribute candidate keys वाली 1NF relation automatically 2NF है।

Third Normal Form (3NF)

order_id → order_date,customer_id
customer_id → customer_name,customer_city

Customer facts order ID से customer ID के through transitively depend हैं। Split:

customers(customer_id PK,customer_name,customer_city)
orders(order_id PK,order_date,customer_id FK)

Formal 3NF: हर nontrivial X → A में X superkey या A prime हो। Historical shipping address intentionally order fact हो सकती है; business meaning dependency तय करता है।

Boyce–Codd Normal Form

BCNF में हर nontrivial dependency का determinant superkey होना चाहिए।

(student_id,subject) → teacher
teacher → subject

Candidate keys (student_id,subject) और (student_id,teacher) हैं। Subject prime होने से 3NF possible, लेकिन teacher superkey न होने से BCNF violation। Decompose:

teacher_subject(teacher PK,subject)
student_teacher(student_id,teacher,
 PRIMARY KEY(student_id,teacher))

Lossless join और dependency preservation check करें।

Complete Normalized MySQL Schema

CREATE TABLE customers(
 customer_id BIGINT PRIMARY KEY,customer_name VARCHAR(100) NOT NULL,
 customer_city VARCHAR(80) NOT NULL) ENGINE=InnoDB;
CREATE TABLE products(
 product_id BIGINT PRIMARY KEY,product_name VARCHAR(120) NOT NULL,
 current_price DECIMAL(10,2) NOT NULL) ENGINE=InnoDB;
CREATE TABLE orders(
 order_id BIGINT PRIMARY KEY,customer_id BIGINT NOT NULL,
 order_date DATE NOT NULL,
 FOREIGN KEY(customer_id) REFERENCES customers(customer_id)
) ENGINE=InnoDB;
CREATE TABLE order_items(
 order_id BIGINT NOT NULL,product_id BIGINT NOT NULL,
 quantity INT NOT NULL,sale_price DECIMAL(10,2) NOT NULL,
 PRIMARY KEY(order_id,product_id),
 FOREIGN KEY(order_id) REFERENCES orders(order_id),
 FOREIGN KEY(product_id) REFERENCES products(product_id),
 CHECK(quantity > 0)) ENGINE=InnoDB;

Composite key duplicate product line रोकती है; FKs relationships protect करते हैं। Secondary indexes measured queries से जोड़ें।

Design Review और Denormalization

  1. Entities/facts/rules लिखें।
  2. Candidate keys/dependencies list करें।
  3. 1NF repeating groups हटाएँ।
  4. 2NF partial dependencies हटाएँ।
  5. 3NF transitive dependencies हटाएँ।
  6. BCNF determinant test करें।
  7. Lossless/dependency preservation check करें।
  8. PK/UNIQUE/FK/NOT NULL/CHECK लगाएँ।
  9. Representative CRUD/concurrency test।
  10. Measured queries indexes से optimize करें।

Denormalize measured need पर ही: duplicate owner, refresh, transaction, reconciliation और recovery document करें। पहले view/cache/summary table consider करें। primary/foreign keys और indexes पढ़ें।

संदर्भ

Principles relational model और MySQL examples official manual से verified हैं।

अक्सर पूछे जाने वाले प्रश्न (FAQ)

Database normalization क्या है?
Functional dependencies के आधार पर relations organize करके redundancy और update anomalies घटाने की disciplined process है, जबकि required information preserve रहती है।
Single-column key में 2NF relevant है?
1NF relation में केवल single-attribute candidate keys हों तो key के part पर partial dependency संभव नहीं, इसलिए वह automatically 2NF है; 3NF issue फिर भी हो सकता है।
3NF और BCNF में क्या अंतर है?
हर nontrivial X to A dependency में BCNF, X को superkey माँगता है। 3NF A को prime attribute होने पर भी अनुमति देता है, इसलिए BCNF stricter है।
ID primary key जोड़ने से table normalize हो जाती है?
नहीं। Surrogate ID row identify करता है, business attributes की dependencies, repeating groups, partial या transitive dependencies नहीं हटाता।
क्या denormalization हमेशा गलत है?
नहीं। Correct normalized model के बाद measured reporting/performance design हो सकता है, यदि synchronization, ownership, consistency और recovery explicit हों।
🔗

Share this topic with a friend

यह topic किसी दोस्त को भेजें

Found it useful? Send it to a classmate learning the same thing.

अच्छा लगा? जो दोस्त यही सीख रहा है, उसे भेज दीजिए।

💻 लाइव कोड एडिटर

इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.