当前位置:网站首页>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
边栏推荐
猜你喜欢

二叉树前,中,后序遍历

Storage read-write speed and network measurement based on rz/g2l | ok-g2ld-c development board

18_ Redis_ Redis master-slave replication & cluster building

Redux——详解

树-二叉搜索树

How to intercept the value of a key from the JSON string returned by wechat?

已知两种遍历序列构造二叉树

【LeetCode】417-太平洋大西洋水流问题

PTA ladder game exercise set l2-001 inter city emergency rescue

【网络安全】网络资产收集
随机推荐
03. Preliminary use of golang
[leetcode] 189 rotation array
Thoroughly understand browser strong cache and negotiation cache
Force deduction solution summarizes the lucky numbers in 1380 matrix
Deux séquences ergodiques connues pour construire des arbres binaires
Loss function and positive and negative sample allocation: Yolo series
NBA player analysis
夏季高考文化成绩一分一段表
Redux——详解
高考分数线爬取
【Experience Cloud】如何在VsCode中取得Experience Cloud的MetaData
Oracle primary key auto increment
17_ Redis_ Redis publish subscription
2279. 装满石头的背包的最大数量
Leetcode skimming -- verifying the preorder serialization of binary tree # 331 # medium
Party History Documentary theme public welfare digital cultural and creative products officially launched
MD5加密
[leetcode] 1254 - count the number of closed Islands
【Salesforce】如何确认你的Salesforce版本?
/bin/ld: 找不到 -lxslt