create database Example 創建數據庫
create table Student 學生表
(
StdNo int identity(200301,1) primary key not null,
Sname varchar(20) not null,
Sspec varchar(10) not null,
Class char(4) not null
)
create table Grade 成績表
(
StdNo int not null,
CouseNo char(2) not null,
grade real null
)
create table Couse 課程表
(
CouseNo char(2)primary key not null,
CouseName char(50) unique null
)
alter table Grade 向成績表中加約束
add constraint cn_grade
check
(grade>=0 and grade<=100)
alter table Grade 向成績表中加外鍵
add constraint pk_CouseNo
foreign key (CouseNo)references Couse(CouseNo)
alter table Grade 向成績表中加外鍵
add constraint fk_StdNo
foreign key (StdNo)references Student(StdNo)
檢索“Grade表”中StdNo字段的數據,要求查詢結果中不包含重複記錄
select distinct StdNo from Grade
從Student表中查詢姓名中第二個字是“小”的學生
select* from Student where Sname like '_小%'
從Student表中查找姓王、姓李和姓劉的學生記錄
select*from Student where Sname like '王%'or Sname like '李%'or Sname like'劉%'
找出沒有成績的學生的信息
select Student.*,Grade.CouseNo,Grade.grade from Student inner join Grade on Grade.grade is null
統計Grade表中記錄學生總數
select count(distinct StdNo) as StudentNumber
from Grade
3 、select distinct Class,CouseNo, avg(grade) as 平均成績 from Grade,Student where Student.StdNo=Grade.StdNo
group by CouseNo,Class
order by avg(grade) desc,Class asc