1. 程式人生 > >資料庫系統概述--關係資料庫標準語言SQL

資料庫系統概述--關係資料庫標準語言SQL

第三章 關係資料庫標準語言SQL

1.SQL功能

數00據查詢select

資料定義create drop alter

資料操縱 insert update delete

資料控制 grant revoke

 

2.資料定義

a.模式的定義和刪除

為使用者zhang建立一個模式test

create scheme test authorization ZHANG

刪除模式test

drop scheme test cascade(級聯)

drop scheme test restrict(限制)

 

b.基本表的定義、刪除與修改

建立一個學生表Student

Create table student(

  Sno char(9) primary key,

  Sname char(20) unique,

  Ssex char(2),

  Sage smallint,

  Sdept char(20)

);

建立一個課程表Course

Create table course(

  Cno char(4) primary key,

  Cname char(40) not null,

  Cpno char(4),

  Ccredit smallint,

  Foreign key(cpno) preferences course(Cno)

);

建立學生選課表SC

Create table sc(

  Sno char(9),

  Cno char(4),

  Grade smallint,

  Primary key(Sno,Cno),

  Foreign key(Sno) references student(Sno),

  Foreign ket(Cno) references course(Cno)

);

刪除student表

Drop table student cascade

 

c.索引建立、修改與刪除

為學生-課程資料庫中的student,course和sc三個表建立索引。其中student表按學號升序建唯一索引,course表按課程號升序建唯一索引,sc表按照學號升序和課程號降序建唯一索引。

Create unique index stusno on student(sno);

Create unique index coucno on course(cno);

Create unique inde  scno on sc(sno asc,cno desc)

將SC表的scno索引名改為scsno

Alter index scno rename to scsno

刪除student表的stusname索引

Drop index stusname

 

3.資料查詢

查詢全體學生的學號與姓名

Select sno,sname from student;

查詢全體學生的姓名、學號、所在系

Select sname,sno,sdept from student;

查詢全體學生的詳細記錄

Select * from student;

查詢全體學生的姓名及其出生年份

Select sname,sbirth from student;

查詢全體學生的姓名、出生年份和所在的院系

Select sname,sbirth,lower(sdept) from student;

查詢選修了課程的學生學號

Select discinct sno from sc;

查詢計算機科學系全體學生的名單

Select sname from student where sdept=’CS’

查詢所有年齡在20歲以下的學生姓名及其年齡

Select sname,sage from student where sage<20;

查詢考試成績不及格的學生的學號

Select distinct sno from sc where grade<60

查詢年齡在20-23歲之間的學生姓名、系別和年齡

Select sname,sdept,sage from student where sage between 20 and 23

查詢年齡不在20-23歲之間的學生姓名、系別和年齡

Select sname,sdept,sage from student where sage not between 20 and 23

查詢計算機科學系(CS)、數學系(MA)和資訊系(IS)學生的姓名和性別

Select sname,ssex from student where sdept in(‘CS’,’MA’,’IS’);

查詢既不是計算機科學系、數學系,也不是資訊系的學生的姓名和性別

Select sname,ssex from student where sdept not in(‘CS’,’MA’,’IS’);

查詢學號為201215121的學生的詳細情況

Select * from student where sno like ‘201215121’

查詢所有姓劉的學生的姓名、學號和性別

Select sname,sno,ssex from student where sname like ‘劉%’

查詢姓歐陽且全名為三個漢字的學生的姓名

Select sname from student where sname like ’歐陽_’

查詢名字中第二個字為陽的學生的姓名和學號

Select sname,sno from student where sname like ’_陽%’

查詢所有不姓劉的學生的姓名、學號和性別

Select sname,sno,ssex from student where sname not like ‘劉%’

查詢DB_Design課程的課程號和學分

Select cno,ccredit from course where cname like ‘DB\_Design’ escape ‘\’

查詢以“DB_”開頭,且倒數第三個字元為i的課程的詳細情況

Select * from course where cname like ‘DB\_%i__’ escape ‘\’

某些學生選修課程後沒有參加考試,所以有選課記錄,但沒有考試成績,查詢缺少成績的學生的學號和相應的課程號

Select sno,cno from sc where grade is null

查所有有成績的學生學號和課程號

Select sno,cno from sc where grade is not null

查詢計算機科學系年齡在20歲以下的學生姓名

Select sname from student where sdept=’cs’ and sage<20

查詢選修了3號課程的學生的學號及其成績,查詢結果按分數的降序排列

