update data

Front explains how to insert data, after data is inserted, if you want to change, which need to update the data in the data table. MySQL UPDATE statement used to update the data in the table, the following syntax.

UPDATE 表名

SET field name value 1 = 1 [, field name value of 2 = 2, ......]

[Conditional Expression the WHERE];

Above syntax, the "field name" field is used to specify the name needs to be updated, "value" for the new data field updates that if you want to update the value of multiple fields, multiple fields can be separated by commas and values ​​" WHERE conditional expression "is optional, used to specify the conditions to be met to update the data. UPDATE statement can update some data in the table and all the data, then these two cases explain in detail.

1. Update All data

When not in use the UPDATE statement WHERE conditional statement, the specified field will all data in the total update.

Next, all updated data demonstrate specific case, as shown in Example 3-6.

[Example 3-6 ] The teacher all age field of the table indicates updated age of 30, as shown in the following SQL statement.

mysql> UPDATE teacher SET age=30;

Query OK, 6 rows affected (0.04 sec)

Rows matched: 6  Changed: 6  Warnings: 0

Performing the above results can be seen after the execution is complete prompt "Changed: 6", explained the success of the six updated data. To further verify, using the SELECT statement teacher to view the data in the table, as shown in the following SQL statement.

mysql> SELECT * FROM teacher;

+------+------+------+

| id   | name | age  |

+------+------+------+

|    1 | AA   |   30 |

| 2 | BB | 30 |

|    3 | CC   |   30 |

|    4 | DD   |   30 |

| 5 | OF | 30 |

|    6 | FF   |   30 |

+------+------+------+

6 rows in set (0.00 sec)

The results can be seen from the above, teacher table, all fields are updated for the age 30, proved successful update.

2. Update some data

Method explained in front of all data, this need is generally relatively small in actual development, and most part of the data update requirement is in the table, the condition can be specified using the WHERE clause of the update data.

Next, the data updating section through the presentation of specific cases, as shown in Examples 3-7.

The emp table name as wages lilei changes of 5000, as shown in the following SQL statement.

mysql> UPDATE emp SET salary=5000 WHERE name='lilei';

Query OK, 1 row affected (0.03 sec)

Rows matched: 1  Changed: 1  Warnings: 0

Performing the above results can be seen after the execution is complete prompt "Changed: 1", explain the success of a data update. To further verify, using the SELECT statement to view data emp table, as shown in the following SQL statement.

mysql> SELECT * FROM emp;

+------+-------+--------+------------+---------+------------+-------------+

| id   | name  | gender | birthday   | salary  | entry_date | resume_text |

+------+-------+--------+------------+---------+------------+-------------+

|    1 | lilei | male   | 1991-05-10 | 5000.00 | 2013-06-10 | none        |

|    2 | lucy  | female | 1988-03-15 | 6000.00 | 2014-10-20 | none        |

|    3 | king  | female | 1993-06-15 | 7000.00 | 2014-07-10 | none        |

|    0 | 2013  | 5000   | 1992-01-01 |    0.00 | 0000-00-00 | 4           |

|    5 | mary  | female | 1995-07-10 |    NULL | NULL       | NULL        |

|    6 | rin   | male   | 1996-01-01 |    NULL | NULL       | NULL        |

+------+-------+--------+------------+---------+------------+-------------+

6 rows in set (0.00 sec)

The results can be seen from the above table emp name is lilei wages and salaries successfully modified 5000.

Next emp table id for staff salaries 2 revised to 8000, resume_text amended as "excellent", as shown in the following SQL statement.

mysql> UPDATE emp

    -> SET salary=8000,resume_text='excellent'

    -> WHERE id=2;

Query OK, 1 row affected (0.04 sec)

Rows matched: 1  Changed: 1  Warnings: 0

Performing the above results can be seen after the execution is complete prompt "Changed: 1", explain the success of a data update. To further verify, using the SELECT statement to view data emp table, as shown in the following SQL statement.

mysql> SELECT * FROM emp;

+------+-------+--------+------------+---------+------------+-------------+

| id   | name  | gender | birthday   | salary  | entry_date | resume_text |

+------+-------+--------+------------+---------+------------+-------------+

|    1 | lilei | male   | 1991-05-10 | 5000.00 | 2013-06-10 | none        |

|    2 | lucy  | female | 1988-03-15 | 8000.00 | 2014-10-20 | excellent   |

|    3 | king  | female | 1993-06-15 | 7000.00 | 2014-07-10 | none        |

|    0 | 2013  | 5000   | 1992-01-01 |    0.00 | 0000-00-00 | 4           |

|    5 | mary  | female | 1995-07-10 |    NULL | NULL       | NULL        |

|    6 | rin   | male   | 1996-01-01 |    NULL | NULL       | NULL        |

+------+-------+--------+------------+---------+------------+-------------+

6 rows in set (0.00 sec)

The results can be seen from the above, emp table id for staff salaries 2 successful changed to 8000, resume_text successfully modified to excellent.

Next emp table all women wage increase based on the original 1000 yuan, as shown in the following SQL statement.

mysql> UPDATE emp

    -> SET salary=salary+1000

    -> WHERE gender='female';

Query OK, 2 rows affected (0.07 sec)

Rows matched: 2  Changed: 2  Warnings: 0

Performing the above results can be seen after the execution is complete prompt "Changed: 2", explain the two data successfully updated. To further verify, using the SELECT statement to view data emp table, as shown in the following SQL statement.

mysql> SELECT * FROM emp;

+------+-------+--------+------------+---------+------------+-------------+

| id   | name  | gender | birthday   | salary  | entry_date | resume_text |

+------+-------+--------+------------+---------+------------+-------------+

|    1 | lilei | male   | 1991-05-10 | 5000.00 | 2013-06-10 | none        |

|    2 | lucy  | female | 1988-03-15 | 9000.00 | 2014-10-20 | excellent   |

|    3 | king  | female | 1993-06-15 | 8000.00 | 2014-07-10 | none        |

|    0 | 2013  | 5000   | 1992-01-01 |    0.00 | 0000-00-00 | 4           |

|    5 | mary  | male   | 1995-07-10 |    NULL | NULL       | NULL        |

|    6 | rin   | male   | 1996-01-01 |    NULL | NULL       | NULL        |

+------+-------+--------+------------+---------+------------+-------------+

6 rows in set (0.00 sec)

The results can be seen from the above, emp table all field values ​​female gender, wages and salaries increased by 1,000 yuan.

Guess you like

Origin www.cnblogs.com/momenglin/p/10928915.html