PHP MySQL Create Or Delete Table In Hindi

📔 : PHP 🔗

पिछले topic में आपने सीखा कि PHP में database create / delete करते हैं , इस topic में आप सीखेंगे कि किसी database के अंदर कैसे table create / delete करते हैं।


PHP में MySQL database से data fetch करने के लिए use किये जाने वाले function तो लगभग same ही रहते हैं , बस आपको MySQL की knowledge होनी चाहिए जिससे आप queries को अच्छे से लिख सकें।

PHP MySQL Create Table

Database के अंदर table create करने के लिए , सबसे पहले तो उस database को select करना होता है , Database को आप दो तरह से select कर सकते हैं -

  1. connect करते समय आप database का name दे सकते हैं।
  2. अगर connect करते समय database का name नहीं दिया है तो फिर mysqli_select_db(mysqli_link, db_name) function की help से select करना होता है।

इस बार database को connect करते समय ही Select करेंगे।

PHP MySQL Create Table Example

File : php_mysql_table.php

Copy Fullscreen Close Fullscreen
<?php
  $connection = mysqli_connect("localhost", "root", null, "db_test");
  if(! $connection)
    die("Database connection error : ".mysqli_connect_error());
    
  $create_table_query = "CREATE TABLE tbl_users (id INT AUTO_INCREMENT PRIMARY KEY, 
    first_name VARCHAR(30) NOT NULL,
    last_name VARCHAR(30) NOT NULL,
    email VARCHAR(50) NOT NULL,
    about_user VARCHAR(500) DEFAULT NULL, 
    create_date TIMESTAMP DEFAULT CURRENT_TIMESTAMP)";

  if(mysqli_query($connection, $create_table_query))
     echo "Table created.";
  else
     echo "Error : ".mysqli_error($connection);
?>
Output
Table created.

Explain :
MySQL में table create करने के लिए , उसके attributes / columns को उनके type के साथ declare करना पड़ता है।

  1. दिए गए example में define की गयी MySQL query में सबसे पहले table का nametbl_users दिया गया है।

  2. उसके बाद table का सबसे पहला column id लिया गया है , जिसका type int (integer) है , Auto Increment (means इसकी value automatically increase होती रहेगी , हमें insert नहीं करनी पड़ेगी ) है , इसके अलावा Primary key भी है जिससे की इसी field के bases पर बाकी operations(edit / delete) perform कर सकें।

  3. Table का दूसरा field first_name है जिसका type varchar है और value की maximum length 30 है , Not Null भी है means record insert करते समय इसमें value insert करनी ही पड़ेगी।

  4. ठीक first_name की तरह ही last_name & email fields भी हैं।

  5. Table का 5th column about_user है , जिसकी length 500 है , लेकिन यह by default null set है , means record insert करते समय इसके लिए value required नहीं है।

  6. और सबसे last में create_date field है , जिसका type timestamp (यह date & time को contain करता है )है, default CURRENT_TIMESTAMP है means इसकी भी value हमें insert नहीं करनी पड़ेगी , record insert होते समय automatically insert हो जायगी।


तो कुछ इस तरह से MySQL Table create करते हैं।

PHP MySQL Rename Table

Table को rename करने के लिए भी बाकी MySQL Queries की तरह ही query run करते हैं।

$query = "RENAME TABLE current_table_name TO new_table_name";
if(mysqli_query($connection, $query))
echo "Table renamed";
else
echo "Error : ".mysqli_error($connection);

PHP MySQL Delete / Drop Table

$query = "DROP TABLE table_name";
if(mysqli_query($connection, $query))
echo "Table dropped";
else
echo "Error : ".mysqli_error($connection);

I hope अब आपको समझ में आ गया होगा कि PHP में MySQL Table को किस तरह से drop करते है।

Related Topics :

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook

b2eprogrammers