Select sno,grade from sc where cno=’3’ order by grade desc

查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系中的學生按年齡降序排列

Select * from student order by sdept,sage desc;

查詢學生總人數

Select count(*) from student

查詢選修了課程的學生人數

Select count(distinct sno) from sc

計算選修1號課程的學生平均成績

Select avg(grade) from sc where cno=’1’

查詢選修1號課程的學生最高分數

Select max(grade) from sc where cno=’1’

查詢學生201215012選修課程的總學分數

Select sum(credit) from sc,course where sno=’201215012’ and sc.cno=course.cno

求各個課程號及其相應的選課人數

Select cno,count(sno) from sc group by cno

查詢選修了三門以上課程的學生學號

Select sno from sc group by sno having count(*)>3

查詢平均成績大於等於90分的學生學號和平均成績

Select sno,avg(grade) from sc group by sno having avg(grade)>=90

查詢選修2號課程且成績在90分以上的所有學生的學號和姓名

Select student,sno,sname from student,sc where student.sno=sc.sno and sc.cno=’2’ and sc.grade>90

查詢與“劉晨”在同一個系學習的學生

Select sno,sname,sdept from student where sdept in( select sdept from student where sname=’劉晨’)

Select s1.sno,s1.sname,s1.sdept from student s1,student s2 where s1.sdept=s2.sdept and s2.sname=’劉晨’

查詢選修了課程名為“資訊系統”的學生學號和姓名

Select student.sno,sname from student,sc,course where student.sno=sc.sno and sc.cno=course.cno and course.cname=’資訊系統’

找出每個學生超過他自己選修課程平均成績的課程號

Select sno,cno from sc x where grade>=(select avg(grade) from sc y  where y.sno=x.sno)

查詢選修了課程1或者選修了課程2的學生

Select sno from sc where cno=’1’ union select sno from sc where cno=’2’

查詢計算機科學系的學生與年齡不大於19歲的學生的交集

Select * from student where sdept=’cs’ intersect select * from student where sage<=19

查詢計算機科學系的學生與年齡不大於19歲的學生的差集

Select * from student where sdept=’cs’ except select * from student where sage<=19

 

4.資料更新

插入操作:

將一個新學生元組(學號:201215128,姓名:陳東,性別:男,所在系:IS,年齡:18歲)插入到student表中

Insert into student(sno,sname,ssex,sdept,sage) values(‘201215128’,’陳東’,’男’,’IS’,18)

將學生張成民的資訊插入到student表中

Insert into student values(‘201215126’,’張成民’,’男’,18,’CS’)

插入一條選課記錄(‘201215128’,’1’)

Insert into sc(sno,cno) values(‘201215128’,’1’)

對每一個系,求學生的平均成績,並把結果存入資料庫

create table dept_age( sdept char(15) avg_age smallint);

insert into dept_age(sdept,avg_age) selct sdept,avg(sage) from student group by sdept;

 

修改資料

將學生201215121的年齡修改為22歲

update student set sage=22 where sno=’201215121’

將所有學生的年齡都增加1歲

update student set sage=sage+1;

將計算機科學系全體學生的成績置0

update sc set grade=0 where sno in (select sno from student where sdept=’cs’)

 

刪除記錄

刪除學號為201215128的學生記錄

delete from student where sno=’201215128’

刪除所有學生的選課記錄

delete from sc;

刪除計算機科學系所有學生的選課記錄

delete from sc where sno in(select sno from student where sdept=’cs’)

 

5.檢視的建立、修改和刪除

建立檢視

建立資訊系學生的檢視

create view is_student as select sno,sname,sage from student where sdept=’is’

建立資訊系學生的檢視,並要求進行修改和刪除操作時仍需保證該檢視只有資訊系的學生

create view is_student as select sno,sname,sage from student where sdept=’is’ with

 check_option

建立資訊系選修了1號課程的學生的檢視(包括學號、姓名、成績)

create view is_si(sno,sname,grade) as select student.sno,sname,grade from student,sc where sdept=’is’ and student.sno=sc.sno and sc.cno=’1’

建立資訊系選修了1號課程且成績在90分以上的學生的檢視

create view is_s2 as select sno,sname,grade from is_s1 where grade>90

將學生的學號及平均成績定義為一個檢視

create view s_g(sno,gavg) as select sno,avg(grade) from sc group by sno;

 

刪除檢視

刪除檢視BT_S和檢視IS_S1

drop view bt_s

drop view is_s1 cascade