#創建資料庫create database ST CHARACTER set utf8;#創建用戶create user ST identified by '19980510';#授權用戶操作該資料庫grant all on ST.* to ST; #創建學生表create table Studen ...
#創建資料庫
create database ST CHARACTER set utf8;
#創建用戶
create user ST identified by '19980510';
#授權用戶操作該資料庫
grant all on ST.* to ST;
----------------
#創建學生表
create table Student
(
Sno char(9) primary key ,
Sname char(20) not null,
Ssex char(2),
Sage smallint,
Sdept char(20)
);
#插入信息
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215121,'李勇','男',20,'CS');
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215122,'劉晨','女',19,'CS');
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215123,'王敏','女',18,'MA');
insert into Student(Sno,Sname,Ssex,Sage,Sdept) values(201215125,'張立','男',19,'IS');
--------------
#創建課程表
create table Course
(
Cno char(4) primary key,
Cname char(40) not null,
Cpno char(4),
Ccredit smallint,
foreign key(Cpno) references Course(Cno)
);
#插入信息
insert into Course(Cno,Cname,Cpno,Ccredit) values(1,'資料庫',5,4);
insert into Course(Cno,Cname,Cpno,Ccredit) values(2,'數學',,2);
insert into Course(Cno,Cname,Cpno,Ccredit) values(3,'信息系統',1,4);
insert into Course(Cno,Cname,Cpno,Ccredit) values(4,'操作系統',6,3);
insert into Course(Cno,Cname,Cpno,Ccredit) values(5,'數據結構',7,4);
insert into Course(Cno,Cname,Cpno,Ccredit) values(6,'數據處理',,2);
insert into Course(Cno,Cname,Cpno,Ccredit) values(7,'PASCAL語言',6,4);
-------------------
/*
[SQL]insert into Course(Cno,Cname,Cpno,Ccredit) values(1,'資料庫',5,4);
[Err] 1452 - Cannot add or update a child row: a foreign key constraint fails (`st`.`course`, CONSTRAINT `course_ibfk_1` FOREIGN KEY (`Cpno`) REFERENCES `course` (`Cno`))
*/
-- 解決方法 先不錄入先修課(Cpno)的值
insert into Course(Cno,Cname,Ccredit) values(1,'資料庫',4);
insert into Course(Cno,Cname,Ccredit) values(2,'數學',2);
insert into Course(Cno,Cname,Ccredit) values(3,'信息系統',4);
insert into Course(Cno,Cname,Ccredit) values(4,'操作系統',3);
insert into Course(Cno,Cname,Ccredit) values(5,'數據結構',4);
insert into Course(Cno,Cname,Ccredit) values(6,'數據處理',2);
insert into Course(Cno,Cname,Ccredit) values(7,'PASCAL語言',4);
------------------
#創建選課表
create table SC
(
Sno char(9),
Cno char(4),
Grade smallint,
primary key(Sno,Cno),
foreign key(Sno) references Student(Sno),
foreign key(Cno) references Course(Cno)
);
#插入信息
insert into SC(Sno,Cno,Grade) values(201215121,1,92);
insert into SC(Sno,Cno,Grade) values(201215121,2,85);
insert into SC(Sno,Cno,Grade) values(201215121,3,88);
insert into SC(Sno,Cno,Grade) values(201215122,2,90);
insert into SC(Sno,Cno,Grade) values(201215122,3,80);
--------------------------
#查詢全體學生的學號和姓名
select Sno,Sname from Student ;
---------
#查詢全體學生的姓名、學號、所在系
select Sname,Sno,Sdept from Student;
---------
#查詢全體學生的詳細記錄 (查詢經過計算的值)
select * from Student;
-----------
#查詢全體學生的姓名及其出生年份
select Sname,2018-Sage from Student;
-----------
#查詢全體學生的姓名、出生年份和所在院系,要求用小寫字母表示系名
select Sname,2018-Sage,lower(Sdept) from Student;
------------
#查詢選修了課程的學生學號
select Sno from SC; -- select all Sno from SC; -- 有重覆
select distinct Sno from SC; -- 無重覆
------------
#查詢電腦科學系全體學生的名單
select * from Student where Sdept = 'CS';
------------
#查詢所有年齡在20歲以下的學生姓名及其年齡
select Sname,Sage from Student where Sage<20;
------------
#查詢考試成績不合格的學生的學號
select Sno from SC where Grade<60;
------------
#查詢年齡在20——23歲(包括20歲和23歲)之間的學生的姓名、系別和年齡
select Sname,Sdept,Sage from Student where Sage>=20 and Sage<=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='CS' or Sdept='MA' or Sdept='IS';
-------------
#查詢既不是電腦科學系、數學系、也不是信息系的學生的姓名和性別
select Sname,Ssex from Student where Sdept not in('CS','MA','IS');
-------------
#查詢學號為201215121的學生的詳細情況
select * from Student where Sno like '201215121';
select * from Student where Sno='201215121';
-- 註意:如果like後面的匹配串中不含通配符,則可以用 = (等於)運算符取代 like 謂詞,用 != 或 <> (不等於)運算符取代 not like 謂詞
-------------
#查詢所有姓劉的學生的姓名、學號和性別
select Sname,Sno,Ssex from Student where Sname like '劉%';
-------------
#查詢姓“歐陽”且全名為三個漢字的學生的姓名
select Sname from Student where Sname like '歐陽_';
-- 註意:資料庫字元集為ASCII時一個漢字需要兩個 _ ; 當字元集為GBK是只需要一個
--------------
#查詢名字中第二個字為“陽”的學生的姓名和學號
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\_%' escape '\\';
-------------
#某些學生選修課程後沒有參加考試,所以有選課記錄,但沒有考試成績。查詢缺少成績的學生的學號和相應的課程號
select Sno,Cno from SC where Grade is null; -- 註意這裡的“IS”不能用 = (等號)代替
-------------
#查詢電腦科學系年齡在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 Sno,Grade from Sc where Cno='3' order by Grade asc; -- 升序
-------------
/*
#查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系中學生按年齡降序排列
select * from Student where order by Sdept,Sage desc; -- 預設為升序
[SQL]select * from Student where order by Sdept,Sage desc;
[Err] 1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'order by Sdept,Sage desc' at line 1
*/
------------
#查詢學生總人數
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(Ccredit) from SC,Course where Sno = '201215122' and SC.Cno = Course.Cno;
------------
#求各個課程號及相應的選課人數
select Cno,count(*) from SC group by Cno;
-- group by 子句將查詢結果按某一列或多列的值分組,值相等的為一組。
-- 分組後聚集函數將作用於每一個組,即每一組都有一個函數值
------------
#查詢選修了三門以上課程的學生學號
select Sno from SC group by Sno having count(*)>3;
-- having 短語作用於組,從中選擇滿足條件的組
-------------
#查詢平均成績大於等於90分的學生學號和平均成績
select Sno, avg(Grade) from SC where avg(Grade)>=90 group by Sno; -- 此句不正確,where子句中不能用聚集函數作為 條件表達式
select Sno, avg(Grade) from SC group by Sno having avg(Grade)>=90;
--------------------------------------------------------------- 連接查詢
#查詢每個學生及其選修課程的情況
select Student.*,SC.* from Student,SC where Student.Sno = SC.Sno;
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from Student,SC where Student.Sno = SC.Sno; -- 自然連接實現
--------
#查詢選修2號課程且成績在90分以上的所有學生的學號和姓名
select Student.Sno,Sname from SC,Student where Student.Sno = SC.Sno and SC.Cno='2' and SC.Grade >=90;
select student.Sno,Sname from Student inner join SC on(student.Sno = SC.Sno) where Cno= '2' and Grade >=90;
----------
#查詢每一門課的間接先修課(即先修課的先修課).按照Cno降序排列
select first.Cno,second.Cpno from Course as first,Course as second where first.Cpno = second.Cno and Second.Cpno is NOT null order by first.Cno desc -- first,second 是Course表的兩個別名
-----------
#查詢全體學生的詳細信息和所選課程號及成績
select Student.Sno,Sname,Ssex,Sage,Sdept,Cno,Grade from Student left outer join SC on (Student.Sno = SC.Sno);
select Student.*,Cno,Grade from Student left outer join SC on (Student.Sno = SC.Sno);
-----------
#查詢每個學生的學號、姓名、選修的課程名及成績
select Student.Sno,Student.Sname,Course.Cname,SC.Grade from Student,Course,SC where Student.Sno = SC.Sno and Course.Cno = SC.Cno;
--------------------------------------------------------------- 嵌套查詢
#查詢與“劉晨”在同一個系學習的學生
select Student.* from Student where Sdept in (select Sdept from Student where Sname = '劉晨');
-----------
#查詢選修了課程名為“信息系統”的學生學號和姓名
select Sno,Sname from Student where Sno in (select Sno from SC where Cno in (select Cno from Course where Cname = '信息系統'));
select Student.Sno,Sname from Student,Course,SC where Course.Cname = '信息系統' and Student.Sno = SC.Sno and Course.Cno = SC.Cno;
----------
#找出每個學生超過他自己選修課程平均成績的課程號
select Sno,Cno from SC x where Grade >= (select avg(Grade) from SC y where y.Sno = x.Sno);
-----------
#查詢非電腦科學系中比電腦科學系任意一個學生年齡小於的學生姓名和年齡
select Sname,Sage from Student where Sage<any(select Sage from Student where Sdept = 'CS')and Sdept<>'CS';
select Sname,Sage from Student where Sage<(select max(Sage) from Student where Sdept = 'CS')and Sdept<>'CS';
-----------
#查詢非電腦科學系中比電腦科學系所有學生年齡都小的學生姓名及年齡
select Sname,Sage from Student where Sage<all(select Sage from Student where Sdept = 'CS')and Sdept<>'CS';
select Sname,Sage from Student where Sage<(select min(Sage) from Student where Sdept = 'CS')and Sdept<>'CS';
-----------
#查詢所有選修了1號課程的學生姓名
select Sname from Student where exists (select * from SC where Sno = student.Sno and Cno = '1');
select Sname,Sno from Student where Sno in (select Sno from SC where Cno = '1');
select Sname,Student.Sno,Grade from Student,SC where SC.Cno = '1' and student.Sno = SC.Sno;
------------
#查詢沒有選修1號課程的學生姓名
select Sno,Sname from Student where not exists (select * from SC where Sno = student.Sno and Cno = '1');
------------
#查詢選修了全部課程的學生姓名
-----------
#查詢至少選修了學生201215122選修的全部課程的學生號碼
---------------------------------------------------------------- 集合查詢
-- 註意:參加集合操作的各查詢結果的列數必須相同;對應項的數據類型也必須相同
#查詢電腦科學系的學生及年齡不大於19歲的學生
------------
#查詢選修了課程1或者選修了課程2的學生
------------
#查詢電腦科學系的學生與年齡不大於19歲的學生的交集
-------------
#查詢既選修了課程1又選修了課2的學生
-------------
#查詢電腦科學系的學生與年齡不大於19歲的學生的差集
2018-10-07 15:57:06