当前位置:文档之家› 数据库读书笔记

数据库读书笔记

(selectcourse_id
fromteaches
whereteaches.ID=10101)
集合的比较:some,all(<,<=,>=,>,<>)
至少比某一个大:>some
selectname
frominstructor
wheresalary>some(selectsalaryfrominstructorwheredept_name='拳脚学院')
18.select子句中表示“所有的属性”:星号“*”
select*
frominstructor
19.排列元组的显示顺序:orderby
默认使用升序。可以显示使用desc表示降序,asc表示升序,排序可在多个属性上进行。
select*
frominstructor
where
orderby sa次”用unique。
找出所有在2009年最多开设一次的课程
selectT.course_id
fromcourseasT
whereunique(selectR.course_idfromsectionasRwhereR.course_id=T.course_idandr.year=2009)
15.select a1,a2相同的属性列会出现两次
fromtable1jointable2onp1
16.更名运算:as
as子句既可以出现在select子句中,又可以出现在from子句中。主要用于:把一个长的关系名替换成短的使用于需要比较同一个关系中的元组情况,自己跟自己进行笛卡尔积运算。

可以将“关系A包含关系B”写成“not exists(B except A)”,若返回true,说明A包含B
selects.ID,
fromstudentass
wherenotexists((selectcourse_idfromcoursewheredept_name='兵器学院')except(selectt.course_idfromtakesastwheres.ID=t.ID))
比所有的都大:>all
selectname
frominstructor
wheresalary>=all(selectsalaryfrominstructorwheredept_name='邪门学院')//找出最大的
空关系测试:exists,not exists。测试一个子查询的结果中是否存在元组。exists结构在作为参数的子查询非空时返回true值。
wheresalary<70000
(updateinstructor
setsalary =case
whensalary<=100000 then salary*1.05
elsesalary*1.03
end)
8.case语句的一般格式:
case
whenp1thenr1
whenp2thenr2
…..
whenpnthenrn
frominstructor
groupbydept_name
25.having子句:相当于分组后的谓词where,对分组限定条件
26.where子句中的嵌套子查询
集合成员资格:in,not in
selectCOUNT(distincttakes.ID)astotal_num
fromtakes
wherecourse_idin
frominstructor
wheresalaryis null
如果算术表达式的任一输入为空,则该算术表达式(涉及+,-,*,/)结果为空。例如,某个特定的元组table.a为空,则表达式table.a+5结果也为空。
任意涉及null的比较运算的结果视为unknown。unknown是true和false以外的第三个逻辑值。
where子句的谓词对一个元组计算出false或unknown,那么该元组不能被加到结果集中。
值非空:is not null
如果元组在所有属性上的取值相等,那么它们就被当作相同元组,即使某些值为空。(’a’,null)和(’a’,null)被认为是同一元组。
23.五个聚集函数:
平均值:avg
最小值:min
(selectcourse_id
fromsection
where semester=’fall’and year=2009)
exceptall
(selectcourse_id
fromsection
where semester=’spring’and year=2010)
22.空值null
selectname
减:altertablerdropA D;
11.强行删除重复:distinct
Selectdistinctdept_name
From instructor
12.基本查询语句:
selecta1,a2,……an
fromtable1,table2,……tablen
wherep;
a为属性,table为关系,p为table间的谓词。
20.where子句谓词
where salarybetween9000and10000
等于where salary <=10000 and >=9000
还可以使用not between比较运算符
允许用(v1,v2,….,vn)来表示n元组,在元组上可以运用比较运算符,按字典顺序进行比较运算。如:
selectname,course_id
“至少两次”用not unique
找出所有在2009年至少开设两次的课程
selectT.course_id
fromcourseasT
wherenotunique(selectR.course_idfromsectionasRwhereR.course_id=T.course_idandr.year=2009)
1.基本类型:
a)Char(n):固定长度,指定长度n;
b)Varchar(n):可变长度,指定最大长度n;
c)Int:整数类型;
d)Smallint:小整数类型;
e)Numeric(p,d):定点数,p是位数(加上一个符号位),d是精度,小数点右边的位数;
f)Real,double,precision:浮点数与双精度浮点数;
5.数据加载:insert
insertintotable1
values(a1,a2,….an);
6.删除元组:delete
deletefromtable1删除student中符合谓词p1的
where p1元组
7.更新:update
updateinstructor
setsalary=salary*1.05
13.自然连接:natural join
Select name,course_id
From instructornatural jointeaches;
只考虑那些在两个关系模式中都出现的属性上取值相同的元组对,这样的属性只出现一次。列出顺序为:先是两个关系模式中的共同属性,然后是那些只出现在第一个关系模式中的属性,最后是那些只出现在第二个关系模式中的属性。
3.primary key(a1,a2,…….an):primary key声明表示属性a1,a2,….an构成关系的主码。主码属性必须非空且唯一。
4.foreign key(b1,b2,…….bn)references table1:foreign key声明表示关系中任意元组在属性(b1,b2,……bn)上的取值必须对应于(在…之中)关系table1中某元组在主码属性上的取。
fromsection
where semester=’spring’and year=2010)
intersect
自动去除重复
如果想保留所有重复,必须用intersect all,在结果中出现的重复元组数等于两个关系中重复次数最少的那个。
(selectcourse_id
fromsection
where semester=’fall’and year=2009)
g)Float(n):精度至少为n位的浮点数。
2.创建关系:create table
a)create tabledepartment
(dept_namevarchar(20),
Buildingvarchar(15),
Budgetnumeric(12,2),
Primarykey(dept_name));
frominstructor,teaches
whereinstructor.id=teaches.id
anddept_id=’biology’;
等同于
selectname,course_id
frominstructor,teaches
where(instructor.id,dept_name)=(teaches.id,’biology’);
intersectall
(selectcourse_id
fromsection
where semester=’spring’and year=2010)
except
此运算在执行集差操作之前自动去除重复。
如果想保留所有重复,必须用except all,在结果中出现的重复元组数等于table1中出现的重复元组数减去table2中重复出现的元组数(前提是此差为正)
①通过from子句定义了一个在该子句中所列出关系上的笛卡尔积
②where子句中的谓词用来限制笛卡尔积所建立的组合。允许使用逻辑连词and,or,not,逻辑连词的运算对象是包含比较运算符<,<=,>=,>,=,<>的表达式。可以用比较运算符来比较字符串、算术表达式、特殊类型,如日期类型
相关主题