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);
<<: CMoney第八届菁英软件工程师战斗营满一个月_Week 4
>>: 企业培训及顾问服务 Corporate Training & Consultancy
In recent years, with the development of web marke...
这篇的上一篇:https://ithelp.ithome.com.tw/articles/10283...
「现在这个年代,带人要带心,公司架构尽量扁平化,跟同事打成一片最要紧。」 一个刚创业没多久的朋友跟我...
强化 导航标记(面包屑):正相关到稍微正相关,全网站都会有的 Schema 强化项目主要是导航标记...
嘿不知不觉的就来到倒数第二篇了呢!网页也写完了呢!是不是要发布了哇! 今天就来说说 Github p...