char(n):存放固定長度 n 的字串資料
varchar(n):存放字串大小為 n 的字串
int:存放整數,最大長度為 4 位元
smallint:存放整數,最大整數為 2 位元
numeric(p,d):存放小數的資料,p 表示精準度,d 表示 d 位數的小數
EX:numeric(3,1)為三位數,包含2位整數與1位小數
real,double,precision:存近似浮點數,精準值依機器定
float(n):存放浮點數值,精準度為 n
null:表示不存在或不確定資料
create table instructor(
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2)
create table instructor(
ID char(5),
name varchar(20) not null,
dept_name varchar(20),
salary numeric(8,2),
primary key (ID),
foreign key (dept_name) references department)
insert into instructor values (‘10211’, null, ’Biology’, 66000);
delete from student;
drop table r;//(delete from r;)
alter table r add A D
//A為新增的屬性名稱,D為類型
alter table r drop A
from:欲查詢的關聯
select(選擇):顯示查詢欄位的最終結果
可在後加上運算,但不會改變原本的資料
select distinct dept_name
from instructo
select name
from instructor
where dept_name= ‘Comp. Sci.'and salary > 80000
資訊系薪水大於80000的教授
select name, course_id
from instructor, teaches
where instructor.ID = teaches.ID
select name, title
from instructor natural join teaches
select name, title
from instructor natural join teaches, course
where teaches.course_id= course.course_id;
old-name as new-name
select ID, name, salary/12 as monthly_salary
from instructor
(%):任何子字符串匹配
(_): 任何字符匹配。
select name
from instructor
where name like '%dar%'
select instructor *
select distinct name
from instructor
order by name
order by name desc
order by dept_name, name
between:指定值可在某數據之間
EX:查找所有教師的名稱
薪水在90,000美元到100,000美元之間
select name
from instructor
where salary between 90000 and 100000
比較元組
select name, course_id
from instructor, teaches
where (instructor.ID, dept_name) = (teaches.ID, ’Biology’);
null表示一個未知值或不存在值
涉及null的任何算術表達式結果都是null
5 + null returns null
is null:檢查空值
Example: Find all instructors whose salary is null.
select name
from instructor
where salary is null
NULL的三維邏輯
聚集函數為多個值輸入後返回為單一值的函數
select count (distinct ID)from teacheswhere semester = ’Spring’ and year = 2010
select dept_name, avg (salary)
from instructor
group by dept_name;
select dept_name, avg (salary)
from instructor
group by dept_name
having avg (salary) > 42000;
select sum(salary )
from instructor
以上語句忽略NULL,如有為空集合結果為null
select count (distinct ID)
from takes
where (course_id, sec_id, semester, year) in
(select course_id, sec_id, semester, year
from teaches
where teaches.ID= 10101);
select name
from instructor
where salary > some (select salary
from instructor
where dept name = ’Biology’);
select name
from instructor
where salary > all (select salary
from instructor
where dept name = ’Biology’);
select course_id
from section as S
where semester = ’Fall’ and year= 2009 and
exists (select *
from section as T
where semester = ’Spring’ and year= 2010 and S.course_id= T.course_id);
select T.course_id
from course as T
where unique (select R.course_id
from section as R
where T.course_id= R.course_id and R.year= 2009);
select dept_name, avg_salary
from (select dept_name, avg (salary) as avg_salary
from instructor
group by dept_name)
where avg_salary> 42000;
select name, salary, avg_salary
from instructor I1, lateral (select avg(salary) as avg_salary
from instructor I2
where I2.dept_name= I1.dept_name);
with max_budget(value) as (select max(budget)from department)
select budget
from department, max_budget
where department.budget= max_budget.value;
delete from instructor
where salary< (select avg (salary) from instructor);
insert into course (course_id, title, dept_name, credits)
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
or
insert into course (course_id, title, dept_name, credits)
values (’CS-437’, ’Database Systems’, ’Comp. Sci.’, 4);
insert into studentselect ID, name, dept_name, 0
from instructor
update instructor
set salary = salary * 1.03
where salary > 100000;
update instructor
set salary = salary * 1.05
where salary <= 100000;
update instructor
set salary = case
when salary <= 100000 then salary * 1.05
else salary * 1.03
end
update student S
set tot_cred= ( select sum(credits)
from takes natural join course
where S.ID= takes.ID and
takes.grade<> ’F’ and
takes.grade is not null);