SQL part1) select


select


その結果、ユーザーが表示したいプロパティのリストが表示されます.
  • 関係代数に対応する工程操作
  • :すべての属性の意味
  • 算術式+システム提供関数(log、squareなど)は
  • で使用できます.
  • 繰り返し可能tuple
    distinctキーを使用すると
  • の繰り返しは許可されません
  • ex) Select distinct deptName from professor;//Find the names of all departments with professors, and remove duplicates
  • の繰り返しを許可するのは間違いで、allキーワードを使用します
  • ex) Select all deptName from professor;
  • お寺


    tupleが満たさなければならない条件を明確にする
  • 線形代数のselect述語で、
  • に対応する
  • 論理演算子の提供
  • and, or, not
  • 算術連選者より上位にランクインした.
  • ex) select name from professor where deptName='CS' and salary > 8000;
  • お寺から


    クエリーに関連するテーブルのリスト
  • 表を表示すると、デカルト乗算に対応します.
  • select * from professor, teaches;//デカルト乗算
  • カタシオン乗算自体ではなく、where節とともに使用される
  • 実行順序

  • fromセクションで指定する各テーブルから各tuple
  • を抽出する.
  • 抽出スープはwhere条件
  • に適している.
  • whereは、条件判定のtupleをgroupbyセクションに送信し、実際に作成されたすべてのtupleの組合せを求め、groupbyプロパティを使用して中間結果をグループ化する.
  • 各サブグループに条件付真条件付サブグループ
  • を適用する.
  • は、これらのサブグループを条件に並べ替えて結果テーブルを形成する.
  • join


    joinは、共通カラムを持つtuple間で新しいテーブルを作成します.
    内部接続はOを繰り返しXを繰り返すことを許可する
    |
    ex)
    // natrual join. 7개
    Select * __from professor natural join teaches__; // inner(=default) join. 8개
    Select * __from professor, teches__ where professor.pID = teches.pID; 

    (inner) join

  • find professor names and course IDs they taught
  • Select name, cId // 3. 이름과 cID 검색
    from professor, teaches // 1. 중간테이블 만들어짐
    where professor.pId=teaches.pID; // 2. 둘의 pID가 같은 튜플 중
  • find the titles of courses offered by the ‘CS’ department, and names of professors who teach the courses
  • Select title, name // 3. title, name만 추출
    from teaches, course, professor // 1. 이 테이블들의 카티시안곱 테이블 만들어낸 다음
    where teches.cID=course.cId and teches.pID=professor.pID and course.deptName='CS'; // 2. 조건들 만족시키는 튜플 들 중
    

    natural join


    表にナチュラル結合を適用する場合は、同名のプロパティによって望ましくない結合演算が発生しないように注意してください.
    例)
    // 잘못된 ver
    Select name, title
    from professor natural join teches natural join course;
    
    // 올바른 ver1
    Select name, title
    from professor natural join teaches, course // prof<-> teaches는 Natural join 하고 그 뒤 course는 일반 조인
    where teaches.cID = course.cID;
    
    // 올바른 ver2
    Select name, title
    from (professor natural join teaches) join course using(cID);
    
    // 올바른 ver3
    Select name,title
    from teaches, course, professor
    where dteaches.cID=course.cID and teaches.pID=professor.pID;

    Rename "as"

  • の新しい名前で名前を変更します.
  • Select pID, name, salary/12 as monthlySalary from professor;
  • 省略可能
  • ex) professor as T = professor T
  • ex) Select sID, name myName, deptName from student;
  • テーブル名も再命名可能
  • ex) Select distinct T.name from professor as T, professor as S where T.salary > S.salary and S.deptName='CS';
  • 文字列比較演算


    like演算子+パターン(%,など)
  • %
  • 長さに関係のない任意の文字列(空の文字列を含む)
  • Select name from professor where name like '%da%'
  • _
  • any single char
  • 混同を避ける
  • ex)「100%」を検索する場合、タイトルは「100%」escape「

  • に加えて、concat(|)、大文字と小文字の変換、抽出など、複数の文字列操作が存在する.

    order by


    order byを使用して他のテーブルを作成するのではなく、結果テーブルに入るtupleの順序を変更します.
  • シーケンス
  • 降順:「desc」
  • 昇順:「asc」(default)
  • Select distinct name from professor order by name;
  • では、マルチプロパティのソートも可能です.
  • Order by deptName desc, name
  • between

  • whereセクションで使用可能な演算子.値区間(境界値を含む)
  • を表します.
  • between A and B
  • To find the names of all professors with salary between 5000 and 6000 including boundaries
  • Select name from professor where salary between 5000 and 6000;```
  • チュートリアルの比較


    ex)
    Select name, cID
    from professor, teaches
    where (professor.pID, deptName) = (teaches.pID, 'CS') // 튜플 비교

    中伏


    SQLはtuple反復を許可する(i.e.multiset)
  • 演算時:入力テーブルのすべてのパターンに対して重複しない演算
  • を行う.
  • 結果:すべての凡例は結果テーブルに属し、重複はありません.
  • しゅうごうえんざん


    SQLは集約演算もサポート
  • セット、X
  • の繰り返しを許可
  • allキーワードの使用時に重複を許可(multiset)
  • union all, intersect all, except all
  • union
  • m+n回「r union alls」/「double」、
  • intersect
  • min(m, n) times in “r intersect all s”
  • except
  • max(0, m–n) times in “r except all s
  • Find course IDs that ran in Fall 2009 or in Fall 2010
  • (select cID from teaches where semester='Fall' and year = 2009) union (select cID from teaches where semester='Fall' and year=2010);
  • Find course IDs that ran in Fall 2009 and in Fall 2010
  • (select cID from teaches where semester='Fall' and year=2009) intersect (select cID from teaches where semester='Fall' and year=2010)
  • Find course IDs taht ran in Fall 2009 but not in Fall 2010
  • (select cID from teaches where semester='Fall' and year=2009) except (select cID from teaches where semester='Fall' and year = 2010);
  • Null


    値が存在する場合、値が存在するかどうか、または値が存在するかどうかなどは不明です.
  • NULL処理
  • の予想外の結果をもたらすこともあります.
  • nullを含む算術演算結果はNULL
  • 5 + null = null
  • は存在しますか?
  • is null/is not null
  • select name from professor where salary is null;
  • nullを含む比較演算結果は不明です
  • 5 < null, null <> null, null = null
  • Three-valued logic
  • 歳の値論理を使用すると、SQL select文のwhereセクションの結果は真/偽/不明であり、whereセクションの結果が真の場合にのみユーザーに返されます.
  • 節の結果を知らない場合、結果はユーザに返されない.
  • unknown=0.5
  • true AND false = min(1,0)=0=false
  • true OR unknown = max(1,0.5)=1=true
  • not false=1-0=1=true
  • not unknown=1-0.5=0.5=unknown
  • Exercises

  • Find student names and department names of students whose GPA is below 2.0
  • Select name, deptName
    from student
    where GPA < 2.0;
  • Find the student names who have taken at least one course of ‘CS’ department. Make sure that there are no duplicate names in the result
  • select distinct name
    from student, course, takes
    where takes.cID=teaches.cID and teaches.cID='CS' and student.sID=takes.sID and student.cID=teaches.cID;
    
    select distinct name
    from student, (course natural join takes) using (sID)
    where course.deptName='CS'
    where 
  • Retrieve the chairperson name, his/her salary, and the name of department he/she is in charge of
  • Select name, salary, department.deptName
    from professor, department
    where department.chairman=professor.pID
    
    // wrong ver
    Select name, salary, department.deptName
    from department, professor
    where department.deptName = professor.deptName;
    次の2番目のエラーverは、わけのわからない「教授が所属する学科」を探している.
    ただ、教授.deptName属性値には空の値が存在し、空の値を持つturpleは結果関係に結合されません.したがって、2番目のクエリは次のクエリと同じです.
    Select name, salary, deptName
    from professor
    where deptName is not null;
  • List student names in a descending order of GPA (highest comes first)
  • select name
    from student
    order by GPA desc;