\documentclass[a4paper]{article}
\usepackage[english]{babel}
%\usepackage[dutch]{babel}
\usepackage{url}
%importeren
\usepackage{graphicx}

\title{Databases assignment 2: Relational Schemas and Relational Algebra Query Language }

\author{Sander van Veen \\ UvA ID: 6167969}

\begin{document}

\maketitle

%\bigskip

%\tableofcontents

%\pagebreak

\section{Question 5}
To answer this question, I assume that the casual SQL database table naming convention (plurar table names and underscores between two or more words) is required. But, in almost every programming-language naming convention, only classes start with an uppercase letter. Thus, I think the naming convention in the book is confusing, because of the uppercase letter for relation schemas. Therefore, I will use the ActiveRecord naming convention.

To prevent ambiquity, I will not use verbs as the name of a crosstable. For example, it is not clear if the table ``works'' deals with persons, who works for a company or companies, which works for other companies. This ambiquity can be prevented, if the table's name is "persons\_companies" or "companies\_companies", instead of just the name "works".

Below is the relational schema for the given online shop's ER diagram:
\\
\\
products( \underline{product\_name}, category, price )\\
software\_products( \underline{product\_name}, platform )\\
educational\_products( \underline{product\_name}, age\_group )\\
\\
companies( \underline{companies\_name}, stockprice )\\
companies\_persons( \underline{companies\_name}, \underline{ssn} )\footnote{Alternative table name: ``employs''} \\
companies\_products( \underline{companies\_name}, \underline{products\_name} )\footnote{Alternative table name: ``makes''}\\
\\
persons( \underline{ssn}, persons\_name, address )\\
persons\_products( \underline{ssn}, \underline{products\_name} )\footnote{Alternative table name: ``buys''}\\

\section{Question 6a}

\subsection{Find the driver-ids of all persons, whose names are ``Jack''.}
$\Pi_{driver\_id}( \sigma_{name} = ``Jack" (person) )$

\subsection{Find the names of all persons who have a ``Golf'' car.}
$\Pi_{name}( \sigma_{person.driver-id} = _{owns.driver-id} ( person \times ( \sigma_{car.license} = _{owns.license} ( owns \times ( \sigma_{name} = ``Golf" (car) ) ) ) ) )$

\subsection{Find the report numbers of those accidents that either happened in ``Amsterdam'' or for which their damage amount was more than 10,000 Euro.}
$\Pi_{participated.report-number}( \sigma_{participated.report-number} = _{accident.report-number} ( participated \times ( \sigma_{location} = ``Amsterdam'' (accident) ) ) \bigcup \sigma_{participated.damage-amount} > 10000 ( participated) )$

\subsection{Find the names of those drivers that had accidents in ``Amsterdam'' from 2008 to 2009, and for which their cars were ``Golf''.}
$\Pi_{name}( \sigma_{participated.report-number} = _{accident.report-number} ( participated \times ( \sigma_{location} = ``Amsterdam'' \bigwedge date >= 2008 \bigwedge date < 2009 (accident) ) ) ) \bigcap \Pi_{name}( \sigma_{person.driver-id} = _{owns.driver-id} ( person \times ( \sigma_{car.license} = _{owns.license} ( owns \times ( \sigma_{name} = ``Golf" (car) ) ) ) ) )$

\subsection{For each city find the total sum of damage amounts of the accidents in that city.}
$\Pi_{location, total\_damage}( location G_{sum(damage-amount)} as total\_damage(accident) )$

\pagebreak

\section{Question 6b}

\subsection{Find those employees who live in ``Amsterdam'' but work for companies located outside ``Amsterdam''.}
$ \sigma_{company.city} \neq ``Amsterdam'' \bigwedge \sigma_{company.company-name} = _{works.company-name} ( company \times ( \sigma_{employee.employee-id} = _{works.employee-id} ( works \times ( \sigma_{city} = ``Amsterdam'' (employee) ) ) ) ) $

\subsection{Find the employee-id values of those employees who earn more than every employee working for ``Philips''.}
$ \Pi_{employee-id}( \sigma_{salary} > _{max\_salary} ( works \times \Pi_{max\_salary}( _{max(salary)} as _{max\_salary}( \sigma_{company-name} = ``Philips'' (works) ) ) ) $

\subsection{Find the company that has the biggest number of employees.}
$ \sigma_{total\_employees} = _{max(total\_employees)}( company-nameGcount(employee-id) as total\_employees (works) ) $

\end{document}