SQL Primer

SQL Primer giulia Tue, 02/18/2020 - 11:44

In this section we are going to give an overview on basic operations that can be performed on a dataset using MonetDB

The SQL Language

The SQL Language giulia Mon, 03/16/2020 - 14:30

MonetDB offers full support for SQL syntax. For this reason, the main mean of interaction with the data is through SQL statements. 

Under construction

Create Views

Create Views mk Mon, 03/16/2020 - 15:26

The set consists of 8 tables, which are all bound to each other using FOREIGN KEY relationships. The voyages table is the main table, which all others refer to. Every table, except invoices has a PRIMARY KEY defined over the columns number and number_sup. Since the invoices table holds zero or more invoices per voyage (identified by number, number_sub) a PRIMARY KEY constraint is not possible. The tables craftsmen, impotenten, passengers, seafarers, and soldiers all share the same columns. We can define a VIEW that combines them all into one big table, to make them easier to access.

_________________________________________________________________________________________

sql>CREATE VIEW onboard_people AS
SELECT * FROM (
SELECT 'craftsmen' AS type, craftsmen.* FROM craftsmen
UNION ALL
SELECT 'impotenten' AS type, impotenten.* FROM impotenten
UNION ALL
SELECT 'passengers' AS type, passengers.* FROM passengers
UNION ALL
SELECT 'seafarers' AS type, seafarers.* FROM seafarers
UNION ALL
SELECT 'soldiers' AS type, soldiers.* FROM soldiers
UNION ALL
SELECT 'total' AS type, total.* FROM total
) AS onboard_people_table;

_________________________________________________________________________________________

The new view will show up and we can just use it as a normal table:

_________________________________________________________________________________________

sql>\d
TABLE  voc.craftsmen
TABLE  voc.impotenten
TABLE  voc.invoices
VIEW   voc.onboard_people
TABLE  voc.passengers
TABLE  voc.seafarers
TABLE  voc.soldiers
TABLE  voc.total
TABLE  voc.voyages

_________________________________________________________________________________________

Simple Queries

Simple Queries mk Mon, 03/16/2020 - 15:28

With the view that we just created, we can for instance calculate the number of records for each group of people:

_________________________________________________________________________________________

sql>SELECT type, COUNT(*) AS total 
FROM onboard_people GROUP BY type ORDER BY type;
+------------+-------+
| type       | total |
+============+=======+
| craftsmen  |  2349 |
| impotenten |   938 |
| passengers |  2813 |
| seafarers  |  4468 |
| soldiers   |  4177 |
| total      |  2467 |
+------------+-------+

_________________________________________________________________________________________

Let's see how many impotenten we have in the dataset:

_________________________________________________________________________________________

sql>SELECT count(*) FROM impotenten;
+------+
| L1   |
+======+
|  938 |
+------+

_________________________________________________________________________________________

It is possible to play with the set in many ways, to find out several things that took place during the voyages of the ships, or the money that was earned.

A few examples are shown below:

_________________________________________________________________________________________

sql>SELECT COUNT(*) FROM voyages
WHERE particulars LIKE '%_recked%';
+------+
| L1   |
+======+
|  358 |
+------+
sql>SELECT chamber, CAST(AVG(invoice) AS integer) AS average
FROM invoices
WHERE invoice IS NOT NULL
GROUP BY chamber
ORDER BY average DESC;
+---------+---------+
| chamber | average |
+=========+=========+
| A       |  282996 |
| Z       |  259300 |
| H       |  150182 |
| R       |  149628 |
| D       |  149522 |
| E       |  149518 |
| null    |   83309 |
+---------+---------+
sql>CREATE VIEW extended_onboard AS
SELECT number, number_sup, trip, trip_sup,
    onboard_at_departure, death_at_cape,
    left_at_cape, onboard_at_cape,
    death_during_voyage, onboard_at_arrival,
    death_during_voyage - left_at_cape AS death_at_arrival
FROM onboard_people;
sql>\d
TABLE  voc.craftsmen
VIEW   voc.extended_onboard
TABLE  voc.impotenten
TABLE  voc.invoices
VIEW   voc.onboard_people
TABLE  voc.passengers
TABLE  voc.seafarers
TABLE  voc.soldiers
TABLE  voc.total
TABLE  voc.voyages

_________________________________________________________________________________________