/* 学生表上创建视图*/createview IS_STUDENT
asselect Sno,Sname,Sage
from STUDENT
where Sdept='IS';/* 因为还有其他表依赖于学生表,所以此语句会删除失败*/droptable STUDENT restrict;/* 此语句是级联删除,可以强制删除*/droptable STUDENT cascade;/* 在视图上查询*/select*from IS_STUDENT;
4. 索引操作
/* 建立索引*/createuniqueindex Stusno on STUDENT(Sno);createuniqueindex Coucno on course(Cno);createuniqueindex SCno on SC(Sno asc, Cno desc);/* 修改索引名,(此方法错误)*/alterindex SCno renameto SCSno;/* 此方法能修改索引名*/exec sp_rename "SC.SCno","SCSno","index";/* 删除索引*/dropindex Studsname;
二、查询
1. 简单查询
/* 查询学生表中的全部成员*/select Sno, Sname from STUDENT;/* 查询学生表中的全部成员*/select Sname, Sno, Sdept from STUDENT;/* 查看表中的所有数据*/select*from STUDENT;/* 列名可以进行基本运算,可以重命名*/select Sname,2022-Sage birth from STUDENT;/* 可以添加字符串行,简单函数*/select Sname,'Year of birth' YearOfBirth,2022-Sage, lower(Sdept)from STUDENT;/* 选择表中的若干元组*/select Sno from SC;/* 使用唯一表示符选择元组*/selectdistinct Sno from SC;
2. 条件查询
/* 条件选择*/select Sname from STUDENT where Sdept='CS';select Sname, Sage from STUDENT where Sage <20;selectdistinct Sno from SC where Grade <60;
3. Between and 用法
/* betwwen and 用法*/select Sname, Sage from STUDENT where Sage between19and20;select Sname, Sno from STUDENT where Sage notbetween19and20;
4. In 用法
/* in 用法*/select Sname, Sdept from STUDENT where Sdept in('CS','IS');select Sname, Sdept from STUDENT where Sdept notin('CS','IS');
5. Like 用法
/* like 用法*/select*from STUDENT where Sno like'201201';select*from STUDENT where Sno ='201201';select Sname, Sno, Sex from STUDENT where Sname like'刘%';select Sname, Sno, Sex from STUDENT where Sname like'刘_';select Sname from STUDENT where Sname like'__三';/* 转义字符*/select Cno, Cname from Course where Cname like'DB\_%'escape'\';
select Cno, Cname from Course where Cname like 'DB_%';
6. Null 语句
/* null 语句*/select Sno, Cno from SC where Grade isnull;select Sno, Cno from SC where Grade isnotnull;
7. and 和 or 语句
/* and 语句*/select Sname, Sage from STUDENT where Sdept ='CS'and Sage <=20;/* or 语句*/select Sname, Sdept from STUDENT where Sdept ='CS'or Sdept ='MA';
8. order 语句
/* order 语句*/select Sno, Grade from SC where Cno ='3'orderby Grade desc;/* 建立索引时用到升降序*/select Sno, Grade from SC where Cno ='3'orderby Grade asc;select*from STUDENT orderby Sdept, Sage desc;
9. 聚集函数使用
/*聚集函数*/selectcount(*)from STUDENT;selectavg(Grade)from SC where Sno ='201222';selectsum(Grade) average from SC where SC.Sno ='201222';selectmax(Grade)from SC;selectmin(Grade)from SC;selectsum(Grade)from SC;selectcount(distinct Sno)from SC;selectSUM(Ccredit)from SC,Course
where SC.Sno ='201222'and SC.Cno = Course.Cno;select*from SC,Course where SC.Cno = Course.Cno;
10. Group 用法
/* group 语句*/select Cno,count(Sno)from SC groupby Cno;select Sno from SC groupby Sno havingcount(*)>2;select Sno,avg(Grade)from SC groupby Sno havingavg(Grade)>=90;
11. 多表连接
/* 自然连接*/select*from STUDENT, SC where STUDENT.Sno = SC.Sno;/* 自身连接*/select first1.Cno, first1.Cname, second1.Cpno, second1.Cname
from Course first1, Course second1
where first1.Cpno = second1.Cno;/*多表连接*/select STUDENT.Sno, Sname, Cname, Grade
from STUDENT, SC, Course
where STUDENT.Sno = SC.Sno and SC.Cno = Course.Cno;
12. 嵌套查询
/* 嵌套查询*/select Sname from STUDENT where Sno in(select Sno from SC where Cno ='2');/* 不相关查询*/select Sno, Sname, Sdept from STUDENT where Sdept in(select Sdept from STUDENT where Sname ='刘晨');/* 相关子查询*/select Sno, Sname from STUDENT where Sno in(select Sno from SC where Cno in(select Cno from Course where Cname ='信息系统'));
13. 带有比较用算符的查找
/* 带有比较运算符的子查询*/select Sno, Sname, Sdept from STUDENT where Sdept =(select Sdept from STUDENT where Sname ='刘晨');select Sno, Cno from SC x where Grade >=(selectavg(Grade)from SC y where y.Sno = x.Sno);
14. 带有 any 、all 谓词的查询
/* 带有any、all 谓词的子查询*/select Sname, Sage from STUDENT where Sage <any(select Sage from STUDENT where Sdept ='CS')and Sdept !='CS';select Sname, Sage from STUDENT where Sage <(selectmax(Sage)from STUDENT where Sdept ='CS')and Sdept !='CS'
15. 带有 exists 谓词chaxun
/* 带有exists 谓词的子查询*/select Sname from STUDENT whereexists(select*from SC where Sno = STUDENT.Sno and Cno ='1');select Sname from STUDENT wherenotexists(select*from SC where Sno = STUDENT.Sno and Cno ='1');select Sno, Sname, Sdept from STUDENT S1 whereexists(select*from STUDENT S2
where S2.Sdept = S1.Sdept and S2.Sname ='刘晨');
16. 存在两次改写全称量词、蕴含式
/* 存在量词写全称量词*/select Sname from STUDENT wherenotexists(select*from Course wherenotexists(select*from SC where Sno = STUDENT.Sno and Cno = Course.Cno));/* 存在量词改写蕴含式*/selectdistinct Sno from SC SCX wherenotexists(select*from SC SCY where SCY.Sno ='2012222'andnotexists(select*from SC SCZ where SCZ.Sno = SCX.Sno and SCZ.Cno = SCY.Cno));
17. 集合查询
/* 集合查询*/select*from STUDENT where Sdept ='CS'unionselect*from STUDENT where Sage <=19;/* 交集*/select*from STUDENT where Sdept ='CS'intersectselect*from STUDENT where Sage <=19;/* 差集*/select*from STUDENT where Sdept ='CS'exceptselect*from STUDENT where Sage <=19;select*from STUDENT where Sdept ='CS'and Sage >19;
18. 基于派生词查询
/* 基于派生词的查询(from 语句中写 select)*/select Sname from STUDENT,(select Sno from SC where Cno ='1')as SC1 where STUDENT.Sno = SC1.Sno;