sql語句大全+實例講解

1.創建3張表

//學生表創建
CREATE table student(
Sno CHAR(9) PRIMARY KEY,
Sname CHAR(20) UNIQUE,
Ssex char(2),
Sage SMALLINT,
Sdept char(20)
);

//課程表創建
CREATE table course(
Cno char(4) PRIMARY KEY,
Cname char(40) not NULL,
Cpno char(4),
Ccredit SMALLINT
);

//學生選課表創建
CREATE table SC(
Sno char(9),
Cno char(4),
Grade SMALLINT
);

2.向表中添加數據
也可使用圖形化工具Navicat或其他進行輸入,

//向學生表中添加數據
INSERT into Student values(
201215121,'李勇','男',20,'CS'),
(201215122,'劉晨','女',19,'CS'),
(201215123,'王敏','女',18,'MA'),
(201215125,'張立','男',19,'IS'
);

//向課程表中添加數據
insert into course VALUES(
'1','數據庫','5',4),
'2','數學','',2),
'3','信息系統','1',4),
('4','操作系統','6',3),
('5','數據結構','7',4),
('6','數據處理','',2),
('7','Java語言','6',4)

//向學生選課表中添加數據
insert into sc values
(201215121,1,92),
(201215121,2,85),
(201215121,3,88),
(201215122,2,58),
(201215122,3,80)

3.數據查詢

3.1單表查詢

3.1.1查詢全體學生的學號與姓名
select Sno,Sname 
from student
3.1.2查詢全體學生的姓名,學號,所在系
select Sname,Sno,Sdept
from student
3.1.3查詢全體學生的詳細記錄
select * from student
3.1.4查詢全體學生的姓名及其出生年份
select Sname,2020-Sage
from Student
3.1.5查詢全體學生的姓名,出生年份和所在的院系,要求用小寫字母表示系名
select Sname,2020-Sage,lower(Sdept)
from student
3.1.6查詢選修了課程的學生學號
select Sno
from SC
3.1.7查詢計算機科學系全體學生的名單
select Sname
from student
where Sdept='CS'
3.1.8查詢所有年齡在20歲以下的學生姓名及其年齡
select Sname,Sage
from student
where Sage<20
3.1.9查詢考試成績不及格的學生的學號
select Sno
from sc
where Grade<60
3.1.10查詢年齡在20-23歲(包括20和23)之間的學生的姓名,系別,年齡
select Sname,Sdept,Sage
from student
where Sage between 20 and 23
3.1.11查詢年齡不中20-23之間的學生姓名,系別,年齡
select Sname,Sdept,Sage
from student
where Sage not between 20 and 23
3.1.12查詢計算機科學系(CS),數學系(MA),信息系(IS)學生的姓名和性別
select Sname,Ssex
from student
where Sdept in('CS','MA','IS')
3.1.13查詢既不是CS,MA,也不是IS的學生的姓名和性別
select Sname,Ssex
from student
where Sdept not in('CS','MA','IS')
3.1.14查詢學號爲201215121的學生的詳細情況
select * 
from student
where Sno='201215121'
3.1.15查詢所有姓劉的學生的姓名,學號,性別
select Sname,Sno,Ssex
from student
where Sname like '劉%'
3.1.16查詢姓歐陽且全名爲3個漢字的學生的姓名
select Sname
from student
where Sname like '歐陽_'
3.1.17查詢名字中第二個字爲“陽”的學生的姓名和性別
select Sname,Ssex
from student
where Sname like '_陽%'
3.1.18查詢所有不姓劉的學生的姓名,學號和性別
select Sname,Sno,Ssex
from student
where Sname not like '劉%'
3.1.19查詢缺少成績的學生的學號和響應的課程號
select Sno,Cno
from sc
where grade is null
3.1.20查詢所有有成績的學生的學號和課程號
select Sno,Cno
from sc
where grade is not null
3.1.21查詢計算機科學系且年齡在20歲以下的學生的姓名
select Sname
from student
where Sdept='CS' and Sage<=20
3.1.22查詢選修了3號課程的學生的學號及其成績,查詢結果按分數的降序排列
//order by 默認升序,ASC是升序,DESC是降序
select Sno,Grade
from sc
where Cno='3'
order by Grade desc
3.1.23查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系的學生按年齡降序排列
select *
from student
order by Sdept,Sage DESC
3.1.24查詢學生總人數
select count(*)
from student
3.1.25查詢選修了課程的學生人數
//學生可以選多門課程,避免重複需在count函數里加distinct短語
select count(distinct Sno)
from sc
3.1.26計算選修1號課程的學生平均成績
select avg(Grade)
from sc
where Cno='1'
3.1.27查詢選修1號課程的學生最高分數
select max(Grade)
from sc
where Cno='1'
3.1.28查詢學生201215121選修課程的總學分數
select sum(Grade)
from sc
where Sno='201215121'
3.1.29求各個課程號及相應的選課人數
//group up 是將查詢結果按某個屬性進行分組
select Cno ,Count(Sno)
from sc
group by Cno
3.1.30查詢選修了3門以上課程的學生學號
//having作用於組,這裏先用group by按Sno進行分組,再用聚集函數count對每一組進行計數,用having提取出滿足條件的組
select Sno
from sc
group by Sno
having count(*)>3
3.1.31查詢平均成績大於等於90分的學生學號和平均成績
//where句中不能用聚集函數作爲條件表達式
select Sno,avg(Grade)
from sc
group by Sno
having  avg(Grade)>=90

