Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

Financial Calculator

Calculate loans, SIP, PPF, NPS & more!

MySQL Create Table

PHP

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)

SQL: Basic CREATE TABLE
CREATE TABLE students ( id INT AUTO_INCREMENT PRIMARY KEY, name VARCHAR(50) NOT NULL, class INT NOT NULL, city VARCHAR(50), created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP)

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 Procedural (CREATE TABLE)
 <?php
   $conn = mysqli_connect("localhost", "root", "", "school");
   $sql = "CREATE TABLE students (id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(50) NOT NULL,
            class INT NOT NULL,
            city VARCHAR(50),
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )";

   if (mysqli_query($conn, $sql)) {
      echo "Table Created Successfully";
   } else {
      echo "Table Creation Error: " . mysqli_error($conn);
   }

   mysqli_close($conn);
 ?>

MySQLi Example – Table Already Exists Check

यदि table पहले से मौजूद हो तो error आएगा। इससे बचने के लिए IF NOT EXISTS उपयोग करें:

CREATE TABLE IF NOT EXISTS
 <?php
   $conn = mysqli_connect("localhost", "root", "", "school");
   $sql = "CREATE TABLE IF NOT EXISTS students (id INT AUTO_INCREMENT PRIMARY KEY,
            name VARCHAR(50) NOT NULL,
            class INT NOT NULL,
            city VARCHAR(50),
            created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP )";

   if (mysqli_query($conn, $sql)) {
      echo "Table Created Successfully";
   } else {
      echo "Table Creation Error: " . mysqli_error($conn);
   }

   mysqli_close($conn);
 ?>

PDO – Create Table

PDO में table create करना बहुत आसान है और error handling भी आसान होती है:

उदाहरण: PDO (CREATE TABLE)
 <?php
   $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
   $sql = "CREATE TABLE teachers (
               id INT AUTO_INCREMENT PRIMARY KEY,
               name VARCHAR(50) NOT NULL,
               subject VARCHAR(50) NOT NULL,
               city VARCHAR(50) )";

   $pdo->exec($sql);
   echo "Teachers Table Created!";
 ?>

Advanced Table Example (Constraints & Keys)

यह example advanced structure के साथ relation दिखाता है:

SQL: Advanced Table Structure
 <?php
   $pdo = new PDO("mysql:host=localhost;dbname=school", "root", "");
   $sql = "CREATE TABLE orders (
                  order_id INT AUTO_INCREMENT PRIMARY KEY,
                  user_id INT NOT NULL,
                  amount DECIMAL(10,2) NOT NULL,
                  status VARCHAR(20) DEFAULT 'pending',
                  created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
                  FOREIGN KEY (user_id) REFERENCES users(id) )";

   $pdo->exec($sql);
   echo "Teachers Table Created!";
 ?>

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 कर सकते हैं।

Article By: Brajlal Prasad
Created on: 04 Nov 2025  7  Views
 Print Article
Report Error

If you want to report an error, or any suggestion please send us an email to [email protected]

Financial Calculator

Financial Calculator

Take control of your finances with our powerful Financial Calculator app—calculate loans, SIP, PPF, NPS and more all in one place!

Play Store