文章目錄
建表SQL
最近遇到一些學過、沒學過、學全、沒學全這樣的題目,總結一下各種方法。
--學生表
create table Student(
Stu_no char(8) , --學號
Stu_name nvarchar(10) ,--姓名
Stu_age tinyint , --年齡
Stu_sex nchar(2) check(stu_sex in('男','女')), --性別
Stu_dept nvarchar(20),--專業
CONSTRAINT pk_sno PRIMARY KEY (Stu_no));
--課程表
create table Course(
Cou_no char(3), --課程號
Cou_name nvarchar(30), --課程名
Cou_pno char(3), --先修課
Cou_teacher nvarchar(10),--老師姓名
CONSTRAINT pk_cno PRIMARY KEY (Cou_no));
--選課表
create table SC(
Stu_no char(8) , --學號
Cou_no char(3), --課程號
Grade decimal(5,1),--成績
CONSTRAINT pk_sc PRIMARY KEY (Stu_no,Cou_no), --主健
foreign key(Stu_no) references Student(Stu_no),--外健
foreign key(Cou_no) references Course(Cou_no), --外健
check ((grade is null) or (grade between 0 and 100)));
-------------------------------
INSERT into Student VALUES('20180001','全學過',20,'男','計算機');
INSERT into Student VALUES('20180002','沒學過',19,'女','計算機');
INSERT into Student VALUES('20180003','沒學全一',18,'男','信息');
INSERT into Student VALUES('20180004','沒學全二',19,'男','信息');
INSERT into Course VALUES('a01','計算機網絡','a04','李琳');
INSERT into Course VALUES('a02','數據庫原理','a04','李琳');
INSERT into Course VALUES('a03','操作系統','a04','張勇');
INSERT into Course VALUES('a04','數據結構','a04','張娜');
INSERT into SC VALUES('20180001','a01',92.5);
INSERT into SC VALUES('20180001','a02',85);
INSERT into SC VALUES('20180002','a03',88);
INSERT into SC VALUES('20180002','a04',90);
INSERT into SC VALUES('20180003','a01',80);
INSERT into SC VALUES('20180003','a03',65);
INSERT into SC VALUES('20180004','a02',78);
INSERT into SC VALUES('20180004','a04',88);
1 查詢學過李琳老師講授課程的學生學號,姓名,成績
(1)多表連接
--1.1學過---多表連接
select student.stu_no ,stu_name,grade
from student,course,sc
where student.stu_no = sc.stu_no
AND course.cou_no = sc.cou_no
AND course.Cou_teacher='李琳';
(2)不相關子查詢 in
--1.2學過--不相關子查詢(注意:投影的屬性不在子查詢表中,
-- 即查詢的屬性列,和篩選條件的屬性不在一張表
select student.stu_no ,stu_name,grade
from student,sc
where student.stu_no = sc.stu_no
AND sc.cou_no in (select Cou_no
from Course
where course.Cou_teacher='李琳')
(3)相關子查詢exists
--1.3學過----相關子查詢
select student.stu_no ,stu_name,grade
from sc,student
where sc.stu_no = student.stu_no
AND EXISTS
(select *
from course
where cou_no = sc.Cou_no --相關子查詢
AND cou_teacher = '李琳')
2 沒學過李琳老師講授課程的學生學號,姓名
(1)不相關子查詢 not in
--2.1 沒學過-- not in
--這是一個錯誤的示例,排除時以選課記錄爲單位
-- 例如 沒選全這個學生有兩門選科,會留下一門不是李琳老師的選課記錄。
select student.stu_no ,stu_name,grade
from student,SC
where sc.stu_no = student.stu_no AND
sc.cou_no not in(--sc.cou_no 以選課記錄爲單位
select cou_no
from course
where cou_teacher = '李琳')
-- 以學生爲單位進行篩選--不相關子查詢
select student.stu_no ,stu_name,grade
from student,sc
where student.stu_no = sc.stu_no
AND student.stu_no not in(--以下是學過李琳老師課程的學號,
--student.stu_no not in 是以學生爲單位進行篩選
select stu_no
from course,sc
where course.cou_no = sc.Cou_no
AND cou_teacher= '李琳')
(2)相關子查詢 not exists
--2.2沒學過 exists 相關子查詢
--以學生爲單位進行篩選
select student.stu_no, stu_name
from student
where not exists(
select *
from sc,course
where sc.cou_no = course.cou_no
AND sc.stu_no = student.stu_no
AND cou_teacher = '李琳'
)
(3)集合的差
--2.3 沒學過 集合的差
----注意篩選時以學生爲單位進行
select stu_no,stu_name
from student
except
select student.stu_no,stu_name
from student,sc,course
where sc.stu_no = student.stu_no
AND sc.cou_no = course.cou_no
AND Cou_teacher='李琳'
3 學全李琳老師講授課程學生的學號,姓名,成績
(1)not exists不相關子查詢
--學全
--相關子查詢,能查到第一個表裏的屬性列,條件寫在最後一個 not exists中
--至於怎麼查出 course_no,cou_name,grade,如果用一個句子表達會很複雜,可以創建一個只含學全的學生表信息
--再把視圖和sc、course連接
-- select * from view_all_study_students;
if exists(select * from sys.views where name='view_all_study_students')
drop view view1
go
create view view_all_study_students
AS
select student.stu_no ,stu_name
from student
where not exists (select *
from course
where cou_teacher ='李琳'
AND not exists(
select *
from sc
where sc.stu_no = student.stu_no
AND sc.cou_no = course.cou_no)
)
if exists (select * from sys.views where name='view_s_c_sc')
drop view view_s_c_sc
go
create view view_s_c_sc AS
select view_all_study_students.Stu_no,view_all_study_students.Stu_name,cou_name,grade
from view_all_study_students,course,sc
where view_all_study_students.Stu_no= sc.Stu_no
AND sc.Cou_no= course.cou_no
go
select * from view_s_c_sc;
4 沒學全李琳老師講授課程的學生姓名、學號
(1)全部學生信息 except 學全的學生信息
略
(2)找出學生學習李琳老師的課程數目,李琳老師教授課程的數目,比較大小
--學過,沒學全均爲條件
select student.stu_no
from student
where stu_no in (select distinct stu_no --學過
from sc,course
where sc.cou_no = course.cou_no
AND cou_teacher = '李琳'
group by stu_no
having count(*)<
(select count(*)
from course
where cou_teacher='李琳'))--沒學全
5 SQL SERVER 使用中遇到的問題
(1) 報錯將截斷二進制數據或字符串
應考慮設置的字段大小是不是太小了,可以使用VARCHAR
(2)語法錯誤:create view必須是批處理中僅有的語句
原因: 你可能在這段代碼之前還有其他的語句是同時在處理。
解決辦法:1、可以在這段代碼的前一行加上GO,在這段代碼結束後一行加上GO
2、在新窗口單獨執行這段代碼
(3)GO 的用法
用來提交T-SQL語句的一個標誌
每個被GO分隔的語句都是一個單獨的事務,一個語句執行失敗不會影響其它語句執行
GO的意思 是 分批處理語句 有加這個 GO ,就執行GO 行的代碼,執行後再執行接下來的代碼……