【问题标题】:How can I update the table in SQL?如何更新 SQL 中的表?
【发布时间】:2017-12-19 01:00:16
【问题描述】:

我已经创建了一个名为 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'

【问题讨论】:

  • 你知道一些我不知道的事情。你会如何使用alter table 来做到这一点?
  • 你说得对,我忘记了 alter table 仅适用于表名或列。谢谢
  • 不要在标题中添加(CLOSED),而是考虑删除问题。
  • Update channel set age = 18 where codChannel = 2
  • 正确标记!!! MySQL SQL 服务器。这是哪一个??????

标签: sql sql-update alter-table


【解决方案1】:

不,在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 */

【讨论】:

  • columnWanted 是具有我要更新的值的列吗?
  • 是的。在您的情况下,是列年龄。
【解决方案2】:

alter table 更改架构(添加、更新或删除列或键,诸如此类)。

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

所以两者真的很不一样。

【讨论】:

    【解决方案3】:

    这是你的答案 -

    -> 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.
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2021-09-27
      • 1970-01-01
      • 2016-04-19
      • 1970-01-01
      • 2013-11-20
      • 2019-07-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多