1. 程式人生 > >資料庫總結作業SQL操作語句三

資料庫總結作業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);