SQL之學生選課數據庫

1、E-R圖

2、關係模式

Student(Sno, Sname, Sage, Ssex, Sdept)
Course(Cno, Cname, Cpno, Ccredit)
SC(Sno, Cno,Grade)

3、實現SQL

/*建立學生表*/
create table Student(
    Sno varchar2(9) primary key,
    Sname varchar2(20) unique,
    Ssex varchar2(2),
    Sage integer,
    Sdept varchar2(20)
)
/*建立課程表*/
create table Course(
    Cno varchar(4) primary key,
    Cpno varchar(4),
    Cname varchar2(20),
    Ccredit integer,
    foreign key (Cpno) references Course(Cno)
    /*表級完整性約束,Cpno是外碼,被參照表是Course,被參照列是Cno*/
)
/*建立學生選課表*/
Create table SC(
    Sno varchar2(9),
    Cno varchar2(4),
    Grade integer,
    primary key(Sno,Cno),
    /*主碼有兩個屬性,必須作爲表級完整性進行定義*/
    foreign key (Sno) references Student(Sno),
     /*表級完整性約束,Sno是外碼,被參照表是Student,被參照列是Sno*/
    foreign key (Cno) references Course(Cno)
    /*表級完整性約束,Cno是外碼,被參照表是Course,被參照列是Cno*/
)

4、修改基本表

alter table <列名>
[ add <新列名> <數據類型> [完整性約束] ]
[ Drop [完整性約束名] ]
[ alter column <列名> <數據類型> ];

4.1 爲Student添加用戶密碼屬性列

alter table STUDENT add Spwd varchar2(20);

4.2 增加課程名取唯一值約束

alter table Course Add unique(Cname);

4.3 將Spwd的數據類型由varchar2(20)改爲varchar2(30)

alter table STUDENT alter column Spwd varchar2(30);

5、刪除基本表

drop table <表名> [ restrict|cascade ];

Restrict說明刪除是有條件的,cascade說明該表的刪除沒有任何限制。

6、數據查詢

select [ all|distinct ] [ <目標列表達式> ]
from <表名或者視圖名>
[ where <條件表達式> ]
[ group by  <列名1> [ having <條件表達式> ] ]
[ order by  <列名2> [ASC|DESC] ]

6.1 查詢全體學生的學號和姓名

select sno,sname from STUDENT t

6.2 查詢學生的姓名和出生年

select sname,(2013-Sage) as birthYear from STUDENT t

6.3 查詢學生的姓名和所在系名,並將系名轉換爲小寫

select sname,lower(sdept) from STUDENT t

6.4 消除取值重複行

select distinct cno from SC t

6.5 查詢GIS專業學生的學號和姓名

select sno,sname from  STUDENT where sdept='GIS'

6.6 查詢年齡小於25的學生的學號和姓名

select sno,sname from  STUDENT where sage<25

6.7 查詢年齡介於20-25之間的學生的學號和姓名

select sno,sname from  STUDENT where sage between 20 and 25

6.7 查詢年齡不介於20-25之間的學生的學號和姓名

select sno,sname from  STUDENT where sage not between 20 and 25

6.8 查詢GIS和RS系學生的學號和姓名

select sno,sname from  STUDENT where sdept in ('GIS','RS')

6.9  查詢不在GIS和RS系學生的學號和姓名

select sno,sname from  STUDENT where sdept not in ('GIS','RS')

6.10 字符匹配

[not] like '<匹配串>' [escape '<換碼字符>']
	%(百分號)代表任何長度的字符串;
	_(下劃線)代表任意單個字符。

6.11 排序查詢

[ order by  <列名2> [ASC|DESC] ]
	ASC爲升序,默認;
	DESC爲降序排序。

6.12 聚集函數

count ( [distinct|all] * )   //統計元組個數
count ( [distinct|all] <列名> )    //統計一列中值個數
sum ( [distinct|all] <列名> )  //計算某一列值的和
avg ( [distinct|all] <列名> )  //計算某一列值的平均值
max ( [distinct|all] <列名> )  //計算某一列值的最大值
min ( [distinct|all] <列名> )  //計算某一列值的最小值

6.12.1 查詢學生總人數

select count(*) as Scount from student

6.12.2 查詢GIS課程的平均成績

select avg(grade) as gisAvg from sc where cno=(select cno from course where cname='GIS')

6.12.2 查詢學生牛一的平均成績

select avg(grade) as Niu1Avg
  from sc, course
 where sno = (select sno from student where sname = '牛一')
   and sc.cno = course.cno

6.13 查詢各個課程的課程號與選課人數

select cno,count(sno) from SC group by cno

6、複合查詢

6.14 查詢選修2號課程且成績在80分以上的學生

select student.sno as sno, student.sname as sname
  from student, sc
 where student.sno = sc.sno
   and sc.cno = '2'
   and sc.grade > 80

6.15 查詢每個學生的學號、姓名、選修課程名以及成績

select student.sno   as sno,
       student.sname as sname,
       course.cname  as cname,
       sc.grade      as grade
  from student, course, sc
 where student.sno = sc.sno
   and sc.cno = course.cno

6.16 查詢和牛一在同一個系的學生

select sno, sname   from STUDENT  where sdept = (select sdept from STUDENT where sname = '牛一')

6.17 查詢選修了GIS課程的學生

select sno, sname
  from STUDENT
 where sno in
       (select sno
          from sc
         where cno in (select cno from course where cname = 'GIS'))

7、數據更新

7.1 插入數據

insert into < 表名 > [ ( < 屬性1 >[ ,< 屬性2 > ...) ]
 values ( < 常量1 > [ ,< 常量2 > ...)

7.2 修改數據

 update < 表名 >
    set < 列名 >= < 值 > [, < 列名 >= < 值 > ] 
[ where < 條件 > ]

7.3 刪除數據

delete from < 表名 > [ where < 條件 > ]

附錄:

視圖:從一個或者幾個基本表(視圖)導出的表,他是一個虛表。

1、建立視圖

create view < 表名 > [ ( < 列名 > ,< 列名 > ] 
as < 子查詢 >
[ with check option ]

2、刪除視圖

drop view < 視圖名 > [ cascade ];

3、視圖的其他操作

視圖的其他操作與表的操作類似。

4、視圖的作用

1、簡化用戶操作; 2、使用戶能以多種角度看同一數據; 3、對重構數據庫提供了一定的邏輯獨立性; 4、對機密數據提供安全保護; 5、適當使用視圖可以更清楚的表達查詢。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章