diff --git a/Writerside/topics/Database-System.topic b/Writerside/topics/Database-System.topic index f7f0c6d..94390ae 100644 --- a/Writerside/topics/Database-System.topic +++ b/Writerside/topics/Database-System.topic @@ -493,14 +493,9 @@ -

- Join: - Form cross product of the - tables, output all tuples and select specific columns. -

-

- Example: -

+

Join: Form cross product of the tables, output all tuples and select + specific columns.

+

Example:

Sailors

@@ -646,9 +641,7 @@
-

- Arithmetic Expressions -

+

Arithmetic Expressions

SELECT S.age, S.age-5 AS age1, 2*S.age AS age2 FROM Sailors AS S @@ -656,13 +649,9 @@

Comparison is "=" not "==" !!!

-

- But SQLite allows this! -

+

But SQLite allows this!

-

- SQL Calculator -

+

SQL Calculator

SELECT log(1000) as three, @@ -684,9 +673,7 @@ 1 -

- String Comparison -

+

String Comparison

SELECT S.sname @@ -699,16 +686,8 @@ WHERE S.sname ~ 'B.*'; -

- Boolean Logic vs. Set Operators -

-

- Reserve a red - or - a - green boat - -

+

Boolean Logic vs. Set Operators

+

Reserve a red or a green boat

SELECT R.sid @@ -719,21 +698,15 @@ SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = 'red'; - \/ + UNION ALL - \/ + SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = 'green'; -

- Reserve a red - and - a - green boat - -

+

Reserve a red and a green boat

-- A boat cannot be red and green at the same time @@ -745,71 +718,107 @@ SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = 'red'; - \/ + INTERSECT - \/ + SELECT R.sid FROM Boats B, Reserves R WHERE R.bid = B.bid AND B.color = 'green'; -

- Set Semantics -

+

Set Semantics

Default (Think of each letter as being a tuple in a relation):

R = {A, A, A, A, B, B, C, D}

S = {A, A, B, B, B, C, E}

  • -

    - UNION: - {A, B, C, D, E} -

    +

    UNION: {A, B, C, D, E}

  • -

    - INTERSECT: - {A, B, C} -

    +

    INTERSECT: {A, B, C}

  • -

    - EXCEPT: - {D} -

    +

    EXCEPT: {D}

  • -

    - "ALL": Multiset Semantics -

    +

    "ALL": Multiset Semantics

    R = {A, A, A, A, B, B, C, D} = {A(4), B(2), C(1), D(1)}

    S = {A, A, B, B, B, C, E} = {A(2), B(3), C(1), E(1)}

  • -

    - UNION ALL (sum of all cardinalities): - - {A(6), B(5), C(2), D(1), E(1)} -

    +

    UNION ALL (sum of all cardinalities): {A(6), B(5), C(2), D(1), + E(1)}

  • -

    - INTERSECT ALL (min of cardinalities): - +

    INTERSECT ALL (min of cardinalities): {A(min(4,2)), B(min(2,3)), C(min(1,1)), D(min(1,0)), E(min(0,1))} - = {A, A, B, B, C} -

    + = {A, A, B, B, C}

  • -

    - EXCEPT ALL (subtract cardinalities): - - {A(4-2), B(2-3), C(1-1), D(1-0), E(0-1)} = {A, A, D} -

    +

    EXCEPT ALL (subtract cardinalities): + {A(4-2), B(2-3), C(1-1), D(1-0), E(0-1)} = {A, A, D}

  • + + +
  • +

    In

    +

    Example

    + + SELECT S.sname + FROM Sailors S + WHERE S.sid IN + (SELECT R.sid + FROM Reserves R + WHERE R.bid = 102); -- Subquery + +
  • + +
  • +

    Not In

    +

    Example

    + + SELECT S.sname + FROM Sailors S + WHERE S.sid NOT IN + (SELECT R.sid + FROM Reserves R + WHERE R.bid = 103); -- Subquery + +
  • + +
  • +

    Exists

    +

    Example 1 (bit odd, but legal)

    + + SELECT S.sname + FROM Sailors S + WHERE EXISTS + (SELECT * + FROM Reserves R + WHERE R.bid = 103); + /* The query will output all the sailors if there is at least one sailor who reserves boat 103 + or return nothing if there isn't */ + + +

    Example 2 (Nested Queries with Correlation)

    + + SELECT S.sname + FROM Sailors S + WHERE EXISTS + (SELECT * + FROM Reserves R + WHERE R.bid = 103 AND R.sid = S.sid); + /* The query will output all the sailors who reserve boat 103. + * In this example, the subquery will take every tuple of the sailors and run it, + * tuple by tuple. + */ + +
  • + +