MySQL Create Table
CREATE TABLE statement MySQL database के अंदर नए table बनाने के लिए उपयोग किया जाता है। एक table rows (records) और columns (fields) से मिलकर बना होता है। PHP application में data store करने के लिए सबसे पहले table structure बनाना अनिवार्य है। हर table में fields के data types, constraints और primary key properly define होना चाहिए ताकि database fast, secure और optimized रहे।
- Table में fields के नाम और उनके data types define किए जाते हैं।
- PRIMARY KEY और AUTO_INCREMENT के साथ unique ID बनाना recommended है।
- NOT NULL, DEFAULT, UNIQUE जैसे constraints data integrity बनाए रखते हैं।
- Foreign keys relations create करने के लिए उपयोगी हैं (advanced DB design)।
Basic Syntax (Create Table)
Important Notes (Beginners Must Read)
- INT numeric values के लिए उपयोग होता है (जैसे class, age)।
- VARCHAR variable-length text store करता है।
- AUTO_INCREMENT automatically new ID assign करता है।
- PRIMARY KEY हर record को uniquely identify करता है।
- NOT NULL field empty नहीं हो सकता।
- DEFAULT value तब use होती है जब कोई value provide नहीं की जाए।
- Large applications में proper indexing performance बढ़ाता है।
MySQLi Procedural – Create Table
PHP से mysqli_query() function का उपयोग करके आप table बना सकते हैं:
MySQLi Example – Table Already Exists Check
यदि table पहले से मौजूद हो तो error आएगा। इससे बचने के लिए IF NOT EXISTS उपयोग करें:
PDO – Create Table
PDO में table create करना बहुत आसान है और error handling भी आसान होती है:
Advanced Table Example (Constraints & Keys)
यह example advanced structure के साथ relation दिखाता है:
Common Mistakes (और समाधान)
- गलती: VARCHAR size बहुत छोटा रखना
Fix: कम-से-कम 50 या ज्यादा रखें (use-case पर depend)। - गलती: Primary key न लगाना
Fix: हर table में primary key होनी चाहिए। - गलती: Wrong data type चुनना
Fix: Numbers → INT, Text → VARCHAR, Date → DATE/TIMESTAMP - गलती: Table naming inconsistent
Fix: हमेशा plural names उपयोग करें (students, teachers, orders)।
Best Practices
- हर table में AUTO_INCREMENT primary key रखें।
- Columns के लिए logical data types चुनें।
- Large tables के लिए indexing बनाएं।
- Consistent naming convention (snake_case or camelCase) का उपयोग करें।
- Table create करने से पहले proper DB design करें।
Conclusion
CREATE TABLE SQL का एक critical हिस्सा है। एक well-structured table application को तेज़, scalable और reliable बनाता है। PHP में आप MySQLi और PDO दोनों के माध्यम से table create कर सकते हैं, IF NOT EXISTS के साथ safe create और constraints के साथ advanced design आसानी से implement कर सकते हैं।
