1、SQL=> DML 数据操纵语言(select、update、insert、delete、......)
DDL 数据定义语言(create、drop、alter、......)
DCL 数据库控制语言(grant、revoke、......)
2、关系型数据库
表 -> 关系(relation)
行 -> 元组(tuple)
列 -> 属性(attribute) -> 域(domain)
3、码
超码 > 候选码 > 主码
超码(super key):属性或属性集,可以确定唯一元组
候选码(candidate key):任意子集不是超码
主码(primary key):由使用者选定的候选码
外码(foreign key):R1中有R2的属性A,则A在R1上称作参照R2的外码
4、DDL
基本类型,char(n),varchar(n),numeric(p,d),float(n)等。char(n)不足n位时自动用空格补全。numeric(p,d)表示有p位数字,其中d位在小数点右边。
语法: create table <table_name> (attribute_1 type_1, attribute_2 type_2, ... , attribute_n type_n, <constraints>);
其中,“约束”例如:“primary key (attr1,attr2)”、foreign key (attr1) reference R2;
5、SQL查询的基本结构及运算
(1)select 默认有重复 = select all;select distinct 去重复;
(2)select...from...where 理解顺序:from -> where -> select
(3)from R1, R2 where R1.ID = R2.ID
= from R1 natural join R2
= from R1 join R2 using(ID)
(4)更名运算:select old_name as new_name, attr2, attr3,...
(5)排序:where ... order by ... desc,... asc:降序&升序
(6)集合运算:union并,intersect交,expect集合差
(7)聚集函数:avg、min、max、sum、count。
group by:分组聚合;理解顺序:from -> where -> group by -> having(having应用到分组上,因此having之前一定会有group by)
(8)嵌套子查询:in、not in、> or < or >= or <= or = or <> some or all、exists、not exists。
(9)with子句:定义临时关系:with Rx (a1,a2,...,an) as (select...);
6、SQL完整性约束
(1)not null
(2)unique:reference后的属性必定是primary key或unique的。
foreign key ... reference ... on delete cascade:同步删除操作
(3)check:+(子句):check (...not in...)
7、SQL授权操作
grant select on department to Amy;
revoke select on department from Amy;