MySQL Insert Records In Hindi | MySQL Insert Multiple Records

📔 : MySQL 🔗

अभी तक आपने MySQL में table को manage करना सीखा कि table कैसे Create करते हैं , और table create करने के बाद columns को drop या add कैसे करते हैं।

इस topic में records को table में insert करना सीखेंगे।

MySQL Insert

Database के किसी भी table में records insert करते time पहले ये जानना जरूरी है कि उस table का structure क्या है , और table में कौन से columns mandatory हैं और कौन से fields optional हैं , और columns किस type की value accept कर रहे हैं।

क्योंकि column के type के according ही तो value insert होगी।

तो हमारे पास एक tutorials database है जिसमे users table में records को insert करेंगे। Insert करने से पहले एक बार table का structure देख लेते हैं।

DESCRIBE users;

Output

+----------+--------------+------+-----+---------+----------------+
| Field    | Type         | Null | Key | Default | Extra          |
+----------+--------------+------+-----+---------+----------------+
| id       | int(11)      | NO   | PRI | NULL    | auto_increment |
| name     | varchar(50)  | NO   |     | NULL    |                |
| email    | varchar(50)  | NO   |     | NULL    |                |
| mobileno | varchar(20)  | YES  |     | NULL    |                |
| address  | varchar(100) | YES  |     | NULL    |                |
+----------+--------------+------+-----+---------+----------------+
5 rows in set (0.071 sec)
  • तो table में कुल 5 columns हैं जिसमे id field auto increment है मतलब इसकी value हमें enter करने की जरूरत नहीं है।

  • name , email fields mandatory हैं तो record insert करते time इनकी value तो देनी ही पड़ेगी नहीं तो error आएगी।

  • हालाँकि mobileno और address fields optional हैं तो record insert करते time need के according value दे भी सकते हैं और नहीं भी।

MySQL Insert Example

Table में records insert करने के लिए INSERT statement का use किया जाता है।

INSERT INTO users(name, email, mobileno, address) VALUES ('User 1', 'user1@gmail.com', '3243907889', 'User 1 Address');

Output

Query OK, 1 row affected (0.111 sec)

Query OK होने पर record insert हो जायगा।

ध्यान रहे कि आपको वही values provide करनी हैं जो columns आपने insert करने के लिए select किये हैं। select किये गए columns की सख्या provide की जाने वाली values से match होनी चाहिए otherwise error आएगी।

See Records

table में record insert हुआ है या नहीं ये देखने के लिए आप SELECT statement का use कर सकते हैं।

SELECT * FROM users;

Output

+----+--------+-----------------+------------+----------------+
| id | name   | email           | mobileno   | address        |
+----+--------+-----------------+------------+----------------+
|  1 | User 1 | user1@gmail.com | 3243907889 | User 1 Address |
+----+--------+-----------------+------------+----------------+
1 row in set (0.039 sec)

MySQL insert into particular columns

हालाँकि need के according आप कुछ particular columns के लिए भी records insert कर सकते हैं।

INSERT INTO users(name, email, mobileno) VALUES ('User 2', 'user2@gmail.com', '9043907889');

OR

INSERT INTO users(name, email, address) VALUES ('User 3', 'user3@gmail.com', 'User 3 Address');

बस ध्यान रहे required fields की value तो देनी ही पड़ेगी , otherwise आपको error आ सकती है।

MySQL Insert Multiple Records

OK , तो अभी तक आप एक एक करके records को insert कर रहे थे , लेकिन आप एक साथ भी कई records को insert कर सकते हैं।

INSERT INTO users(name, email, mobileno, address) VALUES ('User 4', 'user4@gmail.com', '9100021321', 'User 4 Address') , ('User 5', 'user5@gmail.com', '9100021334', 'User 5 Address') , ('User 6', 'user6@gmail.com', '9100021326', 'User 6 Address');

Output

Query OK, 3 rows affected (0.003 sec)
Records: 3  Duplicates: 0  Warnings: 0

Output में आप देख सकते हैं कि 3 rows insert हो चुकी हैं। Multiple records को insert करते time भी आप किसी particular column को choose कर सकते हैं ।

Important : Record insert करते time आप MySQL के predefined functions जैसे CURRENT_DATE() , CURRENT_DATE() etc का use भी कर सकते हैं।

तो I Hope, आपको समझ आ गया होगा कि table में records को कैसे insert किया जाता है।

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