当前位置:网站首页>SQL修改语句
SQL修改语句
2022-07-02 12:15:00 【全栈程序员站长】
大家好,又见面了,我是你们的朋友全栈君。
如果我们要修改数据库中表的数据,这个时候我们就要使用到UPDATE语句。
UPDATE语句的基本语法是:
UPDATE <表名> SET 字段1=值1, 字段2=值2, … WHERE …;
例如,我们想更新employees表id=100的记录的last_name和salary这两个字段,先写出UPDATE employees SET name=”大哥”, salary = 54000,然后在WHERE子句中写出需要更新的行的筛选条件id=100:
即UPDATE employees SET last_name=‘大哥’, salary=54000 WHERE employee_id=100;
–修改前:
–修改后:
在这里我们发现UPDATE语句的WHERE条件和SELECT语句的WHERE条件其实是一样的,因此完全可以一次更新多条记录:
UPDATE employees SET last_name=‘TEST’, salary=54000 WHERE employee_id>=101 and employee_id<=105;
–修改前:
–修改后:
在UPDATE语句中,我们在更新字段时还可以使用表达式。
例如,把所有50000元以上工资的员工加666元:
UPDATE employees SET salary=54000+666 WHERE salary >= 50000;
–查询结果
其中,SET salary=54000+666就是给当前行的salary字段的值加上了666。
如果WHERE条件没有匹配到任何记录,UPDATE语句不会报错,但是也不会有任何记录被更新。
UPDATE语句可以没有WHERE条件,例如:
UPDATE employees SET salary=54000+666
这个时候,整个表的所有记录都会被更新。所以,在执行UPDATE语句时要非常小心,最好先用SELECT语句来测试WHERE条件是否筛选出了期望的记录集,然后再用UPDATE进行更新。
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/147718.html原文链接:https://javaforall.cn
边栏推荐
- 6095. 强密码检验器 II
- Basic knowledge of cryptography
- 自定义异常
- 2022 年辽宁省大学生数学建模A、B、C题(相关论文及模型程序代码网盘下载)
- 【LeetCode】1162-地图分析
- [leetcode] 1020 number of enclaves
- Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
- Name of institution approved in advance
- 【LeetCode】1254-统计封闭岛屿的数量
- SQL stored procedure
猜你喜欢
Finally, I understand the event loop, synchronous / asynchronous, micro task / macro task, and operation mechanism in JS (with test questions attached)
There are 7 seats with great variety, Wuling Jiachen has outstanding product power, large humanized space, and the key price is really fragrant
Solve the problem of frequent interruption of mobaxterm remote connection
2022 college students in Liaoning Province mathematical modeling a, B, C questions (related papers and model program code online disk download)
6.12 critical moment of Unified Process Platform
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
How to avoid 7 common problems in mobile and network availability testing
LeetCode刷题——递增的三元子序列#334#Medium
Leetcode question brushing - parity linked list 328 medium
随机推荐
Basic knowledge of cryptography
【LeetCode】977-有序數組的平方
Redux - detailed explanation
二叉树前,中,后序遍历
02.面向容器化后,必须面对golang
【LeetCode】1254-统计封闭岛屿的数量
百变大7座,五菱佳辰产品力出众,人性化大空间,关键价格真香
NBA player analysis
6090. Minimax games
/bin/ld: 找不到 -lxslt
16_ Redis_ Redis persistence
党史纪实主题公益数字文创产品正式上线
Bing. Com website
[leetcode] 695 - maximum area of the island
PostgresSQL 流复制 主备切换 主库无读写宕机场景
How to find a sense of career direction
LeetCode刷题——统计各位数字都不同的数字个数#357#Medium
[leetcode] 200 number of islands
2278. Percentage of letters in string
【LeetCode】486-预测赢家