Databases

Understanding data organization and usage

Lecture 1: Introduction to Database Concepts

Introduction to Relational Databases

The big picture

Databases : Why?


Centralized Modeling
Centralized Modeling
Per program modeling
Per program modeling
Program 1
Program 1
Program 2
Program 2
File 1
File 1
File 2
File 2
Program 1
Program 1
Program 2
Program 2
DB
DB
V.S
V.S
Viewer does not support full SVG 1.1

Databases : Goals


Databases : 3 different languages


Databases : 3 different languages


DDL - Data Definition Language

Commands to define and modify database structure:

Key point: DDL operations affect the schema, not the data itself

DML - Data Manipulation Language

Commands to manage data within tables:

Key point: DML operations work with data, not structure

Most of your daily work will be DML queries, especially SELECT

DCL - Data Control Language

Commands to control access and permissions:

Example scenarios:

Security principle: Give minimum necessary permissions

Table Structure


Table Structure (2)


Column  (attribute)
Column  (at...
Tuple
Tuple
1
1
France
France
FR
FR
2
2
Spain
Spain
ES
ES
3
3
Italy
Italy
IT
IT
Viewer does not support full SVG 1.1

Column types (most common ones)

Type Name Description
CHAR(n) Fixed length characters string
VARCHAR(n) Variable length characters string
TEXT Infinite length characters string
INT Integer numeric value
FLOAT(n) Floating point numeric value
DATE Stores the date only
TIME Stores the time only

Column types (Advanced)

Type Name Description Use Case
TIMESTAMP Stores date and time Audit trails, event logging
DATETIME Stores date and time with wider range Historical dates, future planning
JSON Stores structured documents Flexible schemas, API responses
BLOB Binary Large Object Images, files (not recommended)

Best Practice: Store files on disk/cloud, keep only references (URLs/paths) in database

Linking with other tables


Linking with other tables (2)


Primary Key
Primary Key
Foreign Key
Foreig...
1
1
France
France
FR
FR
2
2
Spain
Spain
ES
ES
3
3
Italy
Italy
IT
IT
1
1
Paris
Paris
1
1
2
2
Lyon
Lyon
1
1
3
3
Marseille
Marseille
1
1
Viewer does not support full SVG 1.1

ACID Properties

Databases ensure data integrity through ACID properties:

Property Description Example
Atomicity All or nothing Bank transfer: debit AND credit must both succeed
Consistency Valid state transitions Constraints are always respected
Isolation Concurrent operations don't interfere Two users booking the same seat simultaneously
Durability Saved data persists permanently Data survives system crash

Database Normalization - Why?

Example of redundancy problem:

Order1: Customer="John", Address="123 Main St", Product="Laptop"
Order2: Customer="John", Address="123 Main St", Product="Mouse"
→ If John moves, we must update multiple rows!

Solution: Separate Customers table + Orders table

Normal Forms Overview

Normal Form Requirement Eliminates
1NF Atomic values, no repeating groups Multi-valued attributes
2NF 1NF + No partial dependencies Dependencies on part of key
3NF 2NF + No transitive dependencies Dependencies on non-key attributes
BCNF 3NF + Every determinant is a candidate key Remaining anomalies

Most databases aim for 3NF as a practical balance

Types of Relationships

Type Description Example Implementation
One-to-One One record matches exactly one Person ↔ Passport FK in either table
One-to-Many One record matches many Customer → Orders FK in "many" table
Many-to-Many Many records match many Students ↔ Courses Junction table needed

Database vs Files: Advantages

Databases

  • ✓ Concurrent access control
  • ✓ ACID properties
  • ✓ Data integrity constraints
  • ✓ Query optimization
  • ✓ Backup and recovery
  • ✓ Security and permissions
  • ✓ Scalability

Files

  • ✓ Simplicity
  • ✓ No server needed
  • ✓ Portable
  • ✗ No integrity checks
  • ✗ Limited concurrency
  • ✗ Manual backup
  • ✗ No complex queries

Choosing Data Types: Best Practices

Wrong data types can lead to performance issues and data loss!

Recap: Key Takeaways

Next lecture: Entity-Relationship modeling and practical database design!

Practical Setup: Installing MySQL


To practice with MySQL, we'll use the ZIP distribution (no installer required)

This method gives you full control and is perfect for learning!

MySQL Setup: Initialize the Database


After extracting MySQL, initialize the data directory (first time only)

# Navigate to MySQL bin directory
cd C:\mysql\bin   # Windows
cd ~/mysql/bin    # macOS/Linux

# Initialize MySQL with insecure mode (no root password)
mysqld --initialize-insecure --console

# This creates the data directory and system tables
# Look for: [Server] /usr/local/mysql/bin/mysqld: ready for connections

--initialize-insecure creates root user with NO password (easier for learning)

MySQL Setup: Start the Server


Start MySQL server from command line

# Start MySQL server (run in one terminal)
mysqld --console

# You should see:
# [Server] X Plugin ready for connections on port 33060
# [Server] MySQL Server ready for connections on port 3306

# Leave this terminal running - this is your MySQL server!

Keep the server terminal open. To stop: Press Ctrl+C

MySQL Setup: Connect via Command Line


Open a new terminal and connect to MySQL

# Connect as root (no password needed with --initialize-insecure)
mysql -u root

# You should see:
# Welcome to the MySQL monitor.
# mysql>

# Try your first command:
mysql> SHOW DATABASES;
mysql> CREATE DATABASE test_db;
mysql> USE test_db;
mysql> CREATE TABLE hello (id INT, message VARCHAR(50));
mysql> INSERT INTO hello VALUES (1, 'Hello MySQL!');
mysql> SELECT * FROM hello;

Installing DBeaver (Visual Database Tool)


For a better experience, use DBeaver - a free, universal database client

Why DBeaver?

DBeaver: Create MySQL Connection


  • 1. Click "New Database Connection" button (plug icon)
  • 2. Select MySQL from the list
  • 3. Click "Next"

New connection button

Select MySQL (not PostgreSQL!)

DBeaver: Configure MySQL Connection


Click "Test Connection" to verify. DBeaver may download MySQL drivers automatically.

DBeaver: Start Working!


Summary: Your MySQL Setup is Complete!


You now have two ways to work with MySQL:

Method When to Use Pros
Command Line
mysql -u root
Quick queries, scripts Fast, lightweight, scriptable
DBeaver Exploration, complex queries Visual, autocomplete, ER diagrams

Next step: Complete the homework exercises using either tool!

Slide Overview