# 简单的 UPDATE 语句

我们在查询教师表`teachers`的时候发现，教师姓名`name`为'Linghu Chong'的老师邮箱`email`信息为NULL，即没有该部分信息，我们现在希望更新邮箱信息，这时候就需要用到`UPDATE`语句。

UPDATE 语句用于更新表中已存在的记录。

**语法**

```sql
UPDATE table_name
SET column1=value1,column2=value2,...
WHERE some_column=some_value;
```

> 请注意 SQL UPDATE 语句中的 WHERE 子句！WHERE 子句规定哪条记录或者哪些记录需要更新。如果您省略了 WHERE 子句，所有的记录都将被更新！

## UPDATE实例

假设我们要把 教师名`name`为"Linghu Chong" 的邮箱`email`更新为 '<LinghuChong@lintcode.com>'

```sql
UPDATE teachers
SET email='LinghuChong@lintcode.com'
WHERE name = 'Linghu Chong';
```

执行输出结果

```bash
mysql> UPDATE teachers
    -> SET email='LinghuChong@lintcode.com'
    -> WHERE name = 'Linghu Chong';
Query OK, 1 row affected (0.02 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> SELECT * FROM teachers WHERE name='Linghu Chong';
+----+--------------+--------------------------+-----+---------+
| id | name         | email                    | age | country |
+----+--------------+--------------------------+-----+---------+
|  5 | Linghu Chong | LinghuChong@lintcode.com |  18 | CN      |
+----+--------------+--------------------------+-----+---------+
1 row in set (0.01 sec)
```

**注意**

在更新记录时要格外小心！在上面的实例中，如果我们省略了 WHERE 子句，如下所示：

```sql
UPDATE teachers
SET email='LinghuChong@lintcode.com';
```

执行以上代码会将 `teachers`表中所有数据的 `email`改为 '<LinghuChong@lintcode.com>'。

执行没有 WHERE 子句的 UPDATE 要慎重，再慎重。

## 练习题: 更新表中信息

题目描述: 因为最近人工智能发展迅速，越来越多的人希望学习、了解人工智能课程，lintcode的人工智能课程人数也有大幅度增加，我们希望更新课程表`courses`中的人工智能课程(即课程名`name`为'Artificial Intelligence')的学生人数为500，请写出对应的SQL语句

```sql
-- write your sql here
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://liancode.gitbook.io/sql/level-1/update.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
