classDiagram
class Joueuse {
id_joueuse: INT PK
nom: VARCHAR
prenom: VARCHAR
date_naissance: DATE
pays: VARCHAR
}
TablesA Database Management System (DBMS) is software that allows you to:
Examples of DBMS:
There are many different data types that can be stored, the main ones are:
More details in the PostgreSQL documentation.
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
Physical Data Model (UML)
classDiagram
class Joueuse {
id_joueuse: INT PK
nom: VARCHAR
prenom: VARCHAR
date_naissance: DATE
pays: VARCHAR
}
| id_joueuse (PK) | nom | prenom | date_naissance | pays |
|---|---|---|---|---|
| 1 | Sebag | Marie | 1986-10-15 | France |
| 2 | Polgar | Judit | 1976-07-23 | Hungary |
| 3 | Hou | Yifan | 1994-02-27 | China |
| 4 | Kosteniuk | Alexandra | 1984-04-23 | Switzerland |
| 5 | Ju | Wenjun | 1991-01-31 | China |
A foreign key (FK):
1..1: A Person has a Passport and a Passport belongs to a single Person
1..*: A Player plays for a single Team. A Team is composed of several Players
*..*: A Student attends several Courses and a Course is attended by several Students
classDiagram
direction LR
class Joueuse {
id_joueuse: INT PK
nom: VARCHAR
prenom: VARCHAR
date_naissance: DATE
code_pays: VARCHAR FK
}
class Pays {
code_pays: VARCHAR PK
nom: VARCHAR
}
Joueuse "*" -- "1" Pays : BelongsTo
| id_joueuse | nom | prenom | date_naissance | code_pays |
|---|---|---|---|---|
| 1 | Sebag | Marie | 1986-10-15 | FR |
| 2 | Polgar | Judit | 1976-07-23 | HU |
| 3 | Hou | Yifan | 1994-02-27 | CN |
| 4 | Kosteniuk | Alexandra | 1984-04-23 | CH |
| 5 | Ju | Wenjun | 1991-01-31 | CN |
| code_pays | nom |
|---|---|
| CH | Switzerland |
| CN | China |
| FR | France |
| HU | Hungary |
classDiagram
direction LR
class Joueuse {
id_joueuse: INT PK
nom: VARCHAR
prenom: VARCHAR
date_naissance: DATE
code_pays: VARCHAR FK
}
class Tournoi {
id_tournoi: INT PK
nom: VARCHAR
ville: VARCHAR
}
class Participation {
id_joueuse: INT FK
id_tournoi: INT FK
}
Joueuse "*" .. "1" Participation
Participation "1" .. "*" Tournoi
| id_joueuse | nom | prenom | date_naissance | code_pays |
|---|---|---|---|---|
| 1 | Sebag | Marie | 1986-10-15 | FR |
| 2 | Polgar | Judit | 1976-07-23 | HU |
| 3 | Hou | Yifan | 1994-02-27 | CN |
| 4 | Kosteniuk | Alexandra | 1984-04-23 | CH |
| 5 | Ju | Wenjun | 1991-01-31 | CN |
| id_joueuse | id_tournoi |
|---|---|
| 1 | 1 |
| 1 | 2 |
| 3 | 2 |
| 4 | 1 |
| 4 | 2 |
| id_tournoi | nom | ville |
|---|---|---|
| 1 | Norway Chess | Oslo |
| 2 | Tata Steel | Wijk aan Zee |
SQL allows CRUD operations:
SELECT: retrieve data from a tableINSERT: insert new data into a tableUPDATE: update existing dataDELETE: delete data from a tableCRUD
Create, Read, Update, Delete
| id_personne | nom | prenom | date_naissance | adresse |
|---|---|---|---|---|
| 1 | Gatore | Ali | 1990-05-15 | Amiens |
| 2 | Dure | Laure | 1985-09-22 | Auxerre |
| 3 | Erateur | Maud | 1995-03-10 | Lille |
If you then try:
ERROR: relation “personne” does not exist
To display the entire contents of a table.
Tip
For better readability, align your code!
| id_personne | prenom | nom | date_naissance | adresse |
|---|---|---|---|---|
| 2 | Laure | Dure | 1985-09-22 | Auxerre |
The LIKE clause is used to search for specific text in a text column.
% represents zero, one, or more characters_ represents a single characterThe AS keyword allows you to rename a column when displaying it.
⚠️ It does not change the name of the column.
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?
USING
If and only if the 2 columns used to perform the join have the same name.
↪️ You can then use this syntax with USING.
| id_personne | nom | prenom | dnais | adresse |
|---|---|---|---|---|
| 1 | Gatore | Ali | 1990-05-15 | Amiens |
| 2 | Dure | Laure | 1985-09-22 | Amiens |
| 3 | Erateur | Maud | 1995-03-10 | Lille |
Create and insert data
CREATE TABLE commande (
id_commande INT PRIMARY KEY,
produit VARCHAR(50),
quantite INT,
prix_unitaire DECIMAL(10, 2),
id_personne INT,
FOREIGN KEY (id_personne) REFERENCES personne(id_personne)
);
INSERT INTO commande (id_commande, produit, quantite, prix_unitaire, id_personne) VALUES
(1, 'livre', 1, 10, 2),
(2, 'pain', 3, 2, 3),
(3, 'pomme', 10, 0.5, 2);| id_commande | produit | quantite | prix_unitaire | id_personne |
|---|---|---|---|---|
| 1 | livre | 1 | 10 | 2 |
| 2 | pain | 3 | 2 | 3 |
| 3 | pomme | 10 | 0.5 | 2 |
| prenom | produit | quantite |
|---|---|---|
| Laure | livre | 1 |
| Laure | pomme | 10 |
| Maud | pain | 3 |
In the previous join:
How can we include Ali in the table even though he has no order?
LEFT JOIN means that we keep all the content from the previous tableRIGHT JOIN does the opposite| prenom | produit | quantite |
|---|---|---|
| Laure | livre | 1 |
| Laure | pomme | 10 |
| Maud | pain | 3 |
| Ali |
The ****outer join**** performed using the LEFT JOIN keyword indicates that we display:
all data from the ****people**** table
supplemented with data from the ****orders**** table
To filter after a GROUP BY
A relation is in first normal form (1NF) if
Atomic attribute
| id | nom |
|---|---|
| 1 | Ali Gator |
| 2 | Laure Dure |
| 3 | Maud Erateur |
This table does not comply with 1NF because its nom attribute is not atomic.
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.
Create a schema for the programming project
When creating the table, store it in the schema
Specify the schema in queries