SQLとリレーショナル代数

ビュー

ビュー(view)とは仮想的なテーブルのことである. 保存されているわけではなく、問い合わせのたびにテーブルを作成する.

create view View[(column_1, ..., column_m)] as select query;

distinct句

行の重複を削除する.

select distinct column_1, ..., column_n from R;

order by句

行の並び替えを行う.

select * from R order by column [desc|asc];

複数テーブルからのデータ抽出

select * from R where F1 union select * from S where F2;
select * from R intersect select * from S;
select * from R except select * from S;

テーブルの結合

内部結合(inner join)

select * from students as G, credits as S where G.studentID = S.studentID;
select * from students as G join credits as S on G.studentID = S.studentID;
select * from students join credits using(studentID);

上の3つは等価である.

外部結合(outer join)

select * from students as G left outer join courses as R on G.studentID=R.studentID;
select * from students as G right outer join courses as R on G.student=R.studentID;

リレーショナル代数

リレーショナル代数SQL
和集合SELECT * FROM R UNION SELECT * FROM S
共通集合SELECT * FROM R INTERSECT SELECT * FROM S
差集合SELECT * FROM R EXCEPT SELECT * FROM S
直積集合SELECT * FROM R CROSS JOIN S
選択SELECT * FROM R WHERE F
射影SELECT B FROM R
内部結合SELECT * FROM R JOIN S ON F
自然結合SELECT * FROM R NATURAL JOIN S ON R.id = S.id
左外部結合SELECT * FROM R LEFT OUTER JOIN S ON R.id = S.id
右外部結合SELECT * FROM R RIGHT OUTER JOIN S ON R.id = S.id
完全外部結合SELECT * FROM R FULL OUTER JOIN S ON R.id = S.id