数据库系统概念总结:第三章 SQL
生活随笔
收集整理的這篇文章主要介紹了
数据库系统概念总结:第三章 SQL
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
周末無事水文章,期末備考的總結(jié)資料
第三章 SQL
3.1 SQL查詢語(yǔ)言概況
3.2 SQL數(shù)據(jù)定義
3.2.1 基本類型
- char(n):固定長(zhǎng)度n的字符串(長(zhǎng)度不夠自動(dòng)補(bǔ)空格)
- varchar(n):可變長(zhǎng)度的最大長(zhǎng)度為n的字符串
- int:整型
- smallint:小整型
- numeric(p,d):定點(diǎn)數(shù)
- real,double precision:浮點(diǎn)數(shù)與雙精度浮點(diǎn)數(shù)
- float(n):精度至少為n的浮點(diǎn)數(shù)
3.2.2 基本定義模式
- 建表
- 修改/刪除表
– delete from r,意思是刪除表中所有的內(nèi)容
3.3 SQL查詢的基本結(jié)構(gòu)
3.3.1 單關(guān)系查詢
- 強(qiáng)行刪除重復(fù):select distinct dept_name from instructor;
- 顯示指明不去除重復(fù):select all dept_name from instructor;
3.3.2 多關(guān)系查詢
select A1, A2, ..., An from r1, r2, ..., rm where P;3.3.3 自然連接
select name, title from instructor natural join teaches natural join course;3.4 附加的基本運(yùn)算
3.4.1 更名運(yùn)算
- 變量名:select ID, name, salary/12 as monthly_salary from instructor;
- 表名:select distinct T. name from instructor as T, instructor as S where T.salary > S.salary and S.dept_name = 'Comp. Sci.';
3.4.2 字符串運(yùn)算
- 包含子串:select name from instructor where name like '%in%' ;
3.4.3 select子句紅的屬性說明
3.4.4 排序元組的顯示次序
select distinct name from instructor order by name;3.4.5 where子句謂詞
3.5 集合運(yùn)算
3.5.1 并運(yùn)算
select course_id from section where semester = 'Fall' and year = 2009 union select course_id from section where semester = 'Spring' and year = 2010;3.5.2 交運(yùn)算
select course_id from section where semester = 'Fall' and year = 2009 intersect select course_id from section where semester = 'Spring' and year = 2010;3.5.3 查運(yùn)算
select course_id from section where semester = 'Fall' and year = 2009except select course_id from section where semester = 'Spring' and year = 2010;3.6 空值
3.7 聚集(Aggregate)函數(shù)
3.7.1 基本聚集
- avg: 平均值
- min: 最小值
- max: 最大值
- sum: 值的求和
- count: 值的數(shù)量
3.7.2 分組聚集
- group by語(yǔ)法:select dept_name, avg (salary) from instructor group by dept_name;
3.7.3 having子句
- 分組限定的條件:select dept_name, avg (salary) from instructor group by dept_name having avg (salary) > 42000;
3.7.4 對(duì)空值和布爾值的聚集
3.8 嵌套子查詢
3.8.1 集合成員資格
select distinct course_id from section where semester = 'Fall' and year= 2009 and course_id in (select course_idfrom sectionwhere semester = 'Spring' and year= 2010);3.8.2 集合的比較
select name from instructor where salary > some (select salaryrom instructorwhere dept_name = 'Biology');- 注:其中some表示一些,也可被all替代,意思為全部
3.8.3 空關(guān)系測(cè)試
select course_id from section as S where semester = ’Fall’ and year = 2009 and exists (select *from section as Twhere semester = ’Spring’ and year= 2010 and S.course_id = T.course_id);- 注:子查詢?yōu)榉强諘r(shí)返回true值
3.8.4 重復(fù)元組存在性測(cè)試
select T.course_id from course as T where unique (select R.course_idfrom section as Rwhere T.course_id= R.course_id and R.year = 2009);- 注:子查詢結(jié)果中沒有重復(fù)的元組,unique結(jié)構(gòu)將返回true值
3.9 數(shù)據(jù)庫(kù)的修改
3.9.1 刪除
delete from r where P;3.9.2 插入
insert into course values ('CS-437', 'Database Systems', 'Comp. Sci.', 4); insert into course (course_id, title, dept_name, credits) values ('CS-437', 'Database Systems', 'Comp. Sci.', 4);3.9.3 更新
update instructor set salary = salary * 1.03 where salary > 100000;總結(jié)
以上是生活随笔為你收集整理的数据库系统概念总结:第三章 SQL的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库系统概念总结:第二章 关系模型介绍
- 下一篇: 数据库系统概念总结:第五章 高级SQL