You are here
Home > Sql Server > SQL Query (UPDATE Statement)

SQL Query (UPDATE Statement)

SQL Query

      Update Statement:

  • Update statement enables to update existing data in a table.
  • Update statement without where clause affects all rows of table.
  • It can update one or more records in a table.
  • Syntax of Update Statement:
  • update <table_name>

    set <column1>=<expression1>, <column2>=<expression2>,…., <columnN>=<expressionN>

    where condition;

  • Here, column1, column2,…., columnN are columns name. expression1, expression2,…., expressionN are new values to assign to the columns.
  • You can combine number of conditions using AND or the OR operators.
  • Only columns specified in the set clause are modified.
  • Database Table: student
Id Name City
1 Ramesh Anand
2 Pari V.V.Nagar
3 Usha Pune
4 Juhi Baroda
5 Shiv Ahmedabad
6 Sita Delhi
  • Example:
  • update student set Name=’Raja’ where Id=1;
  • One row will be updated. Select data from student table.
  • select * from student;
  • Output:
Id Name City
1 Raja Anand
2 Pari V.V.Nagar
3 Usha Pune
4 Juhi Baroda
5 Shiv Ahmedabad
6 Sita Delhi

 

  • Database Table: student1
Id Name City
1 Ramesh Anand
2 Pari V.V.Nagar
3 Usha Pune
4 Juhi Baroda
5 Shiv Ahmedabad

 

  • Example:
  • update student1 set Name=’Khushi’,  City=’Mumbai’ where Id=3;
  • One row will be updated. Select data from student1 table.
  • select * from student1;
  • Output:
Id Name City
1 Ramesh Anand
2 Pari V.V.Nagar
3 Khushi Mumbai
4 Juhi Baroda
5 Shiv Ahmedabad

 

  • Database Table: customerorder
Id Name City Qty
1 Ramesh Anand 2
2 Pari V.V.Nagar 3
3 Usha Pune 4
4 Juhi Baroda 2
5 Shiv Ahmedabad 4
6 Sita Delhi 6

 

  • Example:
  • update customerorder set Name=’Kumar’,  City=’Mumbai’ where Id=6 and Qty=6;
  • One row will be updated. Select data from customerorder table.
  • select * from customerorder;
  • Output:
Id Name City Qty
1 Ramesh Anand 2
2 Pari V.V.Nagar 3
3 Usha Pune 4
4 Juhi Baroda 2
5 Shiv Ahmedabad 4
6 Kumar Mumbai 6

 

 

Keep visiting my site ask2tech.com for upcoming top technical article

Learn more about DBA at TechNet Consultancy

 

 

One thought on “SQL Query (UPDATE Statement)

Leave a Reply

Top