diff --git a/chapter1.html b/chapter1.html index f611c67..c42fd6a 100644 --- a/chapter1.html +++ b/chapter1.html @@ -21,6 +21,8 @@

Software used

To perform a SQL query to a database, enter the command in the text field and click on Execute Command. As an alternative to Sqlitebrowser you can use Sqliteman which is a good alternative especially for Debian and Ubuntu Linux users. Please refer to the installation guide for details.

+

+

Sample database

As the focus of this tutorial lies on the retrieval of data from an existing database rather than creating new databases, a file databaseOTS.sqlite is provided which contains all the data required later on in the course. Download this file to some location on your computer. Then you can open it in DB Browser for SQLite by clicking on open database.

DB Browser for SQLite

diff --git a/chapter2.html b/chapter2.html index d35db30..deb23d9 100644 --- a/chapter2.html +++ b/chapter2.html @@ -17,14 +17,13 @@

Simple queries

person table

phone_contract table

-

We want to see what is in the table person:

select * from person;

This is too much information as we are only interested in the name:

select name from person;

In general:

select col1, col2, ... from tablename;
-

We can get the number of lines of results by using the count command. To get the number of entries in the person table we use

+

Note that select * selects all columns of the respective table. We can get the number of lines of results by using the count command. To get the number of entries in the person table we use

select count(*) from person;

Results might contain duplicates (they form a multi-set). We can get only the distinct results by

select distinct col1,col2, ... from tablename;
@@ -37,12 +36,8 @@

Constraints

select name, residence from person where age > 30;

For the different type of data, different constrains make sense. For all kind of data, we can check for equal = and not equal !=. For numbers, one can also compare for larger >, larger or equal >=, smaller <, smaller or equal <=.

-<<<<<<< HEAD -

For text, we use like, to find entries where the text value in a column contains a string, where you can use the % as a wild-card for unknown parts of a word. E.g. if you search for '%an%', the following words would fit: Banana, fantasy. Note that in SQLite, the like operator is case insensitive by default. +

For text, we use like, to find entries where the text value in a column contains a string, where you can use the % as a wild-card for unknown parts of a word. E.g. if you search for '%an%', the following words would fit: Banana, fantasy. Note that in SQLite, the like operator is case insensitive by default. -======= -

For text, we use like, to find entries where the text value in a column contains a string. The string should be surrounded by single quotes ', and you can use the % as a wild-card for unknown parts of a word. E.g. if you search for '%an%', the following words would fit: Banana, fantasy, but not Anna, since this is a Capital A.

->>>>>>> 0051abf36686309bb132c5e0d7ed6717a9bb879b

3.) Get all people whose names contains "oh".

In SQLite the >,<,= operators for dates, do an lexicographical comparison, not a numeric one. This means that if there is an inconsistent representation of dates, the comparison might fail even though the two values represent the same dates. E.g. if once the format "2014-10-23" and once the format "2014/10/16" is used. Note that here double quotes " are used instead of single quotes '.

There is a table named flight containg flight data. The column with the dates is called date. @@ -59,7 +54,7 @@

Constraints

Constraints can be combined by the logical operator and and the operator or.

6.) Find all people whose first name is Carlos and who are older than 20 years.

There is another table which is called phone_contract. The column containing the status (active, non-active) is called status.

-

7.) Find out, how many contracts there are and how many accounts are active.

+

7.) Find out, how many contracts there. With a second query find out how many of them are and how many accounts are active.

Results from more than one table

Usually, it is necessary to get information which is spread over more than one table. For example, you may want to know the names of the people who have an active phone contract.

@@ -73,7 +68,7 @@

Results from more than one table

select p.name, c.phone_number from person p, phone_contract c
 where c.name = p.name;

As we did not want to write the complete table name, we have defined and abbreviation in the from clause (person p).

-

9.) Find all names who have an active phone contract.

+

9.) Find all names and ages of persons who have an active phone contract.

10.) Find all names and phone numbers of people who are older than 30.

Sub-queries

@@ -91,7 +86,7 @@

Sub-queries

If you have no more question concerning basics SQL queries, you can procede to the next chapter and solve the theft of the Mona Lisa.

Solutions

-Sub +

Back to start

diff --git a/chapter2_solutions.html b/chapter2_solutions.html index b6e21fe..adae640 100644 --- a/chapter2_solutions.html +++ b/chapter2_solutions.html @@ -61,8 +61,8 @@

