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









Related Tutorials/Questions & Answers:
Advertisements