/* 建立表 */ create table course course_id varchar(20), title varchar(20) not null, price numeric(5,2) default 100.00, primary key(course_id)); /* 刪除表 */ drop table course; /* 增長列 */ alter table course add credits numeric(2,0); /* 刪除列 */ alter table course drop credits; /* 建立視圖 */ create view v as <query expression> /* 插入數據 */ insert into course values('CS-437', 'Database Systems', 'Comp.Sci.', 4); /* 刪除數據 */ delete from course where course_id = 'CS-445'; /* 更新數據 */ update instructor set salary = salary * 1.05 where salary < 70000; /* 查詢數據 */ select distinct dept_name from instructor where salary > 80000 and dept_name <> 'Biology'; /* 用 as 給屬性別名(as 能夠省略)*/ select name as instructor_name from instructor; /* 用 as 給關係別名(as 能夠省略)*/ select * from instructor as T, instructor as S where T.sarary > S.sarary and S.dept_name = 'Biology'; /* 建立索引 */ create index sid on student(ID); /* 授予 */ grant <權限列表> on <關係名或視圖名> to <用戶/角色列表> /* 收回 */ revoke <權限列表> on <關係名或視圖名> from <用戶/角色列表> /* 建立角色 */ create role instructor;
%
:匹配任意字符串_
:匹配一個字符||
:串聯trim()
:去掉後面的空格upper()
、lower()
:轉大小寫select * from department where buidling like '%cat\_room%' escape '\';
select * from instructor order by salary desc, name asc; /* asc 能夠省略 */
select * from instructor where salary between 9000 and 10000; /* 等價於 salary >= 9000 and salary <= 10000; 也有 not between */
union
intersect
except
sql
(select course_id from section where year = 2009) union (select course_id from section where semester = 'Spring') 去並集,會自動去重(union all 不去重)
涉及空值的比較,返回 unknownexpress
判斷空值用 is null,不能用 = null閉包
min
、max
、sum
、avg
、count
count(distinct ID)
group by
分組彙集having
分組的限定條件in
集合成員資格, not in
> some
至少比某個大,< all
, >=
, <=
, <>
exists
空關係測試not exists(B except A)
「關係 A 包含關係 B」unique
重複元組存在性測試with
定義臨時關係select * from a join b on a.id=b.id; select * from a natural left outer join b where course_id is null;
create table section ( course_id varchar(8), sec_id varchar(8), /* check 約束,not null 約束*/ semester varchar(6), check (semester in('Fall','Spring')), year numeric(4,0), check (year>1759 and year < 2100), room_number varchar(7) not null, primary key (course_id, sec_id, semester, year), foreign key (course_id) references course, )
候選碼用 unique 或者 primary key 約束。函數
create table course( ... foreign key(dept_name) references department on delete cascade on update cascade, );
cascade表明級聯,當刪除 department 元組時,course 的對應元組也會被級聯刪除。
相似的還有 set null、set default。測試
varchar(n)
char(n)
int
numeric(p,d)
real
double precision
float(n)
date
time
timestamp
timestamp with timezone
interval
current_date()
當前日期currrent_time()
current_timestamp()
帶時區localtime()
本地時間extract(filed from d)
從 date 或 time 類型的 d 中提取year,month,day,hour,minute,second的任意一種select A1, A2, sum(A3) from r1, r2, ..., rm where P group by A1, A2
等價於
\(_{A_1,A_2}\mathcal{G}_{sum(A_3)}(\Pi_{A_1,A_2,\cdots,A_n}(\sigma_P(r_1\times r_2\times \cdots \times r_m)))\)ui
若\(\beta\subseteq\alpha\) ,則依賴\(\alpha \rightarrow \beta\)是平凡的。spa
\(F^+\) 表示函數依賴集合F的閉包code
Boyce-Codd範式 BCNF:關係 R 是 BCNF 當它的函數依賴集 F 知足:\(F^+\) 中的全部非平凡依賴的都是 R 的超碼。排序
能夠將關係分解爲 BCNF 模式集合。索引
F 的正則覆蓋\(F_c\)知足:全部函數依賴不含無關屬性 且 \(\alpha\)都是惟一的。
\(R_1 \cap R_2\) 是 R 上的超碼,則是無損分解