【SQL Server数据库】数据库的更新操作(四)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/qq_41427568/article/details/102594568

本文上接【SQL Server数据库】建库、建表、简单查询语句(一),本系列主要记录数据库实验内容,本文主要记录SQL Server数据集的更新操作。
具体表的属性attribute和名称table name也可以在上面链接中找到。

/*向student中插入数据*/
insert into student
values  ('20069011','李一',null,'男','0001','1985/1/2'),
		('20069012','李二',null,'女','0002','1986/1/2')	

/*建立一个'成教表'chengjiao,结构和student相同*/
create table chengjiao
(
	SNO char(8) not null unique,
	SNAME char(10) not null,
	AGE smallint,
	SEX char(2),CONSTRAINT chk_sex check (SEX in ('男','女')),
	DNO char(4),
	BIRTHDAY  datetime,
	primary key (SNO)
)

/*将一个新学生插入到student表中*/
insert into student(SNO,SNAME,AGE,DNO)
values('20067027','张三','20','0002')

/*按如下的语句插入另外两个同学的信息到chengjiao表中*/
insert into chengjiao(SNO,SNAME,AGE,DNO)
values('20067011','王二',23,'0003')
insert into chengjiao(SNO,SNAME,AGE,DNO)
values('20067021','张三',19,'0003')

/*将成教表chengjiao中的所有学生一次性全部添加到学生表student中*/
insert into student(SNO,SNAME,SEX,DNO)
(
	select SNO,SNAME,SEX,DNO
	from chengjiao
)

/*依照学生的生日,计算出该学生的年龄*/
update student
set AGE=(YEAR(GETDATE())-YEAR(BIRTHDAY))

/*将所有安排在A209的课程调整到D109*/
update course
set ROOM='D109'
where ROOM='A209'

/*将选课表中的'线性代数'课程成绩减去4分*/
update sc
set GRADE=GRADE-4
where CNO in (select CNO 
			  from course
			  where CNAME='线性代数')

/*从排课表中删除‘杨丽’老师的所有排课纪录*/
delete from course
where TNAME='杨丽'

/*删除学院编号为空的学生记录以及选课记录*/
delete from sc 
where SNO in (select SNO
			  from student
			  where DNO is null)
delete from student
where DNO is null

/*删除表 ’excelxuanke’*/
drop table excelxuanke

/*在选课表中插入一个新的选课记录,学号为 20002059,授课班号为 244501,成绩 80分。*/
insert into sc
values('20002059','244501','80')

/*从选课表中删除选修‘线性代数’的选修纪录*/
delete from sc
where CNO in (select CNO
			  from course
			  where CNAME='线性代数')

/*将机电学院的女生一次性添加到成教表中*/
insert into chengjiao(SNO,SNAME,DNO,SEX,AGE,BIRTHDAY)
(
	select SNO,SNAME,DNO,SEX,AGE,BIRTHDAY
	from student
	where DNO = (select DNO
				 from dept
				 where DNAME='机电工程学院')
)

/*将所有学生的高等数学成绩加5分*/
update sc
set GRADE=GRADE+5
where CNO in (select CNO
			  from course
			  where CNAME='高等数学')

/*将学号尾数为‘4’的同学成绩加 2 */
update sc
set GRADE=GRADE+4
where SNO in (select SNO
			  from student
			  where SUBSTRING(SNO,8,1)='4')
			
/*删除电科系所有学生的选课记录*/
delete from sc
where SNO in (select SNO
			  from student
			  where DNO=(select DNO
						 from dept
						 where DNAME='电科'))

/*将学号为“20002059”的学生姓名改为“王菲”*/
update student
set SNAME='王菲'
where SNO='20002059'

/*删除成绩为空的选课记录*/
delete from sc
where GRADE is null

猜你喜欢

转载自blog.csdn.net/qq_41427568/article/details/102594568