数据库总结作业SQL操作语句三

一.建立基本表

例如:

1.
create table sc

(sno varchar(20),

cno varchar(20),

grade int,//规范格式

primary key(sno,cno),//主码

foreign key (cno) references course(cno),//外码

foreign key (sno) references student(sno)

);
2.插入值
1.insert into course(cno,cname) values(7,'java');//插入一条
2.insert into course(cno,cname) values(8,'c'),(9,'离散');//插入两条及多条记录
3.delete from course where cno=8;//删除某一元组
4.insert into course values(8,'c');//错误!原来有n=4列,未指明插入哪一列。
5.insert into course values(8,'c',1,4);//这样可以
6. update course set ccredit=6 where cno=9;//更改某一元组值
7. update course set ccredit=ccredit+1;//更改全部元组ccredit值
8.select * from student where sdept='计算机' intersect select* from student where sage<20;//计算机系与年龄不超20交集
9.select  sno from sc where cno=1 intersect select  sno from sc where cno=2;//即选修1又选修2的人
10.select  a.sno from sc a,sc b where a.sno=b.sno and a.cno=1 and b.cno=2;//上题也可以利用自身连接做

二.修改基本表

1.增加一新列:alter table  sc  add entryTime DATE;

2.修改某一列数据类型:alter table sc alter column grade int;

3.增加完整性约束:alter table sc add unique(sno);

4.删除某一列:alter table sc drop column grade;

5.删除基本表:drop table sc cascade;

三.索引操作

1.CREATE UNIQUE INDEX  Stusno ON Student(Sno)

2.CREATE UNIQUE INDEX  Coucno ON Course(Cno)

3.CREATE UNIQUE INDEX  SCno ON SC(Sno ASC,Cno DESC);//在sc上建立索引,按学号升序,课程号降序;

4.修改索引名:alter index SCno rename to fun;//将索引名SCno更改;

5.删除索引:drop index fun;

四.数据字典

包含了:

1.关系模式定义

2.视图定义

3.索引定义

4.完整性约束定义

5.各类用户对数据库的操作权限

6.统计信息等

五.单表查询

一.查询列
1.select sno,sname from student;
2.select * from student;//查询所有学生全部信息
二.查询经过计算值
1.select sname,2018-sage from student;
2.select sname,2018-sage date from student;//将查询到的年龄值所在列给予特定名字
3.select sname,'date',2018-sage,upper(sdept) from student;//将系大写,多显示一列'date';
三.选择若干元组
1.select sno from sc;
2.select distinct sno from sc;//去重
3.select sno from student where sdept='计算机';//计算机系
4.select sname from student where sage<20;
5.select distinct sno,cno from sc where grade<60;//distinct 有时候一定要注意!
6.select sname,sage from student where sage between 18 and 20;
7.select sname,sage from student where sage not between 18 and 20;
8.select sname,sage from student where sdept in ('计算机','通信');//in!,特别注意一下
9.select sname,sage from student where sdept not in ('计算机','通信');//in!
10.select sname from student where sname like'王%';//查询王姓同学,不限制长度
11.select sname from student where sname like'王_';//查询姓王且只有两个字的同学
12.select sname from student where sname like'_阳';//查询第二个字为阳的同学
13.SELECT Cno,Ccredit FROM  Course WHERE  Cname LIKE 'DB\_Design' ESCAPE '\ ';
查询DB_Design课程的课程号和学分。
14.SELECT  * FROM    Course WHERE  Cname LIKE  'DB\_%i_ _' ESCAPE '\ ' ;
查询以"DB_"开头,且倒数第3个字符为 i的课程的详细情况。
15.select sno,cno from sc where grade is null;//查询grade为空的学号和课程号;
16.select sno,cno from sc where grade is not null;//查询所有有成绩的...
四.order by字句
1.select sno,grade from sc where cno=3 order by grade desc;//选修了3号课程且成绩降级排列
2.select * from student order by sdept,sage desc;//系号升序,同系按年龄降序排列
五.聚集函数
1.select count(*) from student;//查询学生总人数
2. select count(distinct sno ) from sc;//查询选修课程总人数,注意distinct;
3.select avg(grade) from sc where cno=1;//选修1号课程平均分
4.select max(grade) from sc where cno=3;//max函数作用于几个特定元组
5.select sum(credit) from sc,course where sno=20171789 and sc.cno=course.cno;//总学分
6.select sno,avg(grade) from sc group by sno having avg(grade)>89;//查询平均成绩大于89的
7.

      

