Normal Forms Quick Revision
Normalization क्यों करते हैं?
Normalization keys/dependencies से हर fact को सही relation में रखता है और update, insert, delete anomalies घटाता है। Decomposition lossless हो ताकि joins intended facts वापस बनाएँ; important dependencies preserve करना भी desirable है।
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_cityCustomer 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 → subjectCandidate 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
- Entities/facts/rules लिखें।
- Candidate keys/dependencies list करें।
- 1NF repeating groups हटाएँ।
- 2NF partial dependencies हटाएँ।
- 3NF transitive dependencies हटाएँ।
- BCNF determinant test करें।
- Lossless/dependency preservation check करें।
- PK/UNIQUE/FK/NOT NULL/CHECK लगाएँ।
- Representative CRUD/concurrency test।
- 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 हैं।