自学--数据库笔记--第七篇--SQL sever2008更新数据

自学--数据库笔记--第七篇--SQL sever2008更新数据

所有使用的都为已制作好的表

1.

 1 插入数据 两种语句格式
 2 insert…values语句
 3 
 4 --1.向depart表中插入一行数据('5','计划部','008','2206')
 5 insert into depart
 6 values('5','计划部','008','2206')
 7 --向worker表中插入一行数据,职工号:010,职工名:李飞龙,生日:1967-04-01,部门号:4
 8 insert into worker(wid,wname,wbirthdate,depid)  --指定字段
 9 values('010','李飞龙','1967-04-01','4')
10 --向salary表中插入一行数据,职工号:010,totalsalary:2800
11 insert into salary(wid,totalsalary) --错误的,有两个主键,,需要全部不为空
12 values('010',2800) --数字类型不加引号
13 
14 insert into salary(wid,sdate,totalsalary)
15 values('010','2011-01-04',2800) --数字类型不加引号
16insert…select语句 从某张张表中查询的数据,放到另外一张表中
17 
18 --创建一个新表worker_f,然后将worker表中所有女职工的职工号,职工名,出生日期这三个字段的信息插入进入
19 create table worker_f
20 (
21 wid char(3) primary key,
22 wname varchar(10) not null,
23 wbirthdate date
24 )
25 
26 insert into worker_f
27 select wid,wname,wbirthdate
28 from worker
29 where wsex=''
30 修改数据 update
31 
32 --修改worker表中的数据,将姓名为‘李飞龙’的职工的性别修改为‘男’  基于单表
33 update worker
34 set wsex=''
35 where wname='李飞龙'
36 --将1975年以前出生的职工2011年1月份的totalsalary增加500元,actualsalary增加400元 基于多表
37 
38 --利用多表连接查询
39 update salary
40 set totalsalary=totalsalary+500,actualsalary=actualsalary+400
41 from worker inner join salary on worker.wid=salary.wid
42 where YEAR(wbirthdate)<1975 and YEAR(sdate)=2011 and MONTH(sdate)=1
43 
44 --利用子查询
45 update salary
46 set totalsalary=totalsalary+500,actualsalary=actualsalary+400
47 where YEAR(sdate)=2011 and MONTH(sdate)=1 and wid in
48 (
49 select wid
50 from worker
51 where YEAR(wbirthdate)<1975
52 )
53 删除数据 delete from
54 
55 --删除李飞龙的信息
56 delete from worker
57 where wname='李飞龙'
58 --删除余慧的工资信息
59 
60 --利用多表连接查询
61 delete from salary
62 from worker inner join salary on worker.wid=salary.wid
63 where wname='余慧'
64 
65 --利用子查询
66 delete from salary
67 where wid=
68 (
69 select wid
70 from worker
71 where wname='孙华'
72 )

2.

 1 更新数据库综合操作
 2 
 3 --1.将一个新学生记录(学号:95020;姓名:陈冬;性别:男;所在系:英语;出生日期:1996-03-02;)插入到student表中
 4 insert into student
 5 values('95020','陈冬','','1996-03-02','英语系')
 6 --2.插入一条选课记录('95020','1')
 7 insert into stu_course(sno,cno)
 8 values('95020','1')
 9 --3.在数据库中建立一个有两属性列的新表,其中一列存放系别,另一列存放相应系的平均年龄
10 create table avg_sdept
11 (
12 sdept varchar(30) primary key,
13 avgage int
14 )
15 --4.对数据库的student表按系分组求平均年龄,再把系别和平均年龄存放新表中
16 insert into avg_sdept
17 select sdept,AVG(YEAR(getdate())-YEAR(sbirth)) as 平均年龄
18 from student
19 group by sdept
20 --5.将学生95001的出生日期修改为:1997-09-01
21 update student
22 set sbirth='1997-09-01'
23 where sno='95001'
24 --6.将计算机系全体学生的成绩修改为:0,分别用不同的方式完成
25 --多表链接查询
26 update stu_course
27 set grade=0
28 from student inner join stu_course on student.sno=stu_course.sno
29 where sdept = '计算机系'
30 
31 --子查询
32 update stu_course
33 set grade=0 
34 where sno in
35 (
36 select sdept
37 from student
38 where sdept = '计算机系'
39 )
40 --7.删除第三张新建表中的所有信息
41 delete from avg_sdept
42 --8.删除学号为‘95020’的学生记录
43 delete from student
44 where sno='95020'
45 --9.删除计算机系所有学生的选课记录,分别用不同的方式完成
46 delete from stu_course
47 from student inner join stu_course on student.sno=stu_course.sno
48 where sdept='计算机系'
49 
50 delete from stu_course
51 where sno in
52 (
53 select sno
54 from student
55 where sdept='计算机系'
56 )
posted @ 2017-03-10 09:09  一条闲闲鱼  阅读(370)  评论(0编辑  收藏  举报