Datenbankpraktikum - Exercises
Exercises
- conceptual design (19.9.2017)
- logical design (26.9.2017)
- no database lab on October 3 and 10!!
- Consistency constraints (17.10.2017)
- Updates (17.10.2017)
- Queries (24.10./31.10./7.11. 2017)
- Transactions (14.11.2017)
- User-defined functions and stored procedures (21.11. 2017)
- Views (28.11.2017): Note: this topic will be done in self-study mode, there will be no session at IfI on November 28!
- Trigger (05.12.2017)
- JDBC (12.12./19.12.2017)
Logical Design
In the second lesson, we map the conceptual design onto a logical design. In addition to mapping entity types and relationships, please pay particular attention to the following cases:
- how to implement specialization hierarchies;
- how to deal with attributes whose type is again structured (such as AddressT);
- how to implement static attributes;
- how to handle domains (and whether to use them at all);
- how to handle enumeration types (such as extras in Vehicle).
The Postgres manual about DDL can be found here (sections 5.1 and 5.9). In particular, read the section on inheritance. For the syntax definition how to create types and tables, see here and here.
Constraints
Define primary key, foreign key, uniqueness, not null, and semantic (check) constraints for the logical schema. See here for the relevant parts in the manual.
Solutions are here.
Updates
Execute updates (Insert, Update, Delete) on your database. Try to violate the constraints you previously defined. (There is no reference solution for this topic).
Queries
Documentation
- Queries are described in the Postgres-Manual: here and here.
- Slides with examples for analytic queries, temporary views, and recursive queries are here
- Sample code for recursive queries is here
- Sample code for analytic queries is here
- Sample code for super groups is here
Sample Data
In order to have sufficient data to test, please load a database dump containing the table definitions and data. The dump file is here (the dump file has been replaced on October 26 and should now work). Restore the dump as follows:
- create a database user cashu (if it does not yet exist)
- create a database cashdb (if it does not yet exist) owned by user cashu
- Rename the file from "cashdb-plain.txt" to "cashdb-plain.tar".
- Create the database cashdb and restore it in pgadmin3
(In the object browser, navigate to "Databases", then right-click "cashdb"
and select "restore")
. Alternatively, execute the following command in a shell or command window:pg_restore -c -d cashdb -U cashu -v cashdb-plain.tar
In case pg_restore cannot be found (may happen on Windows), execute the script pg_env.bat first. This script is usually in the installation directory of PostgreSQL.
Exercises
A page with the SQL exercises can be found
here.
You can also download the SQL exercises
here.
Please record your own solutions in a file so that they can be compared to the reference solution later.
Solutions ...
... are here.
Transactions
Documentation about transactions is here in the manual.
The slides shown in the lecture are hereExercises can be downloaded here.
Solutions are here.
Views
Preparation: Views are documented
here.
In the exercises on views we will also cover other approaches how to implement specialization
hierarchies. The script containing the table definitions for the two hierarchies
(Member and Vehicle) without table inheritance are
here. A script for filling the tables is
here.
Exercises are
here.
Reference solutions are
here.
User-defined Functions and Stored Procedures
Preparation: Chapter 36, 40, 41
in the manual. We will only use SQL and PL/pgSQL as
implementation languages. You thus do not need to read the sections on PL/TCL,
PL/Perl and PL/Python etc.
Slides are
here.
Here are exercises for
functions and procedures.
Trigger
Preparation: Chapter 36 and 37
in the manual. Slides are
here.
Here are the exercises for
Triggers.
Solutions are
here
and
here.
JDBC
A short introduction into JDBC is
here.
An introduction into JDBC on the Postgres JDBC driver page:
link.
Until December 12 you should have a Java development environment installed and should be familiar
with it. Possible IDEs are
(NetBeans,
Eclipse, JDeveloper, ...). In the lab, we will show first steps with NetBeans.
Please also download a JDBC-driver for Postgres (e.g. the Type-4-driver
here).
The goal of the JDBC-exercise is to complete several
Java
programs with the appropriate JDBC-calls.
Complete the programs in the following sequence:
- ListPersMembers.java
- GetMembers.java
- OpenReservations.java
- CancelReservations.java
- CreateInvoices.java