sql

在這裏插入圖片描述
選擇操作

在這裏插入圖片描述

連接操作
在這裏插入圖片描述

併發控制的單位:事務

事務的四種屬性:原子性,一致性,隔離性,持久性

SQL-92四種隔離級別 髒讀。 讀提交。 可重複讀 可串行化

DM中的鎖:表鎖,行鎖(共享鎖,排他鎖),鍵範圍鎖

死鎖處理:預防死鎖,檢測死鎖,避免死鎖

分佈式數據庫:適應部門分佈的組織機構,降低費用;提高系統可靠性和可用性,充分利用數據庫資源,逐步擴展處理能力和系統規模

數據獨立性:數據的邏輯獨立性,數據的物理獨立性

在這裏插入圖片描述

1.創建表
創建表student,包括學生編號,姓名,性別,出生年月,班級

create table student(
SNO VARCHAR(3) NOT NULL,
SNAME VARCHAR(4) NOT NULL,
SSEX VARCHAR(2) NOT NULL,
SBIRTHDAY DATETIME,
CLASS VARCHAR(5)
);

創建course,特徵包括課程編號,課程名稱和教師編號
[cc lang=“SQL”]
create table course
(
CNO VARCHAR(5) NOT NULL,
CNAME VARCHAR(10) NOT NULL,
TNO VARCHAR(10) NOT NULL
);
[/cc]
創建score,包括學生編號,課程編號與分數
[cc lang=“SQL”]
create table score
(
SNO VARCHAR(3) NOT NULL,
CNO VARCHAR(5) NOT NULL,
DEGREE NUMERIC(10,1) NOT NULL
);
[/cc]

2.插入數據
[cc lang=“SQL”]
insert into student
(SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES(108,‘JIM’,‘MALE’,1975-09-09,95033);
[/cc]
3.查詢Student表中所有記錄的sname,ssex,class
[cc lang=“sql”]
select sname,ssex,class from student;
select distinct depart from teacher; #查詢教師所有單位
select * from student; #查詢所有學生信息
select * from score where degree between 60 and 80; #查詢表中成績爲60-80的所有記錄
select * from score where degree in (85,86,87); #查詢表中成績爲85,86,88
select * from student where class='95031’or ssex=‘female’; #查詢student表中“95031”班或性別爲“female”的記錄
select * from student order by class desc; #以class降序查詢student表的所有記錄
select * from score order by cno asc,degree des;#以cno升序,degree降序查詢score表的所有記錄
select count() from student where class=‘95031’; #查詢“95031”班的學生人數
select sno,cno from score where degree in ( select max(degree) from score );#查詢score表中的最高分的學生學號和課程號
select avg(degree) from score where cno=‘3-105’;#查詢3-105號課程的平準分
select avg(degree) from score where cno like ‘3%’ group by cno having count(
) >= 5; #查詢score表中至少有5名學生選修的並以3開頭的課程的平均數
14,15,16
select sno from score group by sno having min(degree)>70 and max(degree)<90; #查詢最低分大於70分,最高分小於90分的sno列
select a.sname,b.cno,b.degree from student as a join score as b on a.sno=b.sno #查詢所有學生的sname、cno和degree列
select a.cname,b.cno,b.degree from student as a join score as b on a.cno=b.cno #查詢所有學生的sno、cname和degree列
select a.sname,b.cname,c.degree from student as a join course as b join score as c on a.sno=c.sno and b.cno=c.cno;#查詢所有學生的sname、cname和degree列
select a.sname,b.cname,c.degree from student as a
join (course b,score c)
on a.sno = c.sno and b.cno = c.cno; #進階算法
17
select avg(degree) from score where sno in (
select sno from student where class=‘95033’); #查詢“95033”班級所選課程的平均分
select avg(a.degree) from score a
join student b
on a.sno = b.sno where b.class = ‘95033’; #進階算法
18
create table grade
(
low numeric(3,0),
upp numeric(3,0),
rank char(1)
);
insert into grade values(90,100,‘A’);
insert into grade values(90,100,‘B’);
insert into grade values(90,100,‘C’);
insert into grade values(90,100,‘D’);
insert into grade values(90,100,‘E’);
select a.sno,a.cno,b.rank from score a
join grade b
where a.degree between b.low and b.upp
order by rank; #假設使用如下命令建立了一個grade表,現查詢所有同學的Sno、Cno和rank列,並按照rank列排序
19
select * from score where cno = ‘3-105’
and degree > (
select degree from score where sno = 109 and cno = ‘3-105’
); #查詢score表中選修"3-105"課程的成績高於"109"號同學成績的所有同學的記錄
select a.* from score a
where a.cno = ‘3-105’ and a.degree > all(select degree from score b
where b.sno = ‘109’ and b.cno = ‘3-105’); #進階
20
select * from score s inner join (
select ss.sno, max(ss.degree) as maxd from score ss group by ss.sno
having count(ss.cno)> 1
)
a on s.sno=a.sno and s.degree <> a.maxd;查詢score中選學一門以上課程的同學中分數爲非最高分成績的記錄

[/cc]

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