3.2連接查詢

3.2.1查詢每個學生及其選修課的情況
select student.*,sc.*
from Student,sc
where student.Sno=sc.Sno
3.2.2將上面 的例子用自然連接完成
select student.Sno,Sname,Ssex,Sage,Sdept,Cno,grade
from student,sc
where student.sno=sc.sno
3.2.3查詢選修2號課程且成績在90分以上的所有學生的學號和姓名
select student.Sno,Sname
from Student,sc
where student.Sno=sc.Sno and 
      sc.Cno='2'   and 
      sc.Grade>=90
3.2.4查詢每一門課的間接先修課(先修課的先修課)
//先對一門課找到其先修課,再按此先修課的課程號查找它的先修課,
//將表與自身連接,就要取別名
select FIRST.Cno,second.Cpno
from Course first,Course SECOND
where `first`.Cpno=`SECOND`.Cno
3.2.5查詢每個學生的學號,姓名,選修的課程名及成績
select student.Sno,Sname,Cname,Grade
from student,sc,course
where student.Sno=sc.Sno AND
      sc.Cno=course.Cno

3.3嵌套查詢

3.3.1查詢與劉晨在同一個系學習的學生
select Sno,Sname,Sdept
from student
where Sdept in(
    select Sdept
    from student
    where Sname='劉晨'
)
3.3.2查詢選修了課程名爲“信息系統”的學生學號和姓名
//嵌套查詢太多了,用連接查詢呈現出來
select student.Sno,Sname
from student,sc,course
where student.Sno=sc.Sno  and 
      sc.Cno=course.Cno and
      course.Cname='信息系統'
3.3.3找出每個學生超過他自己選修課程平均成績的課程號
select Sno,Cno
from sc x
where Grade >=(
    select avg(Grade)
    from sc y
    where y.Sno=x.Sno
)
3.3.4查詢非計算機系中比計算機系任意學生年齡小的學生姓名和年齡
//任意:any   所有:all
select Sname,Sage
from student
where Sage<any(
   select Sage
   from student
   where Sdept='CS'
)
3.3.5查詢非計算機系中比計算機系所有學生年齡都小的學生姓名和年齡
select Sname,Sage
from student
where Sage<all(
   select Sage
   from student
   where Sdept='CS'
)
3.3.6查詢所有選修了1號課程的學生姓名
select Sname
from student,sc
where student.Sno=sc.Sno and 
      sc.Cno='1'
3.3.7查詢選修了全部課程的學生姓名
select Sname
from student
where not exists(
  select * 
  from course
  where not exists(
     SELECT *
     from sc
     where Sno=student.Sno AND 
     Cno=course.Cno
  )    
)

3.4集合查詢

3.4.1查詢選修了1號課程或則2號課程的學生
select Sno
from sc
where Cno='1'
UNION
select Sno
from sc
where Cno='2'
3.4.2查詢既選修了課程1又選修了課程2的學生

4.數據更新

4.1插入數據

4.1.1將一個新學生元組(學號:201215128,姓名:陳東,性別:男,系別:IS,年齡:18歲)
insert into student
values ('201215128','陳東','男',18,'IS')
4.1.2插入一條選課記錄
insert into sc(Sno,Cno) VALUES('201215128','1')
4.1.3對每一個系,求學生的平均年齡,並把結果存入數據庫
//首先創建一張表來存放數據
create table Deptage(
  Sdept char(15),
  avg_age smallint
)

//計算數據,存放到表中
insert into Deptage(Sdept,avg_age)
select Sdept,avg(Sage)
from student
group by Sdept 

4.2修改數據

4.2.1將學生201215121的年齡改爲22歲
update student
set Sage=22
where Sno='201215121'
4.2.2將所有學生的年齡增加1歲
update student
set Sage=Sage+1
4.2.3將計算機系全體學生的成績置0
update student
set Sage=0
where Sdept='CS'

4.3刪除數據

4.3.1刪除學號爲201215128的學生記錄
delete 
from student
where Sno='201215128'
4.3.2刪除計算機系所有學生的選課記錄
delete 
from sc
where Sno in(
  select Sno
  from student
  where Sdept='CS'
)
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章