当前位置:网站首页>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
边栏推荐
- SQL transaction
- 2279. 装满石头的背包的最大数量
- 16_ Redis_ Redis persistence
- 6095. Strong password checker II
- 【LeetCode】695-岛屿的最大面积
- MD5 encryption
- 5. Practice: jctree implements the annotation processor at compile time
- LeetCode刷题——两整数之和#371#Medium
- /bin/ld: 找不到 -lgssapi_krb5
- Equipped with Ti am62x processor, Feiling fet6254-c core board is launched!
猜你喜欢
Case introduction and problem analysis of microservice
6.12 企业内部upp平台(Unified Process Platform)的关键一刻
党史纪实主题公益数字文创产品正式上线
Yolov5 code reproduction and server operation
LeetCode刷题——两整数之和#371#Medium
Engineer evaluation | rk3568 development board hands-on test
15_ Redis_ Redis. Conf detailed explanation
Bing. Site Internet
PTA 天梯赛习题集 L2-001 城市间紧急救援
Deux séquences ergodiques connues pour construire des arbres binaires
随机推荐
Bing. Com website
6091. 划分数组使最大差为 K
【LeetCode】977-有序数组的平方
6092. 替换数组中的元素
For the problem that Folium map cannot be displayed, the temporary solution is as follows
(Video + graphic) machine learning introduction series - Chapter 5 machine learning practice
损失函数与正负样本分配:YOLO系列
PostgresSQL 流复制 主备切换 主库无读写宕机场景
[salesforce] how to confirm your salesforce version?
Leetcode question brushing - parity linked list 328 medium
PTA 天梯赛习题集 L2-001 城市间紧急救援
[leetcode] 1905 statistics sub Island
【LeetCode】486-预测赢家
Wechat Alipay account system and payment interface business process
密码学基础知识
自定义异常
Bing.com网站
[leetcode] 877 stone game
. Net again! Happy 20th birthday
Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board