How to insert data into MySQL Table?

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:
How to insert data into MySQL Table?
Data fetch from multiple SQL tables - Hibernate
Advertisements
what is the jsp coding to insert a data in database tables
Purge Data from Mysql tables
please let me get code how to insert a data to mysql using setter and getter method by using java
please let me get code how to insert a data to mysql using setter and getter method by using java
How to insert clob data??
Uploading Excel sheet record in JSP to insert data in MySql
how to insert excel file into mysql datbase in servlet
insert data into mysql databse using swing
how to insert the selected item of combobox in mysql - XML
ModuleNotFoundError: No module named 'tableh'
Insert Mysql Blob data
single query to insert the data
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
how to insert data from netbeans into databse
JDBC Video Tutorial- How to insert data into MySQL database table?
Mysql Multiple Date Insert
Mysql Multiple Date Insert
How to insert image in MySQL database using sql query?
insert images into a Mysql database
mysql select from multiple tables
How to compare two tables, and insert values which r not in one table to another table?
How to insert data from a combobox and textbox values into DB using JSP?
Ant Script to Insert Data in Mysql Table
how to insert data in database using html+jsp
how to insert data into database using jsp & retrive
Create Database and tables in MySQL
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
ModuleNotFoundError: No module named 'tabler'
ModuleNotFoundError: No module named 'tabler'
how to insert, retrieve data from,to db(code)....
Different tables present in MySQL
MySQL BIGINT
how to insert data from database using oops concepts - Development process
how to insert, retrieve data from,to db(code)....
insert data
how to insert and retrieve an image from mysql using java - Java Beginners
Mysql Insert
How to Generate XML files from the data in DB tables?
How to delete data from MySQL?
Mysql Join 3 Tables
mysql tables - JDBC
Servlet Example To Insert Mysql Clob Data
insert query in mysql

Ads