Update command in SQL is used to change any record in the table. Records are to be manipulated or updated using update command. Conditional update is the most common type of update command used in MySQL also. You are requested to go through the SQL WHERE command before using update command as both are to be used in proper combinations. Here is one simple command but before trying this please understand the implication of this. Without using any where command (or without using any restriction) the command will change all the records of the table. So let us start with the simple command.
UPDATE student SET class='Five'
This command will change all the records of the table student and will change all the class field to Five. This is not what is required in common cases so we will be changing records based on some conditions. Now we will change all the class four students to class five. Our command should selectively update those records for which class is equal to “four” and will update them to “five”. We will use one where clause along with update command for updating the records.
UPDATE student SET class='Five' WHERE class='Four'
This command will update only those records for which class is equal to 'Four'. So this way we can update records selectively. Now let us move one more step and change the records selectively based on some value in some other field. We will change records for which class is equal to 'Four' and mark is more than 70. We will promote those students only who has got more than or equal to 70 mark.
UPDATE student SET class='Five' WHERE class='Four' and mark >= 70
This will only change the records for which class=four and mark is more than or equal to 70. We have added two conditions by using AND as a logical operator. This way we can continue with adding more AND combinations to the query. Depending on the logic requirement OR combination can be added to the WHERE clause to UPDATE the records.
Updating multiple columns of a table
We will use one more student table where we will store mark obtained by students in three subjects. You can download sql dump of this table at the end of this tutorial.
My John Rob
Update mark of the student
Let us update the new marks for Krish Star ( id =4)
UPDATE student3 SET math=50, social=60, science=55 WHERE id=4
Let us increase the mark of Arnold ( id =3 ) by 5 in all subjects
UPDATE student3 SET math=math+5, social=social+5, science=science+5 WHERE id=3
By adding a Where condition we are restricting updating to one record only. Without using Where clause we can update the total table with new data. Let us try to update student marks to 0.
UPDATE student3 SET math=0, social=0, science=0
Updating second tables with data from first table
It is often required to use one table and update data in another table. The second table columns gets updated by taking data from first table. Let us say we have one table where students test marks are stored along with other details in other columns. We can collect only the test data and keep them in a separate table. Let us call this second table as student mark table. Both the tables will have student id field which we will be using to link both tables.
Let us start with our student mark table. This table has two columns, s_id stores the student id and mark column stores total mark of the student.
We will keep id of the student in s_id and keep mark as zero. By using sql dump given at the end of this page you can create table. Let us first change the total mark of all students to zero.
UPDATE student3_total SET mark=0
Now let us update this mark column of student3_total table with sum of subject marks of student3 table. Here is the query.
UPDATE student3_total INNER JOIN student3 ON s_id=id SET mark= (math+social+science)
Above query will update mark column of student3_total with sum of the marks of each student.
Updating a password field using md5 encryption
We can update the password field with md5 encryption. To store this data we will change the length of field to 32 char. Here is a command to update password field with new md5 encrypted password.
write a update statement no procedure where we can update employee gender column value to female if it is male or to male if it is female.
how can i insert more than one row in sql 2008
plese send full code in asp.net
I keep getting syntax errors using that. In my example, I am trying to add the contents of Field1+Feild2 to Field1.
TableName (name of table)
Field1 = 5
Field2 = 2
I want to update it so that Field1 = 5+2 in this example.
Tried using two lines:
SET R1= sum(R1,R1a)