-1

I'm learning SQL at the moment, and have found some different methods of updating and setting a table.

For this example, I am wondering what is the "most correct way" of doing this?

    UPDATE student
    SET test1 = 7, test2 = 9
    where stuid = 999

or

    UPDATE student
    SET test1 = 7
    where stuid = 999
    UPDATE student
    SET test2 = 9
    where stuid = 999

Thanks.

Anteara
  • 729
  • 3
  • 14
  • 33

5 Answers5

1

Running one update statement is always better than two. I choose the first one. It will only run on the database once and updates two columns.

John Woo
  • 258,903
  • 69
  • 498
  • 492
1

I will go with first statement because its just 1 call to database server.

DevelopmentIsMyPassion
  • 3,541
  • 4
  • 34
  • 60
1

I suggest the first, don't repeat yourself, and always write neat codes.

Roger Liu
  • 1,768
  • 3
  • 16
  • 25
1

Statement 1 is without any doubt better than statement 2 ... Why to load the db server twice than required ...

alwaysLearn
  • 6,882
  • 7
  • 39
  • 67
1

Firstly always Google yourself before posting any Questions.

The answer (as said by others) is off-course the Method 1. As it will not reload the database Twice.

Also if you are Beginner try to go learn from W3Schools

Prahalad Gaggar
  • 11,389
  • 16
  • 53
  • 71
  • i actually did google and that's what prompted me to ask the question :) http://stackoverflow.com/questions/6446250/sql-statement-with-multiple-sets-and-wheres Here, i can see that almost every single answer states to use MULTIPLE statements, where only one answer uses 'method 1' in my OP, which everyone here is stating is correct. – Anteara Apr 01 '13 at 10:03
  • It that Particular **Question**, as the heading Suggests the **OP** wants to update multiple column with **multiple Where condition**. But in your case you are updating on same **where Condition**. – Prahalad Gaggar Apr 01 '13 at 10:16
  • Ah! I overlooked that part! Thanks! – Anteara Apr 01 '13 at 11:39