六.连接查询

一.等值非等值连接查询
1.select student.*,sc.* from student,sc where sc.sno=student.sno;//选修情况
2.select sname,sc.sno from student,sc where sc.sno=student.sno and cno=3 and grade>90;
二.自身连接
1.select fi.cno,se.cpno from course fi,course se where fi.cpno=se.cno;//选修课的选修课
2.SELECT Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade
    FROM  Student  LEFT OUT JOIN SC ON    
                 (Student.Sno=SC.Sno); //左外连接
3.select student.sno,sname,course.cname,grade from student,course,sc where student.sno=sc.sno and sc.cno=course.cno//多表连接;
三.嵌套查询
1.SELECT Sno, Sname, Sdept
    	FROM Student
   	WHERE Sdept  IN
                  (SELECT Sdept
                   FROM Student
                   WHERE Sname= ' 刘晨 ');//与刘晨相同系学生
2.select s1.sno,sname,sdept from student s1,student s2 where s1.sdept=s2.sdept and s2.sname='刘晨';
3.select sname,sno from student where sno in(select sno from sc where cno in(select cno from course where sname='数学'));
4. select sname,sno from student where sno in(select sno from sc where cno in(select cno from course where cname='数学'));//查询选修了课程名为数学信息
5.select sname,student.sno from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and cname='数学';//同上,利用连接查询;
6.select sno,cno from sc x  where grade >=(select avg(grade) from sc y  where x.sno=y.sno);//相关子查询;
7.create view iiii (sno,avg) as select sno,avg(grade) from sc group by sno;//先建立每个人及其对应选修课平均成绩表
select sc.sno ,cno from sc,iii where sc.sno=iii.sno and sc.grade>=avg;
再连接选择
四.ANY、ALL查询
//ANY任一、ALL全部
1.select sname,sage from student where sage<any(select sage from student where sdept='计算机')and sdept<>'计算机';//查询非计算机科学系中比计算机科学系任意一个学生年龄小的学生姓名和年龄
2.select sname,sage from student where sage<(select max(sage) from student where sdept='计算机') and sdept<>'计算机';//利用聚集函数实现;
3.select sname,sage from student where sage<all(select sage from student where sdept='计算机') and sdept<>'计算机';//小于最小值
4.select sname,sage from student where sage<(select min(sage) from student where sdept='计算机') and sdept<>'计算机';//利用聚集函数实现;
五.EXISTS、NOT EXISTS查询
1.select sname from student,sc  where student.sno=sc.sno and cno=1;
转化成select sname from student where exists(select * from sc where sno =student.sno and cno=1);
2.select sname from student where not exists (select * from sc where sno =student.sno and cno=1);//未选修1号课程
333.select sno,sname,sdept from student s1 where exists(select * from student s2 where s1.sdept=s2.sdept and s2.sname='冯硕');//查询与我同系学生信息
444.select sname from student where not exists(select * from course where not exists(select * from sc where sno=student.sno and cno=course.cno));//查询选修了全部课程人姓名//!!!!重点
555.select distinct sno from sc x where not exists(select * from sc y where y.sno=20171789 and not exists(select * from sc z where z.sno=x.sno and z.cno=y.cno));//至少选修了学号为..选修全部课程人信息
六.集合查询
1. select * from student where sdept='计算机'union select * from student where sage<20;//计算机系及年龄小于20人信息
2.select sno from sc where cno=1 union select sno from sc where cno=2;//不能用and选修了课程1和课程2
UNION:将多个查询结果合并起来时,系统自动去掉重复元组
UNION ALL:将多个查询结果合并起来时,保留重复元组 
七.派生表查询
1. select sno,cno from sc,(select sno,avg(grade) from sc group by sno)as avg_sc(avg_sno,avg_grade)where sc.sno=avg_sc.sno and grade>=avg_sc.avg_gade;//查询成绩超过自己平均成绩
2. select sname from student ,(select sno from sc where cno=1 )ac sc6 where student.sno=sc6.sno;//选修1号课程
八.建立视图
1.//查询只选修了2,1号课程的人
create view  ooop(snoo,num) as(select sno,count(*) from sc group by sno);
select ooop.snoo from ooop,sc where ooop.snoo=sc.sno and  ooop.num=2 and cno=1 and sc.sno in(select sno from sc where cno=2);






    

猜你喜欢

转载自blog.csdn.net/lanshan1111/article/details/83755703
今日推荐