Coding Bootcamp: Introduction to database design

Course tasks

Introduction

So far, we've just been working with one table at a time, and seeing what interesting data we can select out of that table. But actually, most of the time, we have our data distributed across multiple tables, and those tables are "related" to each other in some way

ER diagram

ER Diagram Components (1/3)

ER Diagram Components (2/3)

If the attributes are composite, they are further divided in a tree like structure. Every node is then connected to its attribute. That is, composite attributes are represented by ellipses that are connected with an ellipse.

ER Diagram Components (3/3)

ER Relationships


ER Relationships - Cardinalities (1/3)

ER Relationships - Cardinalities (2/3)

ER Relationships - Cardinalities (3/3)

ER Relationships - Relationship Loops

ISA (1/2)

ISA (2/2)

Structuring an ER (1/3)

Problem

Structuring an ER (2/3)

Solution

Structuring an ER (3/3)

Structuring an ER (cardinalities) (1/2)

Solution

Structuring an ER (cardinalities) (2/2)

Database Schema

Primaries and Foreign Keys

From ER to data base schema

DB Schema

Adding an attribute at a relationship

New DB Schema

SQL JOINS (1/3)

SQL JOINS (2/3)

OrderID CustomerID OrderData
10308 2 1996-09-18
10309 37 1996-09-19
10310 77 1996-09-20
CustomerID CustomerName ContactName Country
1 Alfreds Futterkiste Maria Anders Germany
2 Ana Trujillo Emparedados y helados Ana Trujillo Mexico
3 Antonio Moreno Taquería Antonio Moreno Mexico

SQL JOINS (3/3)

SQL JOIN

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID=Customers.CustomerID;

Result
| OrderID | CustomerName | rderDate |
| --- | --- | --- |
| 10308 | Ana Trujillo Emparedados y helados | 9/18/1996 |
| 10365 | Antonio Moreno Taquería | 11/27/1996 |
| 10383 | Around the Horn | 12/16/1996 |
| 10355 | Around the Horn | 11/15/1996 |
| 10278 | Berglunds snabbköp | 8/12/1996 |

Different SQL JOINs

INNER JOIN (1/2)

Returns all rows when there is at least one match in BOTH tables



## INNER JOIN (2/2)
SELECT * FROM TableA
INNER JOIN TableB
ON TableA.name = TableB.name

LEFT JOIN (1/2)

Return all rows from the left table, and the matched rows from the right table

LEFT JOIN (2/2)

SELECT * FROM TableA
LEFT OUTER JOIN TableB
ON TableA.name = TableB.name

RIGHT JOIN (1/2)

Return all rows from the right table, and the matched rows from the left table

RIGHT JOIN (2/2)

SELECT * FROM TableA
RIGHT OUTER JOIN TableB
ON TableA.name = TableB.name

FULL JOIN (1/2)

Return all rows from the left table, and the matched rows from the right table

FULL JOIN (2/2)

SELECT * FROM TableA
FULL OUTER JOIN TableB
ON TableA.name = TableB.name

ER in class Exercise (1/2)

ER in class Exercise (2/2)

Exercise

Exercise 1

Construct an ER diagram (Entities, Cardinalities, attributes etc.)

Exercise 2

Construct a DB schema

Exercise 3

Insert dummy grade values (varying from 1 to 10)

Exercise 4

Display those students that have participated in an (any) exam

Exercise 5

Display the students and all their grades (from all courses)

Exercise 6

Display the average grade of each student (from all courses)

Exercise 7

Display the first and last name of those students that have an average grade greater than 5

Exercise 8

Display the average grade per course (for all students)

Exercise 9

Display those students that participated at “SQL” course

Exercise 10

Display the max grade (of all courses), the First and the Last name, for each student that have a grade greater than 5 at “SQL” course


Creative Commons Licence
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.