Updating records

Previous Index Next

The syntax of the statement for updating records in a table is as follows:

UPDATE <table_name>
SET <field_name> = <expression> {, <field_name> = <expression>}
  [WHERE <search_condition>]

The above statement updates the records that satisfy <search_condition> in the WHERE clause by changing the values of the field names specified in the SET clause. If there is no WHERE clause, all the records will be updated.

See the following examples (click here to see the contents of the original table).

Decrease the marks of CLC by 10 and increase the marks of CS by 5 for all students.
UPDATE s6a SET clc = clc - 10, cs = cs + 5
After execution, the table becomes:
class_num stud_id name email clc ue phy pm cs
1 92114 Chan Wai Man, Raymond s92114@sample.edu.hk 54 55 70 64 64
2 92133

Chow Chi Ling

clchow@example.com 60 62 62 59 75
3 94302 Fung Ching Man, Mandy mandyfung@example.com 62 50 42 59 65
4 91131 Hung Wai Ming s91131@sample.edu.hk 42 48 55 39 64
5 92153 Leung King kleung@example.com 30 50 51 40 65
6 92211 Poon Kwok Fai kwokfai@testing.com.hk 49 60 70 77 80
7 91194 Sung Hing Wah, Patrick patricksung@example.com 60 72 81 69 75
8 97602 Tang Wing Chi s97602@sample.edu.hk 70 79 70 72 74
9 92145 Wong Ka Tak, Kent klkwon@example.com 25 40 52 50 47
10 93211 Yeung Chun cyeung@testing.com.hk 59 80 77 60 57
11 96374 Lai Fung Chun s96374@sample.edu.hk 68 75 69 65 66
12 94412 Chan Lai Yin cly@testing.com.hk 33 59 53 61 65
13 98832 Man Fook Wing fwman@testing.com.hk 45 77 34 40 56
14 95343 Chung Kwok Fai, Fred fredchung@example.com 62 62 53 47 55
15 97233 Lee Lai May maylee@testing.com.hk 57 71 56 60 60
 
After updating the records using the statement in the previous example, the following statement updates the marks of Leung King as follows:

CLC: 37, UE: 52, PHY: 55, PM: 50, CS: 62.

UPDATE s6a SET clc = 32, ue = 52, phy = 55, pm = 50, cs = 62
  WHERE name = "Leung King"
After execution, the table becomes:
class_num stud_id name email clc ue phy pm cs
1 92114 Chan Wai Man, Raymond s92114@sample.edu.hk 54 55 70 64 64
2 92133

Chow Chi Ling

clchow@example.com 60 62 62 59 75
3 94302 Fung Ching Man, Mandy mandyfung@example.com 62 50 42 59 65
4 91131 Hung Wai Ming s91131@sample.edu.hk 42 48 55 39 64
5 92153 Leung King kleung@example.com 32 52 55 50 62
6 92211 Poon Kwok Fai kwokfai@testing.com.hk 49 60 70 77 80
7 91194 Sung Hing Wah, Patrick patricksung@example.com 60 72 81 69 75
8 97602 Tang Wing Chi s97602@sample.edu.hk 70 79 70 72 74
9 92145 Wong Ka Tak, Kent klkwon@example.com 25 40 52 50 47
10 93211 Yeung Chun cyeung@testing.com.hk 59 80 77 60 57
11 96374 Lai Fung Chun s96374@sample.edu.hk 68 75 69 65 66
12 94412 Chan Lai Yin cly@testing.com.hk 33 59 53 61 65
13 98832 Man Fook Wing fwman@testing.com.hk 45 77 34 40 56
14 95343 Chung Kwok Fai, Fred fredchung@example.com 62 62 53 47 55
15 97233 Lee Lai May maylee@testing.com.hk 57 71 56 60 60

Previous Index Next