Bases de Données Relationnelles et SQL

Ludovic Deneuville

Objectives

What is a database?

  • Organized collection of data
  • Stored electronically
  • Structured to facilitate access and management
  • Designed to minimize redundancy

Relational databases

  • Organizes data into Tables
  • Interconnected tables
  • Structured tables

DBMS

A Database Management System (DBMS) is software that allows you to:

  • store
  • organize
  • manage data in a structured way

Examples of DBMS:

  • relational: PostgreSQL, MySQL, Oracle Database
  • NoSQL: MongoDB, Cassandra

What is it?

  • PostgreSQL: DBMS
  • DBeaver: client tool that allows connection to a DBMS
  • SQL: programming language designed to manage and manipulate relational databases.

Data types

There are many different data types that can be stored, the main ones are:

  • Character strings
  • Numeric values
  • Boolean values
  • Date and time
  • Geometric data

More details in the PostgreSQL documentation.

Types - example

CREATE TABLE personne (
    id_personne      SERIAL PRIMARY KEY,
    nom              VARCHAR(100) NOT NULL,
    prenom           VARCHAR(100) NOT NULL,
    date_naissance   DATE,
    email            VARCHAR(255) UNIQUE,
    nb_enfants       INT CHECK (nb_enfants >= 0),
    taille_m         DECIMAL(3, 2)
    est_actif        BOOLEAN,
    last_updated     TIMESTAMPTZ DEFAULT NOW() 
);

Table

A Table is composed of rows and columns:

  • a row represents a specific record

  • a column represents a particular attribute of these records

  • Primary key (PK): a column or set of columns that uniquely identifies each record in a table

    • allows a row to be identified unambiguously

Table - example

Relationships between tables

Foreign key

A foreign key (FK):

  • establishes a link between 2 tables
  • is a column of table A
  • corresponds to the primary key of table B

Types of relationships

  • 1..1: A Person has a Passport and a Passport belongs to a single Person

    • foreign key in one of the 2 tables
  • 1..*: A Player plays for a single Team. A Team is composed of several Players

    • foreign key in the Player table
  • *..*: A Student attends several Courses and a Course is attended by several Students

    • association table between Student and Course

SQL

  • SQL: Structured Query Language
  • Invented in 1970 by Edgar F. Codd
  • Programming language
  • Used to manage and manipulate relational databases

CRUD operations

SQL allows CRUD operations:

  • SELECT: retrieve data from a table
  • INSERT: insert new data into a table
  • UPDATE: update existing data
  • DELETE: delete data from a table

CRUD

Create, Read, Update, Delete

Actions on a table

Create a Table

Insert data

Delete a table

DROP TABLE personne;

If you then try:

SELECT *
  FROM personne;

ERROR: relation “personne” does not exist

Actions on rows

Select all

To display the entire contents of a table.

SELECT *
  FROM personne;

Filter rows

Update rows

Delete rows

Actions on columns

Select columns

Rename a column

Add an attribute

Delete a column

Rename when displaying

The AS keyword allows you to rename a column when displaying it.

⚠️ It does not change the name of the column.

Joins

Alias

Until now, we only had one table.

We therefore knew that the nom field came from the personne table.

What should we do if we join with a table that also has a column named nom?

SELECT p.nom       -- 2. p.nom: nom attribute of the table aliased as p, i.e. personne
  FROM personne p  -- 1. we declare p as an alias for the personne table

Full join

Types of joins

In the previous join:

  • Laure appears twice because she has 2 orders
  • Maud appears once
  • Ali does not appear

How can we include Ali in the table even though he has no order?

Outer joins

Aggregation

GROUP BY

  • Used to group results based on one or more columns
  • Allows the use of aggregate functions such as COUNT, SUM, AVG…

HAVING

To filter after a GROUP BY

Sort rows

Normal forms

First normal form

Other normal forms

  • 2NF: 1NF + every non-key attribute depends on the key
  • 3NF: 2NF + a non-key attribute cannot depend on another non-key attribute

Schemas

A good practice (not applied here) is to organize our tables into different schemas.

In the same way that you organize files into folders, you will find your way around more easily by organizing tables into schemas.

If you do not specify a schema when creating a table, it is placed in the public schema.

Schemas - example

Create a schema for the programming project

CREATE schema projet;


When creating the table, store it in the schema

CREATE table projet.joueuse(
  ...
);


Specify the schema in queries

SELECT *
  FROM projet.joueuse;

SQL without a table

SELECT CURRENT_DATE;
SELECT 1 + 2;
SELECT 1 > 2;
SELECT 'Hello';

Other concepts not covered

  • ACID, COMMIT, ROLLBACK
  • views, snapshot
  • indexes
  • privileges: grant / revoke
  • WITH (Common Table Expressions)
  • UNION, INTERSECT, EXCEPT
  • DISTINCT
  • EXISTS
  • SEQUENCE

Bibliography

Exercise

exercise