Categories

[MySQL Tutorial 8] Updating Table

0

In the previous MySQL tutorial on Altering table, you learnt how to add a column to the existing table. In this tutorial, you will learn how to update values inside the table.

We may like to change values inside the table. For example, in the tutorial [MySQL Tutorial 7] Altering Table we add a column “name” to the table “employee”. But the column has “null” as default values. To make changes in the table, you can use update command:

Update table_name set your_query;


Check if the new column exits:

Select * from employee;


Insert values into the rows of ‘name’ column as described above:

Update employee set name=’ravi’ where employee_id=’11’;

Update employee set name=’ram’ where employee_id=’12’;

Update employee set name=’shyam’ where employee_id=’13’;

Update employee set name=’ramesh’ where employee_id=’14’;

 

Check the output as below:

To update the employee table using PHP:

<?php
$link=mysql_connect('localhost','root','your_password');
mysql_select_db('mysql');
echo "connected to mysql"."<br>";
$r=mysql_query('update employee set name="ravi" where employee_id=11');
$r=mysql_query('update employee set name="ram" where employee_id=12');
$r=mysql_query('update employee set name="shyam" where employee_id=13');
$r=mysql_query('update employee set name="ramesh" where employee_id=14');
if($r)
{
echo "table updated";
}
else
{
echo "not updated";
}
?>
The output is as below:

Now that you know how to update an existing table, continue with the next MySQL tutorial on how to delete a table. Also, if you have any queries, please leave a comment below.

Share.

Leave A Reply