Categories

[MySQL Tutorial 6] Inserting Values in Table

0

In the previous MySQL tutorial on Creating Table, you learnt how a table is created in a particular database. In this tutorial, you will learn how to insert data into the table.

When we create the table, it is empty and contains no data. Insert query is used to insert values in the table. Insert entries inside the table as below:

Insert into table_name(column1_name, column2_name) values (‘value1’,’value2’);

(Please note that the number of columns to be mentioned inside the query should be same as those created in the table)

Values in the form of text are needed to be kept in single quotes while this is not required in case of numbers.

For example,

Insert into table employee (employee_id, salary) values (11, 1000); 

Insert into table employee (employee_id, salary) values (12, 2000); 

Insert into table employee (employee_id, salary) values (13, 3000); 

Insert into table employee (employee_id, salary) values (14, 4000);

To see the table you have created, use select command as described in the [MYSQL TUTORIAL 4] Selecting Database and Table

Select * from employee;

The output is as follows:

Using PHP, values can be inserted as follows:

<?php

$link=mysql_connect('localhost','root',’your_password');

mysql_select_db('mysql');

echo "connected to mysql"."<br>";

$r=mysql_query('Insert into employee(employee_id, salary) values (11,1000)');

$r=mysql_query('Insert into employee(employee_id, salary) values (12,2000)');

$r=mysql_query('Insert into employee(employee_id, salary) values (13,3000)');

$r=mysql_query('Insert into employee(employee_id, salary) values (14,4000)');

if($r)

{

echo "values inserted";

}

else

{

echo "not inserted";

}
?>

The output in the browser is:

Now that you know how to insert data into the table, continue with the next MySQL tutorial to learn how to add another column in the existing table. Also, if you have any queries, please leave a comment below.

Share.

Leave A Reply