sql server中的insert、update、delete

本文详细介绍了SQL Server中对数据表进行插入、更新和删除操作的使用方法,包括针对单个元组、多个元组及全表的操作。通过实例展示了如何使用INSERT INTO插入数据,UPDATE修改特定条件的元组,以及DELETE删除数据。同时,文章探讨了在进行UPDATE操作时处理多条记录匹配和避免误操作的注意事项。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

本文用到3张表:

student(sno,sname,sage,ssex,sdept)primary key(sno)
sc(sno,cno,score)primary key(sno,cno)
course(cno,cname,cpno,ccredit)primary key(cno)

insert、update、delete操作对象为:
1. 一个元组
2. 多个元组
3. 全表

insert into:插入一个元组
框架为:

insert into tableName(attribute1,attribute2......) values (attribute1's values,attribute2's values.....)

例子:

/两个括号里面的字段名位置一致 
insert into student(sno,sname,sage) values (20200001,张三,20);

/当然,如果表结构中某一字段(列)可以为null,我们在插入时也可以为空 
insert into student(sno,sname,sage) values (20200001,'张三',null);

/当表名后面没有括号,后面的values就只能按表的字段顺序给值 
insert into student values (20200001,'张三',20,'男','计算机科学与技术');

insert into:插入多个元组
框架为:

insert into tableName(attribute1,attribute2......) (select attribute1,attribute2.....
	from sc where .........
)

例子:

/详细DDL语言(数据库模式定义语言)在文末有链接()
creat table fail(
	sno datetype primary key,
	cno	datetype,
	cname datetype,
	score datetype
)/datetype与原表中的一样;比如说我student中sno的数据类型为char(9),那么fail表中的sno的数据类型就是char(9)
/把选修了c1但是成绩不及格的学生添加到fail表中
insert into fail(sno,cno,cname,score) (
	select sno,cno,cname,score from sc,course where sc.cno = course.cno and cno = 'c2'
)

insert into:在sc表中插入一个在student表中和course表中都没有的学号和课程号

	步骤:
		1、在student表中插入该生
		2、在course表中插入改课程
		3、在sc表中插入改条选课记录
	很简单的,请读者自行实现了 ^_^ 

update:一个元组
框架为:

/属性为整数类型可以进行+-*/运算再赋值 或者 可以直接进行修改(修改前后的数据类型要一致)
update tableName set attribute = (attribute +-*/  constant) or constant where .....

例子:

update student set sage = sage * 2 where sno = 20200001;
update student set ssex = '男' where sno = 20200001;

update:多个元组
框架为:

update tableName set attribute =  (attribute +-*/  constant) or constant where .....

例子:

/将选修c2这门课的的同学的分数都改成100update sc set score = 100 where cno = 'c2'

update:多个元组修改-进阶题

一、对于SC表,将课程编号为c2号的最低分改为在原分数*2;
逻辑:

  • 1、得到’c2’这门课的最低分
  • 2、用最低分和’c2’作条件从sc里面找出满足条件的元组
  • 3、修改元组的分数
update sc set score=score * 2 where sno in (
	select sno from sc where score = (
		select min(score) from sc where cno='c2' ) 
	and cno = 'c2'
)

细节:请读者思考最外层查询要用sno in?以及逻辑2中为什么得到了’c2’的最低分了还要加上’c2’这个条件?
二、对于SC表,将课程名为数据结构的最低分改为在原分数*2
逻辑:

  • 1、得到’c2’这门课的最低分
  • 2、用最低分和数据结构作条件从sc里面找出满足条件的元组
  • 3、修改元组的分数

细节:请读者思考最外层查询要用sno in?以及逻辑2中为什么得到了数据结构的最低分了还要加上数据结构这个条件?


/多表连接,
update sc set score = score * 100 where sno in (
	select sno from sc where score = (
		select min(score) from sc,course where sc.cno = course.cno and cname = '数据结构'
	)
	and cno = (
		select cno from course where cname = '数据结构'
	)
)
/嵌套查询
update sc set score = score * 2 where sno in (
	select sno from sc where score = (
		select min(score) from sc where cno = (
			select cno from course where cname = '数据结构'
		)
	)
	and cno = (
		select cno from course where cname = '数据结构'
	)
)

/两个子查询做条件来得到数据结构这门课最低分
update sc set score = 3 where sno in (
	select sno from sc where ( score = (
		select min(score) from sc )
		and 
		cno = (
			select cno from course where cname = '数据结构'
		)
	)
	and cno = (
		select cno from course where cname = '数据结构'
	)
)

对细节的回答:

  • 如果你用sno = ,而不用sno in,那么满足’c2’最低分(数据结构最低分)的结果是多个元组怎么办,就是最低分有好几个学生。

  • 当内层查询返回数据结构最低分时, 你不加’c2’(数据结构)这个条件直接:select sno from sc where score = min(score)l;你不也把其他科目和数据结构最低分相等的元组也加进去了。

delete:一个元组
框架为:

delete from tableName where (table's attribute) = ...;

例子:

/删除学号为20200001的学生记录
delect from student where sno = 20200001;

delete:多个元组
框架为:

delect from tableName where (table's attribute) in (select ......)

例子:

/删除计算机系的所以学生记录
delect from student where snoin (select sno from student where sdept = '计算机')

delete:全表记录
框架为:

delect from tableName;

例子:

/删除所有选课记录
delect from sc;

DDL语言

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值