Solutions - SQL Basics

  • Use

    -select person.name from phone_contract where status = 'active'
    -and person.name = phone_contract.name;
    +select person.name, person.age from person, phone_contract
    +where person.name=phone_contract.name and status = 'active';
     
  • Use

    diff --git a/chapter3.html b/chapter3.html index 5e7c4de..33c3872 100644 --- a/chapter3.html +++ b/chapter3.html @@ -21,7 +21,7 @@

    Which tables are there? - System tables

    Remarks: In other databases (e.g. Oracle, DB2 etc.) this meta information is stored differently, i.e. the system tables are always called differently. Sometimes there is more than one system table. When working with another database than sqlite you need to google for the names of the system tables of that database.

    How to find the thief

    -

    For finding the thief we have the following plan:

    +

    When you follow the tutorial you will step-by-step:

    1. Find out, who was in Paris at that time.
    2. The thief was probably not working alone. Is there any suspicious communication during the time in question?
    3. @@ -60,19 +60,12 @@

      How information of different tables are cleanly connected: Key constraintsWho is the thief? - Order by and group by

      To find out who is the thief, check the text messages stored by the mobile phone providers.

      -<<<<<<< HEAD -

      23.) How is the name of the table containing the text messages and the one containing phone contracts?

      -

      24.) Get all text messages  which where sent between 2014-10-20 and 2014-10-25. -

      25.) Get all phne contract ids where the phone_contract.person_id is equal to the id of one of the suspects.

      -

      26.) Get all text messages where the sent date is the 23.10.2014 and the sender or receiver is one of our suspects.

      - -=======

      23.) What are the names of the table containing the text messages and the one containing phone contracts?

      24.) Get all text messages  which where sent between 2010-10-20 and 2010-10-25. -

      25.) Get all contract ids where the contract.name_id is equal to one of the persons from the results of question 15.

      -

      26.) Get all text messages where the sent date is the 23.10.2014 and the contract_sender_id is equal to the contract ids where the contract.name_id is equal to one of the persons from the results of question 15.

      ->>>>>>> 0051abf36686309bb132c5e0d7ed6717a9bb879b +

      25.) Get all contract ids where the contract.name_id is equal to one of the persons from the results of question 19.

      +

      26.) Get all text messages where the sent date between 2010-10-20 and 2010-10-25 and the contract_sender_id is equal to the contract ids where the contract.name_id is equal to one of the persons from the results of question 19.

      +

      You see that you got all the required information but the output looks kind of chaotic. You can order an result set according to a column with an order by phrase. The query to get all text messages from 21.10.2014 ordered by time reads

       select message from messages where sent like '2014-10-21%'
      diff --git a/chapter3_solutions.html b/chapter3_solutions.html
      index c3448ed..a747adc 100644
      --- a/chapter3_solutions.html
      +++ b/chapter3_solutions.html
      @@ -11,7 +11,6 @@
       

      Solutions - The theft of the Mona Lisa

      -
      1. Use the select command on the table sqlite_master and pragma table_info on all tables present in the database.

      2. @@ -108,7 +107,7 @@

        Solutions - The theft of the Mona Lisa

         select id from phone_contract where phone_contract.person_id
         in (select distinct person.id from person, flight
        -where	residence = 'Paris' or 
        +where residence = 'Paris' or 
         (flight.person_id = person.id
         and dest_city = 'Paris' and date < "2014-10-23"
         and flight.name in (select flight.name from flight
        diff --git a/databaseOTS.sql b/databaseOTS.sql
        index a046f83..74c73b9 100644
        --- a/databaseOTS.sql
        +++ b/databaseOTS.sql
        @@ -190,7 +190,7 @@ INSERT INTO bank_transaction values (430,2655480,null,'Cash withdrawal',443.17);
         
         
         INSERT INTO person values (100, 'Philipp', 'Hamburg', 42);
        -INSERT INTO person values (101, 'Phillipp', 'Mumbai',75);
        +INSERT INTO person values (101, 'Philip', 'Mumbai',75);
         INSERT INTO person values (102, 'Carlos', 'Paris', 21);
         INSERT INTO person values (103, 'Wei', 'Paris', 36);
         INSERT INTO person values (104, 'John', 'Sydney', 37);
        diff --git a/databaseOTS.sqlite b/databaseOTS.sqlite
        index 68063f3..4e30086 100644
        Binary files a/databaseOTS.sqlite and b/databaseOTS.sqlite differ
        diff --git a/installationguide.html b/installationguide.html
        index 78ad80f..198acf3 100644
        --- a/installationguide.html
        +++ b/installationguide.html
        @@ -21,10 +21,10 @@ 

        Microsoft Windows

        To get SQLite, download a zip archive from the SQLite project web page. This zip-archive contains an executable file sqlite3.exe. Copy this file to some place on your computer (the desktop should be fine). Double-click on it to start the program.

        -

        To get the graphical user interface DB Browser for SQLite, formerly called Sqlitebrowser, just download the executable installer file. Double-click in it to run it and agree to the license to finish the installation.

        +

        To get the graphical user interface DB Browser for SQLite, formerly called Sqlitebrowser, just download the executable installer file. Double-click in it to run it and agree to the license to finish the installation. Unfortunately, the installer does not work with 64bit Windows. If you use this operating sytem, you can download the zipped file, save the contained .exe-file to some location on your computer and run it by double-clicking on it (no installation required).

        MacOS

        -

        To get SQLite, download a zip archive from the Sqlite project web page. This zip-archive contains an executable file sqlite3. Copy this file to some place on your computer (the desktop should be fine). Right click on the file and select open and confirm the security message.

        +

        To get SQLite, download a zip archive from the Sqlite project web page. This zip-archive contains an executable file sqlite3. Copy this file to some place on your computer (the desktop should be fine). Right click on the file and select open and confirm the security message. On some systems, this procedure will fail for security reasons. In this case you need to allow the installation of unsigned applications in the system preferences.

        To get the graphical user interface DB Browser for SQLite, formerly called Sqlitebrowser, just download the MacOS drive image. Open it and copy the contained file to some location on your computer. Right click on the file and select open and confirm the security message.

        Linux