当前位置:网站首页>【数据库和SQL学习笔记】7.SQL中的插入(INSERT)、删除(DELETE)、更新(UPDATE)
【数据库和SQL学习笔记】7.SQL中的插入(INSERT)、删除(DELETE)、更新(UPDATE)
2022-08-05 05:15:00 【takedachia】
工具:SQL Server 2019 Express
操作系统:Windows 10
讲完SELECT,本节讲SQL语言中的 插入(INSERT)、删除(DELETE)、更新(UPDATE)。
用到的数据库备份: teaching.bak
回顾一下表结构:
t_student (S#, Sname, Sex, Age, Major)
t_teacher (T#, Tname, Age, Title)
t_course (C#, Cname, T#)
t_student_course (S#, C#, Score)
插入:INSERT INTO
包括插入单条记录、插入多条记录、查询结果的插入。
插入单条记录
语法:
INSERT INTO 基本表名 [(列名表)] VALUES(列值表)
例:对 t_student_course 表插入一条内容:
insert into t_student_course (S#, C#, Score) values ('2012001','c006',90)因为我们插入的是一条完整的信息(列齐全,顺序一致),可直接写成:
insert into t_student_course values ('2012001','c006',90)
当然,插入的顺序可以换(前面列名对应上就可以),并且可以不插全(没填充的内容自动填NULL):
insert into t_student_course (C#, S#) values ('c007', '2012001')
插入多条记录
多条同单条,语法:
INSERT INTO 基本表名 [(列名表)]
VALUES(列值表),
(列值表),
(列值表),
...
查询结果的插入
例:统计男学生平均成绩大于80分的记录,并将学号和平均成绩插入到表S_SCORE中。
先创建表:create table S_SCORE(S# char(10), AVG_SCORE smallint)做插入时可以直接传入一个select到的表结果(INSERT INTO + SELECT),所以:
insert into S_SCORE(S#, AVG_SCORE) select S#, AVG(Score) from t_student_course where S# in (select S# from t_student where Sex='男') group by S# having AVG(Score)>80
删除:DELETE FROM
语法:
DELETE FROM 基本表名 [WHERE 条件表达式]
例1:从 t_student_course 中删除S#为2012001,C#为c007的记录:
delete from t_student_course where S#='2012001' and C#='c007'
例2:从选课表中删除数据库课程的选课记录:
delete from t_student_course where C#= (select C# from t_course where Cname='数据库')可以看到代号为c001的记录被删了。
例3:把c001课程中成绩低于该课程平均成绩的选课记录从t_student_course中删除:
delete from t_student_course where C#='c001' and Score< (select avg(Score) from t_student_course where C#='c001')
更新 / 修改:UPDATE SET
语法:
UPDATE 基本表名
SET 列名=值表达式[, 列名=值表达式...]
[WHERE 条件表达式]
例1:把2012001号学生的年龄修改为20岁,专业修改为机械电子。
update t_student set Age=20, Major='机械电子' where S#='2012001'
例2:将所有女学生的成绩提高10%。
update t_student_course set Score = Score * 1.1 where S# in (select S# from t_student where Sex='女')
例3:当c008课程的考试成绩低于该课程的平均成绩时,将成绩提高5%。
update t_student_course set Score = Score * 1.05 where C#='c008' and Score<(select avg(Score) from t_student_course where C#='c008')
边栏推荐
- [Go through 9] Convolution
- flink实例开发-详细使用指南
- [Go through 8] Fully Connected Neural Network Video Notes
- flink项目开发-配置jar依赖,连接器,类库
- 第二讲 Linear Model 线性模型
- vscode+pytorch使用经验记录(个人记录+不定时更新)
- 【NFT网站】教你制作开发NFT预售网站官网Mint作品
- IDEA 配置连接数据库报错 Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
- CAP+BASE
- 【Reading】Long-term update
猜你喜欢

Kubernetes常备技能

Mesos learning

The difference between the operators and logical operators

Mysql-连接https域名的Mysql数据源踩的坑

flink项目开发-flink的scala shell命令行交互模式开发
![[Go through 7] Notes from the first section of the fully connected neural network video](/img/e2/1107171b52fe9dcbf454f7edcdff77.png)
[Go through 7] Notes from the first section of the fully connected neural network video

DOM and its applications
![[Go through 9] Convolution](/img/84/e6d99793aacf10a7b099f60bcaf290.png)
[Go through 9] Convolution
![[Go through 10] sklearn usage record](/img/70/60783c7d16000c6e9d753d8db9a330.png)
[Go through 10] sklearn usage record

CAP+BASE
随机推荐
IDEA 配置连接数据库报错 Server returns invalid timezone. Need to set ‘serverTimezone‘ property.
【过一下6】机器视觉视频 【过一下2被挤掉了】
Distributed and Clustered
Flink Distributed Cache 分布式缓存
Do you use tomatoes to supervise your peers?Add my study room, come on together
[Remember 1] June 29, 2022 Brother and brother double pain
spark-DataFrame数据插入mysql性能优化
学习总结week3_4类与对象
Using pip to install third-party libraries in Pycharm fails to install: "Non-zero exit code (2)" solution
序列基础练习题
MySql之索引
vscode要安装的插件
[Skill] Long-term update
My 的第一篇博客!!!
redis persistence
门徒Disciples体系:致力于成为“DAO世界”中的集大成者。
The role of the range function
Pycharm中使用pip安装第三方库安装失败:“Non-zero exit code (2)“的解决方法
The software design experiment four bridge model experiment
Flink accumulator Counter 累加器 和 计数器


