How to insert data into MySQL Table?

Ads
 

How to insert data into MySQL Table?

Hi,

How to insert the data into MySQL Database? Please explain me with the examples and videos thanks.

Thanks

View Answers

June 26, 2016 at 2:16 AM

Hi,

The insert into query is used to insert the data into MySQL table. Suppose we have a table:

CREATE TABLE email
(
    first_name varchar(20),
    last_name varchar(20),
    email varchar(40),
    email_count Int
)

Then we can use the following query to insert the data:

insert into email(first_name,last_name,email,email_count)
values('Deepak','Kumar','[email protected]',100);

Thanks


June 26, 2016 at 2:20 AM

There are three ways you can run the insert query:

  1. With all fields in the query:

    insert into email(firstname,lastname,email,email_count) values('Deepak','Kumar','[email protected]',100);

  2. Without fields in the insert query. Here values for all the columns is to be provided:

    insert into email values('Rajeev','Singh','[email protected]',500);

    insert into email values('Dinesh','Singh','[email protected]',200);

    insert into email values('John','T','[email protected]',400);

  3. Insert values in selective columns:

    insert into email(firstname,lastname,email_count) values('Ishu','roy',800) ;

Thanks


June 26, 2016 at 2:21 AM

Hi,

Here is the queries in correct format:

insert into email(first_name,last_name,email,email_count)
values('Deepak','Kumar','[email protected]',100);

insert into email
values('Rajeev','Singh','[email protected]',500);


insert into email
values('Dinesh','Singh','[email protected]',200);


insert into email
values('John','T','[email protected]',400);

insert into email(first_name,last_name,email_count)
values('Ishu','roy',800) ;

Thaks


June 26, 2016 at 2:22 AM

Hi,

Full output of MySQL client tool:

mysql> describe email;
+-------------+-------------+------+-----+---------+-------+
| Field       | Type        | Null | Key | Default | Extra |
+-------------+-------------+------+-----+---------+-------+
| first_name  | varchar(20) | YES  |     | NULL    |       |
| last_name   | varchar(20) | YES  |     | NULL    |       |
| email       | varchar(40) | YES  |     | NULL    |       |
| email_count | int(11)     | YES  |     | NULL    |       |
+-------------+-------------+------+-----+---------+-------+
4 rows in set (0.00 sec)

mysql> insert into email(first_name,last_name,email,email_count)
    -> values('Deepak','Kumar','[email protected]',100)
    -> ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from email;
+------------+-----------+----------------------+-------------+
| first_name | last_name | email                | email_count |
+------------+-----------+----------------------+-------------+
| Deepak     | Kumar     | [email protected] |         100 |
+------------+-----------+----------------------+-------------+
1 row in set (0.00 sec)

mysql> insert into email
    ->         values('Rajeev','Singh','[email protected]',500);
Query OK, 1 row affected (0.00 sec)

mysql> select * from email;
+------------+-----------+----------------------+-------------+
| first_name | last_name | email                | email_count |
+------------+-----------+----------------------+-------------+
| Deepak     | Kumar     | [email protected] |         100 |
| Rajeev     | Singh     | [email protected] |         500 |
+------------+-----------+----------------------+-------------+
2 rows in set (0.00 sec)

mysql> insert into email
    ->         values('Dinesh','Singh','[email protected]',200);
Query OK, 1 row affected (0.01 sec)

mysql> insert into email
    ->         values('John','T','[email protected]',400);
Query OK, 1 row affected (0.01 sec)

mysql> insert into email(first_name,last_name,email_count)
    -> values('Ishu','roy',800) ;
Query OK, 1 row affected (0.00 sec)

mysql> select * from email;
+------------+-----------+----------------------+-------------+
| first_name | last_name | email                | email_count |
+------------+-----------+----------------------+-------------+
| Deepak     | Kumar     | [email protected] |         100 |
| Rajeev     | Singh     | [email protected] |         500 |
| Dinesh     | Singh     | [email protected] |         200 |
| John       | T         | [email protected]   |         400 |
| Ishu       | roy       | NULL                 |         800 |
+------------+-----------+----------------------+-------------+
5 rows in set (0.00 sec)

mysql>

Thanks

Ads









Related Tutorials/Questions & Answers:
How to insert data into MySQL Table?
Data fetch from multiple SQL tables - Hibernate
Advertisements
Purge Data from Mysql tables
what is the jsp coding to insert a data in database tables
How to insert clob data??
please let me get code how to insert a data to mysql using setter and getter method by using java
how to insert excel file into mysql datbase in servlet
Uploading Excel sheet record in JSP to insert data in MySql
Insert Mysql Blob data
how to insert the selected item of combobox in mysql - XML
how to retrieve data from multiple tables in jsp using javabeans
how to retrieve data from multiple tables in jsp using javabeans
how to retrieve data from multiple tables in jsp using javabeans
insert data into mysql databse using swing
how to insert data from netbeans into databse
How to delete data from MySQL?
JDBC Video Tutorial- How to insert data into MySQL database table?
how to insert data into database using jsp & retrive
how to insert data in database using html+jsp
single query to insert the data
How to insert data from a combobox and textbox values into DB using JSP?
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to insert data into databasse by using jdbc
how to insert, retrieve data from,to db(code)....
how to post data in mysql php form
how to insert, retrieve data from,to db(code)....
How to Generate XML files from the data in DB tables?
Ant Script to Insert Data in Mysql Table
Mysql Multiple Date Insert

Ads