当前位置: 移动技术网 > IT编程>数据库>其他数据库 > SQL UPDATE 更新语句用法(单列与多列)

SQL UPDATE 更新语句用法(单列与多列)

2017年12月01日  | 移动技术网IT编程  | 我要评论
update 语句 update 语句用于修改表中的数据。 语法: update 表名称 set 列名称 = 新值 where 列名称 = 某值 例如: pers

update 语句

update 语句用于修改表中的数据。

语法:

update 表名称 set 列名称 = 新值 where 列名称 = 某值

例如:

person表:

lastname firstname address city
gates bill xuanwumen 10 beijing
wilson   champs-elysees  

更新某一行中的一个列

我们为 lastname 是 "wilson" 的人添加 firstname:

update person set firstname = 'fred' where lastname = 'wilson' 

结果:

lastname firstname address city
gates bill xuanwumen 10 beijing
wilson fred champs-elysees  

更新某一行中的若干列

我们会修改地址(address),并添加城市名称(city):

update person set address = 'zhongshan 23', city = 'nanjing'
where lastname = 'wilson'

结果:

lastname firstname address city
gates bill xuanwumen 10 beijing
wilson fred zhongshan 23 nanjing

sql的更新语句update(更新表中数据)

1. 更新表中所有行

2.更新表中特定行 ,更新时候一定不要省略where子句,否则会更新所有行。

更新update语句分为三部分,格式如下:

1 更新的表

2 列名和新的值

3 确定更新哪些行的过滤条件

单个列:

update customers
set cust_email = ' kim@qq.com'
where cust_id = '10000005';

多个列:

update customers
set cust_email = 'kim@qq.com' , cust_contact ='sam roberts'
where cust_id = '10000005';

即 更新多个列时,只需要使用一条set命令,每个 “列=值”对之间用逗号分隔,最后一列不用逗号。

update 警告!

在更新记录时要格外小心!在上面的实例中,如果我们省略了 where 子句,如下所示:

update customers
set cust_email = 'kim@qq.com'

执行以上代码会将 customers 表中所有数据的 cust_email 都改成了kim@qq.com。
执行没有 where 子句的 update 要慎重,再慎重。一般只有批量替换我们才会这么做。

移动技术网小编:强烈建议一定要先备份再操作。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网