Sure! Below are the SQL CREATE TABLE statements for your PostgreSQL database based on the provided schema. The tables are created in an order that respects foreign key dependencies. Each table includes primary keys (PK) and foreign keys (FK) as specified.
-- Table: Departement
CREATE TABLE Departement (
Code INTEGER PRIMARY KEY,
Nom VARCHAR(100) NOT NULL,
Domaine VARCHAR(100) NOT NULL
);
-- Table: Enseignant
CREATE TABLE Enseignant (
ID INTEGER PRIMARY KEY,
DepartementCode INTEGER NOT NULL,
Prenom VARCHAR(100) NOT NULL,
Nom VARCHAR(100) NOT NULL,
Courriel VARCHAR(100) NOT NULL,
CONSTRAINT fk_enseignant_departement
FOREIGN KEY (DepartementCode)
REFERENCES Departement(Code)
ON UPDATE CASCADE
ON DELETE RESTRICT
);
-- Table: Etudiant
CREATE TABLE Etudiant (
ID INTEGER PRIMARY KEY,
Prenom VARCHAR(100) NOT NULL,
Nom VARCHAR(100) NOT NULL,
Courriel VARCHAR(100) NOT NULL
);
-- Table: Matiere
CREATE TABLE Matiere (
ID INTEGER PRIMARY KEY,
EnseignantID INTEGER NOT NULL,
DepartementCode INTEGER NOT NULL,
Nom VARCHAR(100) NOT NULL,
Duree INTEGER NOT NULL,
CONSTRAINT fk_matiere_enseignant
FOREIGN KEY (EnseignantID)
REFERENCES Enseignant(ID)
ON UPDATE CASCADE
ON DELETE SET NULL,
CONSTRAINT fk_matiere_departement
FOREIGN KEY (DepartementCode)
REFERENCES Departement(Code)
ON UPDATE CASCADE
ON DELETE SET NULL
);
-- Table: Matiere_Etudiant
CREATE TABLE Matiere_Etudiant (
MatiereID INTEGER NOT NULL,
EtudiantID INTEGER NOT NULL,
Note INTEGER,
PRIMARY KEY (MatiereID, EtudiantID),
CONSTRAINT fk_matiere_etudiant_matiere
FOREIGN KEY (MatiereID)
REFERENCES Matiere(ID)
ON UPDATE CASCADE
ON DELETE CASCADE,
CONSTRAINT fk_matiere_etudiant_etudiant
FOREIGN KEY (EtudiantID)
REFERENCES Etudiant(ID)
ON UPDATE CASCADE
ON DELETE CASCADE
);
Explanation of the SQL Statements:
-
Departement Table
- Columns:
Code: Primary Key (PK).
Nom and Domaine: Non-nullable strings.
-
Enseignant Table
- Columns:
ID: Primary Key (PK).
DepartementCode: Foreign Key (FK) referencing Departement(Code).
Prenom, Nom, and Courriel: Non-nullable strings.
- Constraints:
fk_enseignant_departement: Ensures referential integrity with Departement.
- On Update:
CASCADE – updates to Departement.Code propagate to Enseignant.DepartementCode.
- On Delete:
RESTRICT – prevents deletion of a Departement if it’s referenced by any Enseignant.
-
Etudiant Table
- Columns:
ID: Primary Key (PK).
Prenom, Nom, and Courriel: Non-nullable strings.
-
Matiere Table
- Columns:
ID: Primary Key (PK).
EnseignantID: Foreign Key (FK) referencing Enseignant(ID).
DepartementCode: Foreign Key (FK) referencing Departement(Code).
Nom: Non-nullable string.
Duree: Non-nullable integer.
- Constraints:
fk_matiere_enseignant: Ensures referential integrity with Enseignant.
fk_matiere_departement: Ensures referential integrity with Departement.
- On Update:
CASCADE – updates propagate.
- On Delete:
SET NULL – sets the foreign key to NULL if the referenced record is deleted.
-
Matiere_Etudiant Table
- Columns:
MatiereID: Foreign Key (FK) referencing Matiere(ID).
EtudiantID: Foreign Key (FK) referencing Etudiant(ID).
Note: Integer to store grades.
- Primary Key: Composite key consisting of (
MatiereID, EtudiantID).
- Constraints:
fk_matiere_etudiant_matiere: Ensures referential integrity with Matiere.
fk_matiere_etudiant_etudiant: Ensures referential integrity with Etudiant.
- On Update:
CASCADE – updates propagate.
- On Delete:
CASCADE – deletes propagate, removing associations if either Matiere or Etudiant is deleted.
Additional Notes:
-
Data Types: PostgreSQL doesn’t utilize the (10) specification for INTEGER. Thus, INTEGER is used directly.
-
Constraints:
- Primary Keys (
PRIMARY KEY): Ensure each record is unique.
- Foreign Keys (
FOREIGN KEY): Maintain referential integrity between tables.
- Composite Primary Key: In
Matiere_Etudiant, the combination of MatiereID and EtudiantID uniquely identifies each record, preventing duplicate enrollments.
-
ON DELETE and ON UPDATE Actions: These define how changes in referenced tables affect the current table. Adjust them as needed based on your application's requirements.
Feel free to adjust the constraints (like ON DELETE and ON UPDATE behaviors) based on your specific business logic and requirements.