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

来源:互联网 发布:mac能玩什么单机游戏 编辑:程序博客网 时间:2024/05/22 00:53

数据库笔记—7—SQL sever2008更新数据

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

1.

插入数据 两种语句格式
insert…values语句

--1.向depart表中插入一行数据('5','计划部','008','2206')insert into departvalues('5','计划部','008','2206')
--向worker表中插入一行数据,职工号:010,职工名:李飞龙,生日:1967-04-01,部门号:4insert into worker(wid,wname,wbirthdate,depid)  --指定字段values('010','李飞龙','1967-04-01','4')
--向salary表中插入一行数据,职工号:010,totalsalary:2800insert into salary(wid,totalsalary) --错误的,有两个主键,,需要全部不为空values('010',2800) --数字类型不加引号insert into salary(wid,sdate,totalsalary)values('010','2011-01-04',2800) --数字类型不加引号

–insert…select语句 从某张张表中查询的数据,放到另外一张表中

--创建一个新表worker_f,然后将worker表中所有女职工的职工号,职工名,出生日期这三个字段的信息插入进入create table worker_f(wid char(3) primary key,wname varchar(10) not null,wbirthdate date)insert into worker_fselect wid,wname,wbirthdatefrom workerwhere wsex='女'

修改数据 update

--修改worker表中的数据,将姓名为‘李飞龙’的职工的性别修改为‘男’  基于单表update workerset wsex='男'where wname='李飞龙'--将1975年以前出生的职工20111月份的totalsalary增加500元,actualsalary增加400元 基于多表--利用多表连接查询update salaryset totalsalary=totalsalary+500,actualsalary=actualsalary+400from worker inner join salary on worker.wid=salary.widwhere YEAR(wbirthdate)<1975 and YEAR(sdate)=2011 and MONTH(sdate)=1--利用子查询update salaryset totalsalary=totalsalary+500,actualsalary=actualsalary+400where YEAR(sdate)=2011 and MONTH(sdate)=1 and wid in(select widfrom workerwhere YEAR(wbirthdate)<1975)

删除数据 delete from

--删除李飞龙的信息delete from workerwhere wname='李飞龙'
--删除余慧的工资信息--利用多表连接查询delete from salaryfrom worker inner join salary on worker.wid=salary.widwhere wname='余慧'--利用子查询delete from salarywhere wid=(select widfrom workerwhere wname='孙华')

2.

更新数据库综合操作

--1.将一个新学生记录(学号:95020;姓名:陈冬;性别:男;所在系:英语;出生日期:1996-03-02;)插入到student表中insert into studentvalues('95020','陈冬','男','1996-03-02','英语系')
--2.插入一条选课记录('95020','1')insert into stu_course(sno,cno)values('95020','1')
--3.在数据库中建立一个有两属性列的新表,其中一列存放系别,另一列存放相应系的平均年龄create table avg_sdept(sdept varchar(30) primary key,avgage int)
--4.对数据库的student表按系分组求平均年龄,再把系别和平均年龄存放新表中insert into avg_sdeptselect sdept,AVG(YEAR(getdate())-YEAR(sbirth)) as 平均年龄from studentgroup by sdept
--5.将学生95001的出生日期修改为:1997-09-01update studentset sbirth='1997-09-01'where sno='95001'
--6.将计算机系全体学生的成绩修改为:0,分别用不同的方式完成--多表链接查询update stu_courseset grade=0from student inner join stu_course on student.sno=stu_course.snowhere sdept = '计算机系'--子查询update stu_courseset grade=0 where sno in(select sdeptfrom studentwhere sdept = '计算机系')
--7.删除第三张新建表中的所有信息delete from avg_sdept
--8.删除学号为‘95020’的学生记录delete from studentwhere sno='95020'
--9.删除计算机系所有学生的选课记录,分别用不同的方式完成delete from stu_coursefrom student inner join stu_course on student.sno=stu_course.snowhere sdept='计算机系'delete from stu_coursewhere sno in(select snofrom studentwhere sdept='计算机系')
0 0