如何在SQL中更新表?

时间:2017-12-19 01:00:16

标签: sql sql-server sql-update alter-table

我创建了一个名为Youtuber的表,代码如下:

create table Channel (
    codChannel int primary key,
    name varchar(50) not null,
    age float not null,
    subscribers int not null,
    views int not null
)

在此表中,有2个频道:

|codChannel |       name      | age | subscribers |       views |
|     1     |    PewDiePie    | 28  |  58506205   | 16654168214 |
|     2     | Grandtour Games | 15  |       429   |       29463 |

所以,我想将Grandtour Games的年龄编辑为" 18"。我怎么能用update做到这一点?
我的代码是对的吗?

update age from Grandtour Games where age='18'

3 个答案:

答案 0 :(得分:3)

不,在update中,您必须遵循以下顺序:

update tableName set columnWanted = 'newValue' where columnName = 'elementName'

在你的代码中,输入:

update Channel set age=18 where name='Grandtour Games'

以下评论:

/* Channel is the name of the table you'll update

   set is to assign a new value to the age, in your case

   where name='Grandtour Games' is referencing that the name of the Channel you want to update, is Grandtour Games */

答案 1 :(得分:0)

alter table更改架构(添加,更新或删除列或键,这类事情)。

更新表会更改表中的数据而不更改架构。

所以这两者真的很不一样。

答案 2 :(得分:0)

这是你的回答 -

-> ALTER is a DDL (Data Definition Language) statement 
UPDATE is a DML (Data Manipulation Language) statement.


->ALTER is used to update the structure of the table (add/remove field/index etc). 
Whereas UPDATE is used to update data.

希望这有帮助!