1.create table department
(dept name varchar (20),
building varchar (15),
budget numeric (12,2),
primary key (dept name));
create table r
(A1 D1,
A2 D2,
. . . ,
An Dn,
integrity-constraint1,
. . . ,
integrity-constraintk);
2.insert into instructor
values (10211, ’Smith’, ’Biology’, 66000);
3.delete from student;
//delete tuples from a relation
4.drop table r;
//deletes all information about the dropped relation from the database
5.alter table r add A D;
6.alter table r drop A;
单关系查询
7.select name from instructor;
8.select distinct dept name from instructor;
9.select all dept name from instructor;
10.select ID, name, dept name, salary * 1.1 from instructor;
11.select name from instructor where dept name = ’Comp. Sci.’ and salary > 70000;
注释:SQL允许在where子句中使用逻辑连词and,or,not
多关系查询
12.select name, instructor.dept name, building from instructor, department where instructor.dept name= department.dept name;
13.
select name, course id from instructor, teaches where instructor.ID= teaches.ID;
select name, course id from instructor natural join teaches;
14.select A1, A2, . . . , An from r1 natural join r2 natural join . . . natural join rm where P;
15.select name, title from instructor natural join teaches, course where teaches.course id = course.course id;
16.select name, title from (instructor natural join teaches) join course using (course id);
17.select name as instructor name, course id from instructor, teaches where instructor.ID= teaches.ID;
18.select T.name, S.course id from instructor as T, teaches as S where T.ID= S.ID;
19.select dept name from department where building like ’%Watson%’;
Percent (%): The % character matches any substring.
Underscore ( ): The character matches any character
20.select instructor.* from instructor, teaches where instructor.ID= teaches.ID;
21.select name from instructor where dept name = ’Physics’ order by name;
select * from instructor order by salary desc, name asc;
22.select name from instructor where salary between 90000 and 100000;
select name from instructor where salary <= 100000 and salary >= 90000;
23.select name, course id from instructor, teaches where (instructor.ID, dept name) = (teaches.ID, ’Biology’);
集合运算
24.(select course id from section where semester = ’Fall’ and year= 2009)
union
(select course id from section where semester = ’Spring’ and year= 2010);
25.(select course id from section where semester = ’Fall’ and year= 2009)
union all
(select course id from section where semester = ’Spring’ and year= 2010);
26.(select course id from section where semester = ’Fall’ and year= 2009)
intersect
(select course id from section where semester = ’Spring’ and year= 2010);
27.(select course id from section where semester = ’Fall’ and year= 2009)
except
(select course id from section where semester = ’Spring’ and year= 2010);
3.7聚集函数——基本聚集
28.Average: avg
• Minimum: min
• Maximum: max
• Total: sum
• Count: count
select avg (salary) from instructor where dept name= ’Comp. Sci.’;
select avg (salary) as avg salary from instructor where dept name= ’Comp. Sci.’;
29.select count (distinct ID) from teaches where semester = ’Spring’ and year = 2010;
30.select count (*) from course;
3.7.2聚集函数——分组聚集
31.select dept name, avg (salary) as avg salary from instructor group by dept name;
(没搞懂到底是排序还是进行后续的分组)
32.select avg (salary) from instructor;
33.select dept name, count (distinct ID) as instr count from instructor natural join teaches
where semester = ’Spring’ and year = 2010 group by dept name;
3.7.3 having子句……
参考