(‘123',' Tony','27406483','NewYork') Go
Insert saler
( saler_id , saler_name , phone , address ) Values
(‘2366',' Tony','23328998','Sydney')
8.修改SALE数据库中saler表指定的saler_id 为123记录的值,指定其saler_id 为1234,saler_name 为 Jonny,phone 为22228888 ,address 为 ShangHai ; Use SALE Go
UPDATE saler
SET saler_id = ‘1234' ,
saler_name = ‘Jonny ‘ , phone = ‘22228888' , address = ‘ShangHai' , WHERE saler_id = ‘123' go
9.删除saler表中所有电话为23328998的记录; Use SALE Go
DELETE FROM saler
WHERE phone = ‘23328998' Go
10. 先删除表saler中的所有数据,然后删除表saler,最后将SALE数据库删除; Use SALE truncate saler go
DROP TABLE saler Go
DROP DATABASE SALE GO
十、完成下列操作:
1、 向Student表增加“入学时间”列,其数据类型为日期型。 ALTER TABLE Student ADD Scome DATE; 2、删除Student表
DROP TABLE Student ; 3、查询全体学生的学号与姓名 SELECT Sno, Sname FROM Student;
4、查所有选修过课的学生的学号 SELECT DISTINCT Sno
FROM SC;
5、查所有年龄在20岁以下的学生姓名及其年龄 SELECT Sname, Sage FROM Student WHERE Sage <20;
6、查所有姓刘的学生的姓名、学号和性别 SELECT Sname, Sno, Ssex FROM Student
WHERE Sname LIKE '刘%';
7、查姓“欧阳”且全名为三个汉字的学生的姓名 SELECT Sname FROM Student
WHERE Sname LIKE '欧阳__';
8、计算1号课程的学生平均成绩 SELECT AVG(Grade) FROM SC
WHERE Cno='1';
9、查询学习1号课程的学生最高分数 SELECT MAX(Grade) FROM SC
WHERE Cno='1';
10、将一个新学生记录(学号:95020;姓名:陈冬;性别:男;所在系:IS;年龄:18岁)插入Student表中 INSERT
INTO Student
VALUES ('95020', '陈冬', '男', 'IS', 18); 11、将学生95001的年龄改为22岁 UPDATE Student SET Sage=22
WHERE Sno='95001';
12、删除学号为95019的学生记录 DELETE
FROM Student
WHERE Sno=‘95019’;
13、删除计算机科学系所有学生的选课记录 DELETE FROM SC WHERE 'CS'= (SELETE Sdept FROM Student
WHERE Student.Sno=SC.Sno);
十一、以书中教务管理数据库为例,用T-SQL语句完成完成下列查询功能: ?1.显示每个学生的学号、姓名、班级、课程名称、成绩及总分。
select
student.student_id,student.student_name,student.class_id,course.course_name,student_course.grade from student,course,student_course
where student.student_id=student_course.student_id and course.course_id=student_course.course_id order by student.student_id
compute sum(grade) by student.student_id
?2.显示各班的各门功课的最高分、最低分和平均分。
Select student.class_id,max(grade),min(grade),avg(grade) from student,student_course Where student.student_id=student_course.student_id Group by student.class_id Order by student.class_id
?3.检索84年、85年出生的学生姓名、班级、性别、年龄和出生日期。
select student_name,class_id,sex,2007-year(birth)as age,birth from student where year(birth)=1984 or year(birth)=1985
?4.显示各位教师的姓名、任教课程及相应班级及上课地点。 select
teacher.teacher_name,course.course_name,teacher_course_class.class_id,teacher_course_class.course_classroom
from teacher,course,teacher_course_class where teacher.teacher_id=teacher_course_class.teacher_id and teacher_course_class.course_id=course.course_id
?5.统计不同职称的教师的人数,并显示教师姓名、职称。 select teacher_name,profession from teacher
order by profession
compute count(profession) by profession
?6.显示上海、南京、北京等地的学生姓名、地址,并统计相应的人数。 select student_name,home_addr from student
where home_addr like '%上海%' or home_addr like '%南京%'or home_addr like '%北京%' compute count(student_name) ?7.显示书名,出版社,作者及价格;
select book_name,publish_company,author,price from book ?8.将所有书的价格打九折显示;
select book_name,publish_company,author,price*0.9 from book ?9.显示84年以后出生的学生的姓名,性别,班级及出生日期; select student_name,sex,class_id,birth from student where year(birth)>1984
?10.显示g99402班的男生的所有信息;
select * from student where class_id='g99402' and sex='1' ?11.按出生日期排序显示学生的姓名,性别,出生日期及住址; select student_name,sex,birth,home_addr from student order by birth
?12.显示每个学生的学号,总分;
select student_id,sum(grade) from student_course group by student_id
?13.按学号排序显示每个学生的学号,姓名,课程号及成绩; select
student.student_id,student.student_name,student_course.course_id,student_course.grade from student,student_course
where student.student_id=student_course.student_id order by student.student_id
?14.显示每个学生的学号,姓名,课程名称及成绩; .select
student.student_id,student.student_name,course.course_name,student_course.grade from student,course,student_course where student.student_id=student_course.student_id and course.course_id=student_course.course_id order by student.student_id
?15.显示成绩不及格,及80分以上的学生学号,姓名,课程名称及成绩; Select
student.student_id,student.student_name,student.class_id,course.course_name,student_course.grade from student,course,student_course where student.student_id=student_course.student_id and course.course_id=student_course.course_id and (grade<60 or grade>=80) order by student.student_id,grade
?16.显示不同班级的各课程的最高分,最低分及平均分;
select class_id,max(grade)as max,min(grade) as min ,avg(grade) as avg from student,student_course
where student.student_id=student_course.student_id group by class_id order by class_id
?17.显示林红同学不选的课程号; select course_id from course
where course_id not in(select course_id from student_course,student where student.student_id=student_course.student_id and student.student_name='林红') 18 显示成绩都大于80分的学生的学号 Select student_id from student_course Group by student_id having min(grade)>80
十二、某科研部门为了进行科研情况管理,设计了如下三个数据表:
科研课题表KYKT:KTID(课题编号).KTNAME(课题名称).KTJF(课题经费)(万元计) 科研情况表KYQK:RYID(人员编号).KTID(课题编号).SBF(设备费).GZL(工作量:日).SYJF(使用经费)
科研人员表KYRY:RYID(人员编号).RYNAME(人员姓名).ZC(职称).SEX(性别).ADDR(地址)
按要求写出SQL查询语句:
(1) 查询课题经费大于100万元的课题编号及其名称。 SELECT KTID,KTNAME FROM KYKT WHERE KTJF>100
(2) 查询同时参加编号为101和105课题的科研人员编号。
SELECT K1.RYID FROM KYQK AS K1,KYQK AS K2 WHERE K1.RYID=K2.RYID AND K1.KTID=’101’AND K2.KTID=’105’ 或
SELECT RYID FROM KYQK WHERE KTID=’101’ AND RYID IN (SELECT RYID FROM KYQK WHERE KTID=’105’)
(3) 查询科研情况表中所有科研人员的编号。 SELECT DISTINCT RYID FROM KYQK (4) 检索67号科研人员的信息。 SELECT * FROM KYRY WHERE RYID=’67’ (5) 检索102号课题的平均工作量
SELECT AVG(GZL) FROM KYQK WHERE KTID=’102’
(6) 检索工作量在300-400日课题编号.人员编号及工作量.并按工作量降序排序。 SELECT KTID,RYID,GZL FROM KYQK WHERE GZL BETWEEN 300 AND 400 (7) 在科研人员中追加一个科研人员(53,DAIHONG,DOTOR,MALL,TIAJIN)。 INSERT INTO KYRY VALUES(‘53’,DAIHONG,DOCTOR,MAIL,TIANJING) (8) 删除58号科研人员的所有科研课题。 DELECT FROM KYQK WHERE KYID=’58’ (9) 修改203号课题的课题经费为100万元。 UPDATE KYKT SET KTJF=100 WHERE KTID=’203’ (10)
把69号科研人员的工作量减少10% 。
UPDATE KYQK SET GZL=0.9*GZL WHERE RYID=’69’